tags:

views:

286

answers:

5

Is there a simply layout manager I can use in a JPanel to create something akin to a bar chart? FlowLayout almost meets this need. The added component orientation needs to be left to right (default for FlowLayout), but they need to "rest" on the bottom of the panel with excess space at the top (not available in FlowLayout). Also, the components will all the be the same height and width.

Thanks.

+3  A: 

A BoxLayout will do the trick as demonstrated in this posting

camickr
A: 

A BoxLayout might work for you. It lets you layout components left-to-right or top-to-bottom, with the tightly coupled Box class to force spacing constraints.

Kevin Montrose
A: 

I actually prefer the FormLayout, since it is very flexible but you have to write a lot of code though. And in the beginning its a little bit confusing with its percentage and pixel parameters.

But you can for example tell a control that it is 5 pixels left of another control (thats the main part...it layouts controls in relation to neighbors), then it takes 100% of the lasting space availabel including a border space of 5 pixels (you need to use -5 then).

I think it looks somewhat similar to this

FormData data = new FormData();
data.left = new FormAttachement(neighborControl, 5);
data.right = new FormAttachement(100, -5);
...
button.setLayoutData(data);

This example is for JFace, but there are Swing implementations as well. I will look up my old code later this day to check if the code I wrote is right :)

Here´s a additional link

lostiniceland
+1  A: 

If you are going to do something like a bar chart, you might want to consider not using Components at all. Just have a single JComponent that overrides (IIRC) paintComponent. It'll be easier to do the calculations in a manner appropriate to a bar chart rather than trying to use an inappropriate layout manager abstraction.

FWIW, I default to GridBagLayout, even if a simpler layout manager will do, on this basis that the code can be more consistent.

Tom Hawtin - tackline
+1  A: 

You can do exactly what you want in GridBagLayout. Yes, I know everyone hates GBL; yes, I know I'll get down-voted. But it really is not difficult to understand and you can use it for almost any layout goal.

The trick to get a component to "stick" to the bottom is to use the anchor and fill properties of the GridBagConstraints object properly (i.e. SOUTH and NONE)

oxbow_lakes
GridBagLayout is also nice. But I remember that I didnt like it in the beginning as well ;)Still, in some cases FormLayout generates nicer results when it comes to an actual resize. For example I dont like it when buttons or textboxes change their height. Or can this be set to fix with GBL as well?Anyway + :)
lostiniceland
Yes - you can make sure a text field or combo box will get no extra vertical spacing by setting its weighty to 0 and its Anchor property to either NONE or HORIZONTAL
oxbow_lakes