JXMultiSplitPane going bonkers when I switch tabs - java

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 ..

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.

A Flexible Layout manager that will change the layout according to resize

In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.
I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel.
I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.
MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.
The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.
FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).
One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.

Centering the JTabbedPane but not the actual tabs

How would I go about making the length of the tabs automatically resize based on how much room is left in that row of tabs.
Picture:
As you can see the tab's width is based off the text in the tab.
If you need me to explain what I want better then just ask me because I don't know if I made it clear enough.
You can use a custom component and set it's preferred size. For example, in ButtonTabComponent of TabComponentsDemo:
label.setPreferredSize(new Dimension(...));
You have to choose an appropriate dimension based on other aspects of your layout, so it won't be automatic.
I want to define a size for the actual tabbed pan.
The size of the JTabbedPane is a function of the dimensions and LayoutManager of the Container to which it has been added. In the example cited, the default layout of the frame's content pane is BorderLayout, and add(pane) adds it to the center by default.
To accomplish your goal, I see two approaches:
Divide the current width of the enclosing Container among the existing tabs and repaint the tabbed pane, as shown in this example.
Develop your own implementation of TabbedPaneUI and interpret SCROLL_TAB_LAYOUT accordingly.

Java's FlowLayout does not resize correctly

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.

Change button text without changing button size

I'm using a borderLayout to arrange my components in a JFrame and a Box(BoxLayout.X_AXIS) to put buttons next to each other. But it creates two problems:
I want the buttons to have the same size, but it automatically resizes them to fit the text within them (especially annoying when I change the text inside a button at runtime)
I want the buttons to have a little bit of space between them (let's say 10 px)
Is this possible using the borderLayout, or do I need to use the setLayout to null? And if so, wouldn't this screw up the original placement of the buttons in the frame? Or would this still be dealt with by the Box which is placed with the borderLayout?
A couple of suggestions
Try setting the preferredSize to a suitable Dimension value
If that doesn't work, try also setting the maximumSize and minimumSize to this same Dimension value
If that still doesn't work, change the buttons' layout manager to a GridBagLayout. The advantage of this layout manager is that it lets you control the layout's behaviour in minute detail. The disadvantage is that you usually need to configure a large number of properties on the GridBagLayout in order to get the desired behaviour. I'd advise checking out a GridBagLayout tutorial first, as it's a reasonably complex beast.
If you want them to have the same size then just add the buttons to a GridLayout and they will automatically be sized to the largest text string. You can also specify a gap between components.

Categories

Resources