📄 databasegui.java
字号:
//Vedio rental System Developed by Banu
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;
public class databaseGUI extends JPanel implements ActionListener
{
private final String strFileName = "database.mdb";
private final int width = 800, height = 500;
private JTabbedPane tabbedSystem;
private databaseToolbar panToolbar;
private databaseMenu menuSystem;
private tableList listTables;
private Connection databaseConnection;
private void setupConnection ()
{
try
{
databaseConnection = connectDatabase.getConnection(strFileName, "", "");
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
private void setupTableList ()
{
try
{
listTables = new tableList(databaseConnection);
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null, "Error reading from\n" + System.getProperty("user.dir") + "\\" + strFileName + ". Please check permissions", "Database Access Error", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
public databaseGUI ()
{
this.setPreferredSize(new Dimension(width, height));
this.setLayout(new BorderLayout());
tabbedSystem = new JTabbedPane();
tabbedSystem.setPreferredSize(new Dimension(width, (height - 40)));
setupConnection();
setupTableList();
menuSystem = new databaseMenu(this);
menuSystem.tableOpen(false);
panToolbar = new databaseToolbar(this);
panToolbar.tableOpen(false);
this.add(panToolbar, BorderLayout.NORTH);
this.add(tabbedSystem, BorderLayout.SOUTH);
}
public Container getGUI ()
{
return this;
}
public JMenuBar getMenu ()
{
return menuSystem;
}
private boolean isTableOpen (String strTableName)
{
for (int i = 0; i < tabbedSystem.getTabCount(); i++)
{
if (tabbedSystem.getTitleAt(i).equalsIgnoreCase(strTableName))
{
return true;
}
}
return false;
}
private void tableOpen (boolean state)
{
menuSystem.tableOpen(state);
panToolbar.tableOpen(state);
}
public void actionPerformed (ActionEvent e)
{
String strCommand = e.getActionCommand();
if (strCommand.equalsIgnoreCase("toolViewTable"))
{
viewTable digView = new viewTable(null, listTables.getList());
digView.setVisible(true);
String strTableName = digView.getChosenTable();
// testing point, can be removed
System.out.println("Reached view table through tool bar " + strTableName);
if (!strTableName.equals(""))
{
if (isTableOpen(strTableName))
{
JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
tabbedSystem.addTab(strTableName, new objTab(strTableName,databaseConnection, "View"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
// following code for view command selected from menu bar. this is to by-pass the table select window
else if (strCommand.equalsIgnoreCase("viewCustomer") || strCommand.equalsIgnoreCase("viewVedio") || strCommand.equalsIgnoreCase("viewRental"))
{
String strTableName="";
if (strCommand.equalsIgnoreCase("viewCustomer"))
strTableName = "Customer";
else if (strCommand.equalsIgnoreCase("viewVedio"))
strTableName = "Vedio";
else if (strCommand.equalsIgnoreCase("viewRental"))
strTableName = "Rental";
// testing point
System.out.println("Reached View selected table");
if (isTableOpen(strTableName))
{
JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
tabbedSystem.addTab(strTableName, new objTab(strTableName,databaseConnection, "View"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
}
}
else if (strCommand.equalsIgnoreCase("reportOverdue"))
{
try
{
tabbedSystem.addTab("Overdues", new objTab("Rental",databaseConnection, "Report"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
}
else if (strCommand.equalsIgnoreCase("toolSelectTable"))
{
selectTable digSelect = new selectTable(null, listTables.getList());
digSelect.setButtonText("Edit");
digSelect.setVisible(true);
// testing point
String strTableName = digSelect.getChosenTable();
System.out.println("Reached select table " + strTableName);
if (!strTableName.equals(""))
{
if (isTableOpen(strTableName))
{
JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
tabbedSystem.addTab(strTableName, new objTab(strTableName , databaseConnection,"Edit"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
else if (strCommand.equalsIgnoreCase("newRecord"))
{
selectTable digSelect = new selectTable(null, listTables.getList());
digSelect.setButtonText("Add");
digSelect.setVisible(true);
// testing point
String strTableName = digSelect.getChosenTable();
System.out.println("Reached select table " + strTableName);
if (!strTableName.equals(""))
{
if (isTableOpen(strTableName))
{
JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
tabbedSystem.addTab(strTableName, new objTab(strTableName , databaseConnection,"New"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
System.out.println(" Catch block after objTab");
}
}
}
}
else if (strCommand.equalsIgnoreCase("deleteTable"))
{
selectTable delSelect = new selectTable(null, listTables.getList());
delSelect.setButtonText("Delete");
delSelect.setVisible(true);
String strTableName = delSelect.getChosenTable();
if (!strTableName.equals(""))
{
if (isTableOpen(strTableName))
{
JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
tabbedSystem.addTab(strTableName, new objTab(strTableName , databaseConnection,"Delete"));
tableOpen(true);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
else if (strCommand.equalsIgnoreCase("closeTable"))
{
tabbedSystem.remove(tabbedSystem.getSelectedIndex());
if (tabbedSystem.getTabCount() == 0)
{
tableOpen(false);
}
}
else if (strCommand.equalsIgnoreCase("closeAll"))
{
tabbedSystem.removeAll();
tableOpen(false);
}
else if (strCommand.equalsIgnoreCase("outputTXT"))
{
try
{
String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex());
outputFile.TXT(listTables.getRecords(strTable), strTable);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error", JOptionPane.ERROR_MESSAGE);
}
}
else if (strCommand.equalsIgnoreCase("outputXML"))
{
try
{
String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex());
outputFile.XML(listTables.getRecords(strTable), strTable);
}
catch (Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error", JOptionPane.ERROR_MESSAGE);
}
}
else if (strCommand.equalsIgnoreCase("exit"))
{
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -