Is it normal to have all java swing gui in one class?

java, swing, user-interface

Solution

No, I don't recommend to put all GUI code in class - at least if it gets big and does several things.

In that case use the Mediator pattern:

- put subcomponents to custom subclasses of JPanel

- that subclass listens for events of its children and updates other children

- fires own custom events if needed

Also note: If you're about to learn GUI programming and don't have a specific requirement to use Swing, consider to use the newer GUI toolkit JavaFX.

Problem

I am just starting out with swing development and am having an issue. Is it normal to put the entire GUI into a single class? The application that I am building has one single JFrame that displays multiple different "pages". For example if the user clicks on a button they are taken to an entirely different page with a different layout. I've configured a Card Layout, and the one card that I have build so far uses the GridBag layout. The question that I have, then, is 1. whether or not each page should have its own class? 2. If they do how do I communicate between the GUI controller which runs the card layout and the individual pages? 3. Or should I just put all of the GUI into the GUI controller and let it run like that. Below is the code for what I have so far, I am new to this and really would like to get good at it so if you spot any major issues that I missed please feel free to point them out. Code for the individual page: ``` public class HomePage extends JPanel implements ActionListener{ private GridBagLayout gl; private JPanel frm; JButton newPersonalContact; HomePage(){ frm=new JPanel(); gl=new GridBagLayout(); GridBagConstraints gbc=new GridBagConstraints(); frm.setLayout(gl); newPersonalContact=new JButton("New Personal Contact"); JButton newBusinessContact=new JButton("New Business Contact"); JButton showAllContacts=new JButton("Show All Contacts"); JButton saveAndQuit=new JButton("Save and Quit"); JPanel top=new JPanel(); top.setBackground(new Color(218,165,32)); top.add(new JLabel("Western Governers University Presents:")); JPanel middle=new JPanel(); middle.setBackground(new Color(43,37,85)); GridLayout ge=new GridLayout(4,4); middle.setLayout(ge); middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(newPersonalContact); middle.add(newBusinessContact); middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(showAllContacts); middle.add(saveAndQuit); middle.add(new JLabel(""));middle.add(new JLabel("")); middle.add(new JLabel("")); middle.add(new JLabel("")); JPanel bottom=new JPanel(); bottom.setBackground(new Color(218,165,32)); gbc.fill=GridBagConstraints.BOTH; gbc.weightx=1.0; gbc.weighty=2.0; gbc.gridx=0; gbc.gridy=0; frm.add(top,gbc); gbc.weighty=6.0; gbc.gridx=0; gbc.gridy=1; frm.add(middle,gbc); gbc.weighty=1.0; gbc.gridx=0; gbc.gridy=2; frm.add(bottom,gbc); newPersonalContact.addActionListener(this); newBusinessContact.addActionListener(this); showAllContacts.addActionListener(this); saveAndQuit.addActionListener(this); } public void actionPerformed(ActionEvent ae){ if (ae.getSource()==newPersonalContact){ //What goes here? } } public JPanel getFrame(){ return frm; } } ``` code for the GUI controller: ``` public class GUIController { JFrame frm; CardLayout cl; Container pane; GUIController(){ frm=new JFrame(); frm.setSize(800,600); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel card2=new JPanel(); card2.setBackground(Color.black); JPanel cards=new JPanel(new CardLayout()); cards.add(new HomePage().getFrame(), "Home"); cards.add(card2,"New Personal Contact"); pane=frm.getContentPane(); pane.add(cards,BorderLayout.CENTER); } public void start(){ this.frm.setVisible(true); } public void showCard(){ cl.show(pane, "Card2"); } } ``` While this is for a school Project, the GUI interface is not required just something I am interested in learning. Thanks for any help.

Original source