Java's FlowLayout does not resize correctly - java

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.

Wrap Layout gives an explanation and a solution.

If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.

Related

Get a Fixed sized GridLayout

I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout. On the east portion of the border layout is another Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east. The JFrame is not resizable also.
Is there a way to get a fixed size for the center component while allowing the JTextArea to still expand?
"I need to make a fixed sized for a GridLayout with 100 buttons located in the center portion of a BorderLayout".
Sorry, but that's not going to work. BorderLayout doesn't work like that. You can nest JPanel containers with different Layout managers to get your desired effect.
"Gridlayout that keeps shrinking the center component whenever the text is longer then the size of the current JTextAreas located in the east."
You should wrap your text area in a JScrollPane, and setLineWrap(true) and setWrapStyleWord(true) on you text area. The last two will set it, so that the line typed wraps when it is reaching the right edge of the text area. Also If you are setting the size to the text area, don't. Instead, use the following constructor to set its size
JTextArea jta = new JTextArea(20, 50); <--- rows, and character columns
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(jta);
container.add(scroll); <--- make sure you don add jta anywhere else
Without more context to your querstion, these are really the only valid suggestions I can make.

List of contacts swing

I need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.

JTextArea auto height resize

All component widths are same.
I have a JPanel (box layout; PAGE_AXIS).
I have added a Box.filler on it which has following specifications:
Min size(height) -1
Preferred size(height) JPanel height
Maximim size(height) JPanel height
I added JTextArea with border 7 pixels thick (only below).
When I type in text, the JTextArea expands, but not enough, and after a couple of lines the text goes off bounds (you can't see it anymore).
I can determine how many lines of text exist (using FontMetrics), and I can force setSize accordingly, but nothing really changes. Printing out JTextArea.getHeight() does show that the size has changed on my terms, but visually it stays as if it didn't. And I haven't forgot validating and repainting.
How can I force the JTextArea to resize to certain size?
You could alternatively just make the JTextArea wrap words?:
JTextArea.setWrapStyleWord(true);
you could also try setting the amount of rows using setRows(int rows) and then revalidate() and repaint()
You could put the JTextArea into a JScrollPane to keep the additional lines visible without changing the space the JTextArea needs on screen.
See the Oracle Tutorial on how to use Scroll Panes for more information.

Java - Having trouble setting JFrame to a good size

I'm making Minesweeper as a school project. It's close to completion, but the only problem now is setting JFrame's size. I just can't figure out a way to set frames to the size I want.
The program looks almost like a Swing version of the original Minesweeper on Windows XP.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button. The top panel's using flow layout, and the bottom panel's using grid layout for the buttons.
I set the preferred size of the frame's content pane. Getting the width is easy (The numbers of fields in a row * my button size), but the problem is getting the height right. The frame always go down to the 2nd last row of the minefield.
I also tried pack() but it resizes it to the preferred size of the content pane, which isn't the right size to begin with. What can I do?
Don't have the JFrame (or better its contentPane) use FlowLayout since this won't give the JFrame the best size for its components. Instead why not have it use the default BorderLayout? Your mine cell's will probably have their getPreferredSize() method overridden and thus will direct the size of the enclosing containers. As always, call pack() on the JFrame after filling it with components and before calling setVisible(true) on it.
Set a preferred size for the buttons in the GridLayout and pack() the frame after adding them.
Don't try to manually set the size. You should let each component display at its preferred size and use the pack() method.
The main frame's layout is flow layout. There's a top panel for the time, mines, and reset button
I would use a BorderLayout. Create a top panel and add it to the NORTH.
Then create a panel for the grid and add it to the CENTER. If you have problems with the buttons in the grid resizing then try creating a JPanel as a wrapper panel. Add the buttons to this panel and then add this panel to the CENTER of the frame. The panel will retain its preferred size.

JXMultiSplitPane going bonkers when I switch tabs

I'm using JXMultiSplitPane (from SwingX 1.6.2) to implement a three-pane horizontal interface. In the center pane is a JTabbedPane with two tabs: one with a JTextArea (in a JScrollPane, of course) used for entering Markdown code and the other a JEditorPane (again, in a scroll pane) for displaying a rendered HTML preview. When the user switches to the preview pane, the text in the editor is processed and displayed in the preview pane.
My problem is that if I enter text in the editor with long lines, and then switch to the preview, the center pane will expand. Sometimes it's just by a little bit, other times it'll take up more room than is actually on the screen. But if I move one of the resize handles manually, everything will snap back in place.
I've found only two ways to deal with this before it happens:
Manually resize one of the panes before entering any text.
Give the center pane a weight of 1 in the MultiSplitLayout model.
I can't use the second one since it will expand the center pane to take up almost the whole window by default.
Is there a way to fix this?
Update
After a little more testing, even technique (2) doesn't keep the size constant; switching between the two tabs changes the size of the center pane slightly.
I now believe that the problem is partly with the tabbed pane. The JTextArea and the JEditorPane do not have the same size and that JTabbedPane is resizing when I switch between them (since I'm resetting the JEditorPane text every time. This wouldn't be a problem except that JXMultiSplitPane will keep automatically resizing the center pane until the user forces a specific size by resizing manually.
So I should be able to fix the issue by making the size of the JTabbedPane fixed, but still able to be resized by the handle bars. Any tips on doing that?
The MultiSplitLayout is .. a LayoutManager, so you have to understand how it works (me too, not overly familiar with it myself :-)
The basic layout happens according to the component's prefSize, the weights are for distributing excess/missing space relative to the pref. By default, the dividers are "floating", that is they are positioned between the components as layouted by the basic mechanism. The moment a user touches a divider, dividers are "not-floating", comp sized to fit in-between the dividers. That's the reason for you not seeing the size-greed after moving the divider once. So one ways out is to
setup the JXMultiSplitPane as usual, add the components and realize the frame
fix the dividers after the manager has done its initial layout
String layout = "(ROW " +
"(LEAF name=selector weight=0.15)" +
"(LEAF name=center weight=0.7)" +
"(LEAF name=list weight=0.15)" +
")";
JXMultiSpitPane pane = new JXMulitSplitPane((MultiSplitLayout.parseModel(layout))
// add components and realize the frame
...
pane.getMultiSplitLayout().setFloatingDividers(false);
Alternatively, give more weight to the weights - force the layoutManager to use them for the layout itself (instead of only for the distribution of excess/missing space). A side-effect is that the prefSize of the comps might be set (by the layout, which is a no-no-never, but who's perfect ;-)
pane.getMulitSplitLayout().setLayoutByWeights(true);
Not sure which way I would prefer or if/how that could be made easier in the multisplit ..

Categories

Resources