4
\$\begingroup\$

I've never done Java GUI's before. After much trouble I've gotten my GUI to look how I want, however it feels as if my code is very inefficient, as if I'm going about this the wrong way?

     //main panels
private JPanel westPanel = new JPanel(new BorderLayout());
private JPanel eastPanel = new JPanel(new BorderLayout());

//door panels
private JPanel doorsPanel = new JPanel(new BorderLayout());
private JPanel doorsGrid = new JPanel(new GridBagLayout());
private JPanel doorButtons = new JPanel(new FlowLayout());

//light panels
private JPanel lightsPanel = new JPanel(new BorderLayout());
private JPanel lightsGrid = new JPanel(new GridBagLayout());
private JPanel lightTimerGrid = new JPanel(new GridBagLayout());
private JPanel lightButtons = new JPanel(new FlowLayout());

//list panels
private JPanel listPanel = new JPanel(new BorderLayout());
private JPanel listButtons = new JPanel(new GridBagLayout());
private JLabel itemDesc = new JLabel("Item Name - Item Price");
private JLabel totalPrice = new JLabel("Total Price: ");

doorsPanel.setBorder(BorderFactory.createTitledBorder("Manage Doors: "));
doorsPanel.add(doorsGrid, BorderLayout.NORTH);
doorsPanel.add(doorButtons, BorderLayout.SOUTH);

lightsPanel.setBorder(BorderFactory.createTitledBorder("Manage Lights: "));
lightsPanel.add(lightsGrid, BorderLayout.NORTH);
lightsPanel.add(lightButtons, BorderLayout.SOUTH);
lightsPanel.add(lightTimerGrid, BorderLayout.CENTER);

listPanel.setBorder(BorderFactory.createTitledBorder("Manage Shopping List: "));
listPanel.add(listButtons, BorderLayout.EAST);

westPanel.add(doorsPanel, BorderLayout.NORTH);
westPanel.add(lightsPanel, BorderLayout.CENTER);

eastPanel.add(listPanel, BorderLayout.SOUTH);

add(westPanel, BorderLayout.WEST);
add(eastPanel, BorderLayout.EAST);

Current GUI layout

\$\endgroup\$

migrated from stackoverflow.com Apr 3 '13 at 11:16

This question came from our site for professional and enthusiast programmers.

  • 4
    \$\begingroup\$ What's wrong with your code? It seems OK to me. \$\endgroup\$ – Eng.Fouad Apr 2 '13 at 21:34
  • 2
    \$\begingroup\$ If it looks like you want it, it should be ok. I like the design... \$\endgroup\$ – Craig Smith Apr 2 '13 at 21:39
  • 5
    \$\begingroup\$ Gui layouts almost always take a large amount of code. \$\endgroup\$ – BevynQ Apr 2 '13 at 21:40
  • 2
    \$\begingroup\$ I would split the different parts into separate classes (at least three: Doors, Lights, and Shopping List), and a top class that contain these classes. Also if this is your first UI (not just Java UI) I would read a bit about the MVC pattern. \$\endgroup\$ – osundblad Apr 2 '13 at 21:50
  • \$\begingroup\$ to expand on the comment by @osundblad Doors, Lights and ShoppingList might be Observer's. \$\endgroup\$ – Thufir Apr 3 '13 at 11:21
2
\$\begingroup\$

Your code seems perfectly fine!

However...

If all of this code is in the same block (e.g., a constructor or a layout builder method), I would recommend that you break up the layout code into smaller functions. That will improve readability and code structure a bit.

Also, you should definately familiarize yourself with some GUI builders for Java (in my case it was WindowBuilder for Eclipse). Writing layout code by hand can be a pain and a source of bugs that you won't be able to resolve easily.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy