Java Swing: Vertical Layout with fixed width and variable height

Michael Dietrich picture Michael Dietrich · Nov 20, 2012 · Viewed 13k times · Source

I'm looking for a layout manager (or set of layout managers) that help me to arrange my components vertically. All components (buttons) should have the same width, bordered by the underlaying dialog client area width. Each of these buttons is able to expand its height depending on the connected JLabel (the labels will be displayed once the user clicked on the related button) - in short: all the components should be stretched to the same fixed width, but with a variable height depending on their content. The whole dialog content should be placed within a JScrollPane to support a vertical scrollbar if needed.

I'm at the point, where I stacked a lot of different layout managers (mostly borderlayout for horizontal stretching and boxlayout for vertical arrangement) to implement my aimed behaviour. But it's neither satisfying code nor does it work completely as I want.

I spent a lot of time surfing the internet for this problem and discovered that vertical layouts are a common problem in Java. A lot of people replied that GridBagLayout would be the best layout doing this job, but I didn't get it to work the way I wanted.

I hope that is enough information to give me some help for that problem.

Best regards Michael

edit: -not needed anymore-

edit 2:

Here is an image of my current attempt: http://www.pic-upload.de/view-16978954/example-image.png.html

This is almost the result i want, but has strange beviour on resizing.

edit 3:

I think my main problem is the JScrollPane combined with a html containing JLabel, I need something like setMaximumSize(...), but it doesn't work as it should: http://www.pic-upload.de/view-16988005/example-image3.png.html

I need a fixed width, but i cannot find a way to set it.

setPreferredSize(...) works, but I don't know the height of the JLabel, because it contains html text.

edit 4:

my Button:

public class Expander extends JPanel implements ActionListener {

    private static class HtmlViewer extends JLabel {

        private static final long serialVersionUID = 8787130155299083869L;


        public HtmlViewer(String doc) {
            super(doc);
            this.setBackground(Color.WHITE);
            this.setOpaque(true);
        }
    }


    private static final long serialVersionUID = 4480221797736558685L;


    JToggleButton button;
    JComponent component;


    public Expander(String text, String doc) {
        this(text, new HtmlViewer(doc));
    }

    public Expander(String text, JComponent expandableComponent) {
        this.setLayout(new BorderLayout());

        button = new JToggleButton(text) {
            private static final long serialVersionUID = -3330376265192275758L;

            @Override
            public void paint(Graphics g) {
                super.paint(g);

                if (this.isSelected()) {
                    g.drawString("▼", this.getWidth() - 20, 15);
                } else {
                    g.drawString("■", this.getWidth() - 20, 15);
                }
            }
        };

        button.setFocusPainted(false);
        button.addActionListener(this);

        component = expandableComponent;
        component.setBorder(new EmptyBorder(5, 5, 5, 5));

        this.add(button, BorderLayout.PAGE_START);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (button.isSelected()) {
            this.add(component);
        } else {
            this.remove(component);
        }
        this.getTopLevelAncestor().validate();
        this.getTopLevelAncestor().repaint();
        this.setMaximumSize(this.getPreferredSize());
    }
}

the frame:

public class grid2 {

    public static void main(String[] args) {

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel();
        JScrollPane scroll = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        addStuff(p);

        frame.add(scroll);

        frame.pack();
        frame.setVisible(true);
    }

    public static void addStuff(Container container) {
        container.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.weighty = 0;

        Expander e = new Expander("test", "<html>fgb fdh fgfhg ifghiufdshfidsghfiufdsghiudsfhdsfiu dshiufhds if dhf idsufhdsiufhiufhiuds hfdshfiudshfuidsifudshfiudshf  ufdhfiushdfiudshiufhdsiufhdsiuf udshfiudshfiudshfudshf iuhfiudshfiudshfiudshf</html>"); // long html text example
        e.setMaximumSize(new Dimension(500, 10000));
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        container.add(e, c);

        JButton button2 = new JButton("Button 2");
        c.gridy = 1;
        c.gridwidth = GridBagConstraints.REMAINDER;
        container.add(button2, c);

        c.gridy = 2;
        c.weighty = 1;
        c.gridwidth = GridBagConstraints.REMAINDER;
        container.add(new JLabel(), c);
    }
}

Answer

Sri Harsha Chilakapati picture Sri Harsha Chilakapati · Nov 20, 2012

See http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html

setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

Example