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

asked Apr 2, 2013 at 21:30
\$\endgroup\$
5
  • 4
    \$\begingroup\$ What's wrong with your code? It seems OK to me. \$\endgroup\$ Commented Apr 2, 2013 at 21:34
  • 2
    \$\begingroup\$ If it looks like you want it, it should be ok. I like the design... \$\endgroup\$ Commented Apr 2, 2013 at 21:39
  • 5
    \$\begingroup\$ Gui layouts almost always take a large amount of code. \$\endgroup\$ Commented Apr 2, 2013 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\$ Commented Apr 2, 2013 at 21:50
  • \$\begingroup\$ to expand on the comment by @osundblad Doors, Lights and ShoppingList might be Observer's. \$\endgroup\$ Commented Apr 3, 2013 at 11:21

1 Answer 1

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.

answered Jun 20, 2013 at 10:35
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.