Search code examples
javaswinguser-interfacenetbeans-8

Java UI: set swing components behavior on resize


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


Solution

  • 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)