Question

I am having a hard time to properly set the way my swing components behave on resize.

I have two problems with that interface: expected

A: The toggle button at the beginning of each row is here to collapse/expand the text. All the elements are contained in a JLayeredPane. On the button click, I edit the pane's height to expand or collapse the content (either 31 or 310). Expand works fine an pushes the elements below. On the other hand, collapse does hide the text but leaves all the elements in position. Here is my code:

private void expandText(java.awt.event.ActionEvent evt) {
    JToggleButton button = (JToggleButton) evt.getSource();
    Container parent = button.getParent();
    Dimension size = parent.getSize();
    String icon;

    if (button.isSelected()) {
        size.height = 310;
        icon = "/org/cytoscape/ocsana/resources/images/minus.png";
    } else {
        size.height = 31;
        icon = "/org/cytoscape/ocsana/resources/images/plus.png";
    }

    parent.setSize(size);
    try {
        button.setIcon(new ImageIcon(ImageIO.read(getClass().getResource(icon)).getScaledInstance(-1, 15, Image.SCALE_SMOOTH)));
    } catch (IOException ex) {
    }

    backgroundPane.revalidate();
    backgroundPane.repaint();
}

B: The screenshot above is the minimum size of the window. When I resize the window horizontally, the inner pane only resize to the value of min + (frame.width - min) / 2 meaning my right scrollbar does not stick to the right side of the frame.

See below a demonstration of the both problems: result

Was it helpful?

Solution 3

According to camickr answer and comments, see how I solved it:


Point A is due to my free layout used in NetBeans. I did not succeed to fix my code so I changed the structure of my elements. It is probably not optimal and does not use all the swing concepts right, but it works the way I want.

I have a JLayeredPane in the background that uses a GridBagLayout. This background pane contains one column of JPanel of height 30 and 260, one for the summary line, the other one for the details.

enter image description here

The expand/collapse function controlled by the JToggleButton works by hiding the below panel belowPanel.setVisibility(false). No need for repack or anything, just that. Here is how the code looks like without changing the button's icon:

private void inverseVisibility(JToggleButton expand, JPanel target) {
    if (expand.isSelected()) {
        target.setVisible(true);
    } else {
        target.setVisible(false);
    }
}

As I only wanted the elements to resize horizontally, all my panels have Horizontal as Fill value and Northwest as Anchor. I've set the weightX = 1; weightY = 0. Finally I added a panel in the bottom with a Southwest anchor and fill both along with both weights to 1 (not sure it changes anything but this way I am certain that it will fill all the blank space at the bottom it the window is resized at a bigger size than its content).


Point B has been solved by taking my background panel, that fit in my Frame, and putting it into a JScrollPane. The error I had was due to the Netbeans editor that did not properly stick the scroll pane to the side of the frame, due to incoherences in the sizes defined in both the frame and the scroll pane. My advise to you if you are using this tool is to set the fewest values as possible as a lot of values are heavily interconnected by the gui designer.

Get the full code (95,864 bytes)

OTHER TIPS

Well, you could add a listener to the frame so have an action on event when the frame is being resized. And then pack() the frame.

public final class TestFrame extends JFrame {

(...)

    this.getRootPane().addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent e) {

             this.pack();
             this.revalidate();

        }
    });

}

It you are using the paint graphic method, you should as well repaint() your frame.

In that method you can also manyally set the preferred size of the window by computing it based on e.getWidth()

How does your expand/collapse code work? Do you just make component visible/invisible, or do you add remove components from the panel?

On the other hand, collapse does hide the text but leaves all the elements in position.

If you add/remove components then the basic code is:

panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();

meaning my right scrollbar does not stick to the right side of the frame.

It depends on the layout manager you are using. I would guess the easiest would be to use a GrigBagLayout. It allows you to "fill" the space available. Read the section from the Swing tutorial on How to Use GridBagLayout for more information and examples.

All the elements are contained in a JLayeredPane.

Not sure why you are using a layered pane. By default a layered pane doesn't use a layout manager.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top