aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Looking for layout advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Looking for layout advice" Watch "Looking for layout advice" New topic
Author

Looking for layout advice

jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
I have an application that monitors servers on our network.
It's main screen has a tabbed pane. On one of the panes I want to plot the response times of three different servers.

How can I get three canvases on a panel that I will place on the tabbed pane?



I don't know if that will display what I am trying to do or not.

Stated another way, there are three canvases placed in the CENTER of three panels. Those panels a placed in a parentPanel (NORTH, CENTER, and SOUTH). That parentPanel is then placed in the tabbedPane.

I'm running into a number of irritating problems and I was just looking here to see if anyone thinks this layout won't work, and if so, could you suggest another approach to displaying this data?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

My first question would be, why do you have 3 canvas' on 3 JPanels that are placed on a parent JPanel. It seems like the 3 JPanels could just be omitted. They seem like an extra component you don't need?

With that being said, I'm not real clear yet on the layout you are needing. You seem to be using a BorderLayout and when putting components N, CENTER, and S, the center component will be larger than the N and S components.

If you just want to line up 3 components vertically, take a look at either a BoxLayout or GridLayout.


GenRocket - Experts at Building Test Data
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You mention using canvases; I would use a JPanel instead of a Canvas — mixing Swing and AWT components can give undesirable results.

One of the problems of using a BorderLayout is that the North and South sections will need some component vertical size information to properly display your components. They expand the components to fill the horizontal space. This can get messy. If you know the height you want for the North and South components you can do

Assuming all three graphic components are to be the same size you could try GridLayout (as Gregg recommends above) or a GridBagLayout. Here's a demo of those two:
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
GridLayout (3,2) seems to be yielding some better results.

It's also introduced some, but I think they are more easily explained here.

Check out this code fragment:


This is placing one of my canvases on the tabbedPane, but it has the side affect of making my button the same height as my canvas. Do I have to specifically set the size of that button to get around that problem?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
GridLayout ignores the preferred size of the components you hand it and tries to display all of its components at the same size. To keep the button at its preferred size you can add it to a JPanel and add the JPanel to the GridLayout (or BorderLayout). The JPanel will expand to fill the grid cell and allow the JButton to remain at its preferred size. The general practice in Swing is to use the JComponent methods get/setPreferredSize in lieu of setSize.
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
OK, I'll give the button on a Panel a try.

I've changed from Canvases to panels and that has also cleared up some of my problems.

I was seeing the canvases appear drawn overtop of other components and I was assuming that it must be happeneing because of the order in which I was creating the components.

However, like I said above, as soon as I switch to JPanels in place of the Canvases, that problem went away.

Thanks a lot, guys.
 
Don't get me started about those stupid light bulbs.
 
subject: Looking for layout advice