aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JSeparator issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JSeparator issues" Watch "JSeparator issues" New topic
Author

JSeparator issues

Tyler Kenna
Greenhorn

Joined: Sep 08, 2012
Posts: 7
Hi Everyone!

I am trying to create a GUI for a class project involving recursion. We have 4 different recursive problems to solve, and the option to present them graphically. i would like to add 4 separate panels to a frame, and separate them with JSeparator. Here's what I see in my head:

EDIT: Apparently formatting doesn't work on here to draw a frame. Either way, I'd like it to be a pretty 2X2 grid with JSeparators inbetween, but it is not nearly that pretty.

I will post the code below. In short, I'm using GridLayout. I have tried both pack()ing the frame and setting it to a constant size. I have also set each of the JPanels to 200X200. I have not added anything to the panels themselves yet (buttons, fields, etc), but if I am not mistaken that should not make a difference since I setSize(200, 200)d each of the panels. I'm thinking perhaps I should be using something other than GridLayout (like BoxLayout) but I'm not sure. If you can point me in the right direction, that would be appreciated. Thanks!

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2317
    
  49
You haven't said what the display problem actually is and the posted code can't be run so it's a bit hard to provide definitive help. However I can tell you the GridLayout forces all components to be the same size which means the separators will be the same width and height as the panels which is probably not what you are trying to achieve, So I'm guessing this is your problem.
I assume you also want for example your vertical separator to have a fixed width regardless of the how much wider the user makes the panel ie all extra space will be given to the panels and none to the separator. You can certainly do this with GridBagLayout, not sure about BoxLayout as I haven't used it much.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i would like to add 4 separate panels to a frame, and separate them with JSeparator.

instead of a JSeparator, perhaps adding a border to each panel might work.
Tyler Kenna
Greenhorn

Joined: Sep 08, 2012
Posts: 7
GridLayout forces all components to be the same size which means the separators will be the same width and height as the panels which is probably not what you are trying to achieve, So I'm guessing this is your problem.


That's what is happening. After reading what you said, I decided to build the frame using netbeans builder just to see if I could spot what was happening, and when I selected one of the separators, the entire grid box was selected. Good to know. I'm still new to GUIs, so that really helps.

instead of a JSeparator, perhaps adding a border to each panel might work.


+1 to this. For my purposes, borders may work better. I didn't even think of that.

Thank you both!

Tyler
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2317
    
  49
If you aren't too worried about not having JSeparator objects between the items then borders are a good way to do it but are a bit fiddly if you only want lines between the components and not around the outside of them. Another approach is as follows:
Set the background panel to opaque and give it the background colour you want to use for the dividing lines. Set the layout to GridLayout with a component spacing of the dividing line width you require. Set each of your panels to opaque and give them the background colour you want and finally add them to the root panel. ie

 
 
subject: JSeparator issues