Java - Layout Manager Selection - java

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.

A BoxLayout will do the trick as demonstrated in this posting

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.

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)

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.

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

Related

Wrapping Text in Java Swing

Okay, I am kind of desperate right now. I hope you guys can help.
I need to layout content panels with Java Swing. The Problem is, that every content is different. So I need a panel that resize itself for every content. Basically what LayoutManagers are invented for.
I need a left panel and a right panel. The widths of the panels should be fixed. The heights should adjust to the given content
|<---- 30% ------->|<----- 70% -------------------->|
Easy going I thought, but it just wont work. I tried different layout managers. Some of them keep the 30% rule, but doesn't wrap the content and just display them in one single line (BorderLayout).
If a LayoutManager does support line-break (even if its just for HTML text but that is fine for me) it wont support the fixed width. A combination of both didn't worked for me either.
Note that I need to stick to Swing and can not use another more advanced library because the system I am developing for is stuck to Java 1.5. Furthermore, I know the total screenwidth so I could calculate the width of the panels to work with fixed widths, but I need to be flexible with the height.
You can achieve this by using nested BorderLayouts. Start by setting your Panel's layout as BorderLayout.
After that, for each left and right panels, set layouts as BorderLayout again. At this level, you will set %30 and %70 ratio.
Within this layouts, add your contents to NORTH layouts. This will enable your panels' height to match given content.

How to setup this GUI with Java Swing?

I got a GUI to make and I've tried so many ways, but I can't seem to build it correctly, I always get the layouts wrong and my application gets all messed up. I just want some help on how to build something close to the GUI in the image, except the image part, that isn't necessary. Doesn't need to be exactly like it, I just need some ideas of what layouts to use and how. Thanks
As with all problems, start by breaking it down into manageable chunks.
Associate the various UI elements together into groups of "functionality" and separate them into separate components - this way you can focus on the individual layout requirements for each section
From this, I would then focus on breaking down each group into individual components and focus on there core functionality
So this is pretty basic, I'd use a GridBagLayout because it would allow me to anchor the components to the top position of the container. You could also use the VerticalLayout from SwingX
So, there are a number of possibilities, but, because it would be simple to do, I'd use a GridBagLayout, as it would allow me to provide more space for the two lists
A little more complicated, but still, GridBagLayout, as it allows more control over providing space to individual components
And, GridBagLayout for all the reasons highlighted above.
Finally...
Bringing it all together...
I'd probably start by placing the green, blue and yellow sections into a separate container (using, surprise, a GridBaglayout, as it allows for components to expand cells) and then combine it with the purple section onto a container using a BorderLayout
I would recommend that you take the time to look at Laying Out Components Within a Container and try somethings and see what does and doesn't work.

Is there anyway to "pack" a JPanel?

Is there any thing I can do to make my JPanel pack like a JFrame, or do something like that. I want to do this instead of giving it dimensions. Please comment if any addition info is needed. Thanks.
Please try JPanel.updateUI and let me know if that helps.
You should make GUI calls in the Event Dispatcher Thread:
EventQueue.invokeLater(() -> {
someJPanel.updateUI();
});
If I understand you correctly, your JPanel is not being automatically (re)sized, correct? In that case, you can use Component.validate (or JComponent.revalidate())
Let me explain my use case, so that my answer will make sense.
I have a game board G on which I am absolutely positioning tiles Ts and some other property panels say P, Q and R.
Only the Game board G doesn't use a layout. However, for all the tiles and other panels, I do want to use layout management. For the
tiles, I want to calculate and set both position and size based on how many tiles are there. So, the setBounds method works great.
The problem is with the other panels like P where I know the x and y where I want to place them, however, I would like it if they figured out their own preferred sizes. This becomes a problem because setBounds insists on setting both position and size, and setLocation doesn't seem to work.
The solution which worked for me is putting a slightly oversized panel Outer-P and setting opaque to false and then placing P inside it. Note that Outer-P has a layout manager which allows P to be its preferred size. Personally, I used new MigLayout("insets 0","[]","[]") which lets the child take its preferred width and height.
As far as the user is concerned, P is in the top left, and looks packed. The wrapper panel is invisible.

How to resize components when resizing a JFrame? Using Null Layout

I am using the NetBeans Designer to create a JFrame. Also it is worth mentioning I am fairly new to Java so I might not understand some things / do things correctly. The frame has about 100 panels, more buttons than I would even think about counting, about ~40 tables, basically most swing components the NetBeans designer provides are being used within the frame. Also for the main frame I am using Null Layout (in order to have a background image inside a JLabel). I know it is not recommended but it doesn't affect the general layout of things as I'm using panels/LayeredPane/TabbedPane for everything, each with it's own design (most of them on Free Design with no Layout specified - that's how I started, didn't know about Layouts and it would take ages now to rearange everything after using Grid Bag Layout for example).
Now getting to my problem, I need to be able to resize the frame and make it resize all components contained. I have to carry a presentation tomorrow of it and I just noticed it doesn't fit on smaller displays (and resizing it doesn't do it properly, it just hides components). I do not care much if it's just an improvisation / not the best approach to the problem as after the 15 min presentation I will probably never open it again.
Thanks.
I would try to go through all the components tree and try to set them smaller font and reduce all their bounds to some static %.
In other words for each component multiply x,y,widht,height to e.g. 0.75 and call setFont() passing derived font of 25% smaller.

Java Swing: Choosing the correct LayoutManager

I'm building a PropertyPanel. Currently I'm using a GridLayout to manage the JLabels and their corresponding fields where I can specify the value. But the problem is that the GridLayout automatically manages the size of the columns: it makes them the same width.
This means when I'm having a big value field, the colum, is getting bigger (which is good), but the other column (with all my JLabels) is getting bigger as well. Here is a screenshot:
< BAD
As you can see, the image property has a huge value, which makes both columns bigger, and I'm having a lot of space after the JLabels.
So, I'm searching for a LayoutManager which makes each column as big as necessary.
I want a layout like this (it's edited with Gimp):
< GOOD
Thanks
You can use SpringLayout for this. See How to Use SpringLayout.
Example layout:
Remember that you also can nest layouts.
SpringLayout is what I typically use for forms like this. Although I think GridBagLayout would also work nicely.
I tend to try to hack everything by mixing GridLayout and BorderLayout, so maybe it's not the best solution but...
Create two GridLayouts, both have a single column. One for the labels the other for the controls.
Now create a BorderLayout to be the parent.
Add the left grid to the BorderLayout.WEST and the right grid to the BorderLayout.CENTER.
While this was answered 11 hours ago, I just thought I'd pop in & make a suggestion. I suggest GroupLayout.
I was looking to break from nested layouts for a name/value dialog recently and looked at both GroupLayout & SpringLayout. It seemed the only advantage offered by SpringLayout was that it could achieve the right aligned text of the labels (there may be a way to do it using GL, but I couldn't figure out how). On the downside, the Java Tutorial examples for SpringLayout used a whopping 'helper class' to define layout constraints.
In the end (it was only a very short 'study') I chose to use GroupLayout.
Consider using MigLayout. If constrained within the current JDK, GridBagLayout.
Here's an overview of the standard LayoutManagers:
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
You could e.g. use GridBagLayout or the non-standard MigLayout, if you want to code the GUI by hand.
If you want to use a GUI builder (e.g. the one in NetBeans) you could use the GroupLayout.

Categories

Resources