• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI - Boxes inside FlowLayout

 
Kevin Thien
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cause that's all we've learnt in class, basically.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I would leave the listener unchanged, myself.

It just goes to shwo how opinions can vary.
 
Tony Docherty
Bartender
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2965
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic