FlowLayout performs "pressure" from the right, so as all components are trying to take their minimal widths. Contrary, BoxLayout tries to spread all components to fill entire height of the space.
Can I add some filler as last component or something to make all components have minimal heights in BoxLayout?
You could use Box.createGlue(), which returns a component that takes up as much space as the BoxLayout will give it. Adding it to the bottom of a vertical BoxLayout will scrunch the other components to the top.
You could also use nested layouts.
Related
I don't know which is the best layout to use for this image, I think maybe grid layout (5*1) may be helpful but I can't add multiple components in one grid layout cell.
Here's the GUI:
The sections of that column the Currency Exchange GUI seem to be of equal height, which would suggest a single column GridLayout. For unequal height sections, perhaps a GridBagLayout or a BoxLayout as suggested in the comments.
Each of the sections themselves looks to be a default FlowLayout, with a line of components centered with some padding between.
This is a mock up for what I have in mind as a layout for my project:
The way I tried to accomplish this is:
I set the entire frame to a border layout and then cut it horizontally with two panels, we'll call them north and south panels. The south panel is Panel 3 from the first picture.
I set the north panel to a border layout as well and cut it vertically with two panels. These become panel 1 and panel 2 in the first picture. The problem occurs when I try to resize the window. I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same. The problem is, instead of resizing, the panels just move away from each other like so:
Any ideas for creating the desired design? Am I on the right track or is there another swing layout that is better suited to my needs?
I would like the panels to scale proportionally to eachother so the size ratio's between the panels stay the same.
Try using a horizontal BoxLayout. I believe it will allocate extra space proportionally up to the components maximum size if extra space is available.
Or if that doesn't work you can use a GridBagLayout. You can use the weightx constraint for each component you add to the panel. This will control how much extra space is given to each component.
Read the section from the Swing tutorial on Layout Managers for working example of each.
This doesn't seem like it should be very hard but I can't figure out how to do this:
I have a subclass of JPanel. It has a fixed height, but can be any width. The subclasses, on construction, set their preferred size using setPreferredSize(), which means I have to provide a width in addition to the height.
I would like to make a scrolling list of some number of my subclass, where the subclasses all fill the available horizontal space.
Right now I have a scroll view containing a JPanel containing my subclasses. The containing JPanel uses a BoxLayout with a vertical orientation.
Vertically, it looks great. Horizontally, my custom panels are just stuck at the preferred size. What would be the easiest way to make my panels fill the available horizontal space? I tried writing some layout listeners for them, but the performance was flakey (it seems that sometimes the event messages get dropped?) and the code looked hacky. Other views, such as the JList, JTree, and scroll views seem to resize automatically to fill the available space in a BoxLayout, so I feel like there must be something I can do in my JPanel subclass that I haven't thought of.
I'm open to using another Layout Manager if something else is better suited for this. I looked at GridBagLayout, but that seemed more geared towards static layouts where components aren't added and removed at runtime.
Edit: I found this on Oracle's Documentation for BoxLayout which has an example that looks exactly like what I want:
What if none of the components has a maximum width? In this case, if all the components have identical X alignment, then all components are made as wide as their container. If the X alignments are different, then any component with an X alignment of 0.0 (left) or 1.0 (right) will be smaller. All components with an intermediate X alignment (such as center) will be as wide as their container. Here are two examples:
Could someone show me the code that will produce the same results? The example code in the documentation doesn't look like it covers this particular picture.
When you put your component in a JScrollPane, your component may implement the Scrollable interface to adjust the scrollpane’s behaviour. By doing this you can implement the method getScrollableTracksViewportWidth() to return true so your component will always have the available width and be scrollable in vertical direction only.
This is how JList, JTable, JTree, and all the text components of Swing do it.
Alright - I found the problem. It appears that the default implementation of getMinimumSize() and getMaximumSize() will simply return the value of getPreferredSize() if it is set. So by setting a preferred size without a maximum size, my maximum size was the preferred size. By overriding getMaximumSize() to return (99999, preferredHeight), it now works exactly as I want.
I have a JTextArea wrapped in a JScrollPane, which I use to log my application's output. I'm using the default, plain font with a size of 9 for the text area, and the scroll pane's height is 48 px. This results in an even distribution of lines in the scroll pane view, but there's a problem: if you scroll all the way up or all the way down, this happens:
As you can see, the top line got cut off, which is why I'm wondering if there's a way to limit the scroll pane's scroll range so it, for example, can't reach the top or bottom 6 pixels. Alternative solutions are also welcome.
You could change the margin (top/bottom) of your JTextArea by setting a custom Border using the method setBorder inherited from JComponent. The documentation for JComponent suggests the following:
Although technically you can set the border on any object that
inherits from JComponent, the look and feel implementation of many
standard Swing components doesn't work well with user-set borders. In
general, when you want to set a border on a standard Swing component
other than JPanel or JLabel, we recommend that you put the component
in a JPanel and set the border on the JPanel.
That would yield the same result as limiting the scroll range, while being more straight forward.
EDIT:
OP reported that the following solution worked for him:
textAreaLog.setBorder(BorderFactory.createEmptyBorder(0, 6, 0, 6));
Place the JTextArea in a JPanel with empty borders where top and bottom insets are 6 pixels?
I have two JPanels (let's call these Panel1 and Panel2). These panels are of the same width, but varying heights.
I want to put these JPanels into one big JPanel (lets call it Panel0), and stack them on top of each other (I decided to set Panel0's layout as GridLayout(0,1)).
The problem, is that both nested panels (panels 1 and 2) end up having the same dimensions (those of the biggest between the two), instead of the setPreferredDimension and setDimension that I set to them.
Sorry, I can't really provide any code (there's a lot of crap added to the panel's, and it's all for something work-related). Any advice? Thanks!
GridLayout forces all components to be the same size; that's why it's called a grid.
Since you only have two panels, I'd suggest using a BorderLayout with one panel at NORTH and the other CENTER. If you allow resizing, then the one in CENTER will be the expand to fill any extra vertical space, so just be aware of that.