This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes GUI - Boxes inside FlowLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "GUI - Boxes inside FlowLayout" Watch "GUI - Boxes inside FlowLayout" New topic
Author

GUI - Boxes inside FlowLayout

Kevin Thien
Greenhorn

Joined: Aug 12, 2012
Posts: 13
Hi guys, I've set up my GUI via FlowLayout and used boxes to set up my UI as needed for an assignment. The issue is that the code looks really messy, and I'd like to break it down further to reduce mess and allow for changes in the future.



Is there a way to change that big bulk to just something like:
top();
info();

?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Why are you using flow layout in the first place?
Do you mean to change those lines to individual methods? Yes, you can do that, but it would be awkward passing references to all the components you have to add. It might be better to keep the code where it is.
Kevin Thien
Greenhorn

Joined: Aug 12, 2012
Posts: 13
Cause that's all we've learnt in class, basically.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
Are you simply adding one Box to the Frame, with a second Box inside? Try a GridLayout with 1 row and 1 column. There is a section in the Java Tutorials about layouts.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
As Campbell has said leave the code where it is but you can make it a little more readable by adding an empty line between each code block and/or comments saying what each code block is achieving. You could also move the buttons action listener to where you create the button rather than having it in the component layout section.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
I think I would leave the listener unchanged, myself.

It just goes to shwo how opinions can vary.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
Campbell Ritchie wrote:It just goes to shwo how opinions can vary.

Well if we all agreed on everything life would be pretty boring

Personally I think layout code is verbose and complicated enough without having the odd line of non-layout related code inserted into the middle of it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37884
    
  22
You mean just move the addActionListener to after all the layout code? Then the listener statement would look exactly the same, and we would agree after all
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2158
    
  47
You mean just move the addActionListener to after all the layout code?

Either after or before, wherever is most appropriate. Personally I tend to create a component, configure it, add listeners etc and then do the layout but I don't have any objection to other coding orders as long as it's logical and consistent.

Then the listener statement would look exactly the same,

Err, yes. I wasn't suggesting changing the statement just moving the whole line to somewhere else.

and we would agree after all

Oh no does that mean life really is boring after all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GUI - Boxes inside FlowLayout
 
Similar Threads
Panel appears only when resizing JFrame.
Gui Q
Cave In
Gray codes
Problem with BoxLayout and JTextField