I'm just wondering what is the cleanest way you can write your GUI/Swing code? Right below is the code I have for my main frame. Could you guys let me know if it looks clean/understandable, and if there is any way I can improve it?
The code I have provided is the main frame of my program, which is basically a GUI program that allows users to open different databases, select tables, and then the columns they would like to inner-join. I have a DatabaseView, and a TableView as well. This is my main view. I'm just wondering if I'm doing it properly (organization-wise). Like, if the ActionListeners are where they're supposed to be, how I grouped the customizers, and Actions, etc.
"Export" means exporting the selected tables' columns (inner-joined) to Excel.
public class MainView {
private JFrame frame;
private JPanel dbPanel;
private JPanel previewPanel;
private MainController controller;
private JSplitPane splitPane;
private JTextField txtCondition;
private JComboBox<String> dbChoices;
private CardLayout cardLayout;
public MainView() {
try {
frame = new JFrame();
initFrame();
frame.setVisible(true);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void initFrame() {
customizeFrame();
cardLayout = new CardLayout();
dbPanel = new JPanel(cardLayout);
previewPanel = new JPanel();
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, dbPanel, previewPanel);
customizeSplitPane(splitPane);
dbChoices = new JComboBox<String>();
txtCondition = new JTextField();
JMenuBar menuBar = new JMenuBar();
JMenu mnFile = new JMenu("File");
JMenuItem mntmConnectToDB = new JMenuItem("Open a Database");
JMenuItem mntmDisconnectDB = new JMenuItem("Disconnect Database");
JSeparator separator = new JSeparator();
JMenuItem mntmExit = new JMenuItem("Exit");
JMenu mnOptions = new JMenu("Options");
JMenuItem mntmExport = new JMenuItem("Export");
JMenu mnHelp = new JMenu("Help");
JMenuItem mntmAbout = new JMenuItem("About");
JToolBar toolBar = new JToolBar();
JButton btnOpenDatabase = new JButton("Open Database");
JButton btnCloseDatabase = new JButton("Close Database");
JButton btnExport = new JButton("Export");
Label lblSelectDB = new Label("Select Database:");
JLabel lblEnterCondition = new JLabel("Enter condition: WHERE ");
/*---------------------- Default settings ----------------------*/
mntmDisconnectDB.setEnabled(false);
dbChoices.setBounds(173, 29, 184, 20);
toolBar.setBounds(0, 0, 874, 23);
lblSelectDB.setBounds(10, 29, 146, 22);
lblEnterCondition.setBounds(10, 452, 146, 20);
txtCondition.setBounds(167, 452, 190, 20);
txtCondition.setColumns(10);
/*---------------------- Action Listeners ----------------------*/
mntmConnectToDB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
connectBtnActionPerformed(evt);
}
});
btnOpenDatabase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
connectBtnActionPerformed(evt);
}
});
btnCloseDatabase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
closeBtnActionPerformed(evt);
}
});
dbChoices.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
selectDBActionPerformed(evt);
}
});
mntmAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
aboutBtnActionPerformed(evt);
}
});
/*--------------------------------------------------------------*/
frame.setJMenuBar(menuBar);
menuBar.add(mnFile);
mnFile.add(mntmConnectToDB);
mnFile.add(mntmDisconnectDB);
mnFile.add(separator);
mnFile.add(mntmExit);
menuBar.add(mnOptions);
mnOptions.add(mntmExport);
menuBar.add(mnHelp);
mnHelp.add(mntmAbout);
frame.getContentPane().add(splitPane);
frame.getContentPane().add(toolBar);
toolBar.add(btnOpenDatabase);
toolBar.add(btnCloseDatabase);
toolBar.add(btnExport);
frame.getContentPane().add(lblSelectDB);
frame.getContentPane().add(dbChoices);
frame.getContentPane().add(txtCondition);
frame.getContentPane().add(lblEnterCondition);
}
public void addController(MainController c) {
controller = c;
}
public void addDBchoice(String dbName, DatabaseView dbView) {
dbChoices.addItem(dbName);
dbPanel.add(dbView, dbName);
}
/*---------------------- View Customizers ----------------------*/
private void customizeFrame() {
frame.setTitle("DB to Excel");
frame.setSize(890, 541);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
}
private JSplitPane customizeSplitPane(JSplitPane splitPane) {
splitPane.setBounds(0, 62, 874, 379);
splitPane.setOneTouchExpandable(true);
splitPane.setDividerLocation(600);
return splitPane;
}
/*-------------------------- Actions --------------------------*/
private void closeBtnActionPerformed(ActionEvent evt) {
System.exit(0);
}
private void connectBtnActionPerformed(ActionEvent evt) {
controller.connectDatabase();
}
private void selectDBActionPerformed(ActionEvent evt) {
String selectedDB = (String) ((JComboBox<String>) evt.getSource()).getSelectedItem();
System.out.println("Selected DB: " + selectedDB);
cardLayout.show(dbPanel, selectedDB);
}
private void aboutBtnActionPerformed(ActionEvent evt) {
controller.showAbout();
}
}