Rafail Rafail - 1 month ago 17
Java Question

2 JPanels with scrolls

So I have this panel positioned at the top (NORTH) side of my frame called menupanel. Inside this panel I have two other panels - topPanel and botPanel (red and black). Inside both of those panels there are several buttons(in this example labels). I would like to add scrolls (horizontal) on the two panels (bot and top).

// creating the frame
JFrame frame;
frame = new JFrame("Test");
frame.setVisible(true);
frame.setLayout(new BorderLayout());
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// creating labels

JLabel label1 = new JLabel("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT");
JLabel label2 = new JLabel("TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2");
JLabel label3 = new JLabel("TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3");

JLabel label4 = new JLabel("TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT");
JLabel label5 = new JLabel("TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2 TEXT2");
JLabel label6 = new JLabel("TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3 TEXT3");

// creating the main and menu panel

JPanel menupanel,topPanel,botPanel,mainpanel;
mainpanel = new JPanel();
menupanel = new JPanel(new BorderLayout());
topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
botPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));

topPanel.setPreferredSize(new Dimension(300, 100));
botPanel.setPreferredSize(new Dimension(300, 100));

topPanel.add(label1);
topPanel.add(label2);
topPanel.add(label3);

botPanel.add(label4);
botPanel.add(label5);
botPanel.add(label6);

topPanel.setBackground(Color.RED);
botPanel.setBackground(Color.BLACK);
mainpanel.setBackground(Color.BLUE);


JScrollPane scrollT,scrollB;
scrollT = new JScrollPane(topPanel);
scrollT.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollB = new JScrollPane(botPanel);
scrollB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
menupanel.add(scrollT);
menupanel.add(scrollB);

menupanel.add( topPanel, BorderLayout.NORTH );
menupanel.add( botPanel, BorderLayout.SOUTH );


frame.add(mainpanel, BorderLayout.CENTER);
frame.add(menupanel, BorderLayout.NORTH);
frame.pack();


You can see an image here

Answer

You're adding components to more than one container:

scrollT = new JScrollPane(topPanel);  // topPanel added to scroll pane
scrollT.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollB = new JScrollPane(botPanel);  // botPanel added to scroll pane
scrollB.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
menupanel.add(scrollT); // scrollpanes added to menupanel -- without Borderlayout constants?
menupanel.add(scrollB);

menupanel.add( topPanel, BorderLayout.NORTH );  // topPanel added to menupanel
menupanel.add( botPanel, BorderLayout.SOUTH );

This is not allowed For Swing GUI's.

Instead add the topPanel and botPanel to the JScrollPanes and the JScrollPanes to the rest of the GUI, but don't re-add topPanel and botPanel to anything.

Another problem -- you're constraining topPanel and botPanel's sizes, and that will make them not scroll -- avoid doing this. If you need to constrain something's size, do the scrollpane's viewport or scrollpane itself.

e.g.,

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.*;

public class ScrollPaneFun2 extends JPanel {

    private static final int PREF_W = 500;

    public ScrollPaneFun2() {
        JPanel topPanel = new JPanel(new GridLayout(0, 1));
        JPanel botPanel = new JPanel(new GridLayout(0, 1));

        for (int i = 0; i < 3; i++) {
            JPanel innerTopPanel = new JPanel(new GridLayout(1, 0, 3, 0));
            JPanel innerBotPanel = new JPanel(new GridLayout(1, 0, 3, 0));
            for (int j = 0; j < 50; j++) {
                innerTopPanel.add(new JLabel("Text " + (i + 1)));
                innerBotPanel.add(new JLabel("Text " + (i + 1)));
            }
            topPanel.add(innerTopPanel);
            botPanel.add(innerBotPanel);
        }

        JScrollPane topScrollPane = new JScrollPane(topPanel);
        JScrollPane botScrollPane = new JScrollPane(botPanel);
        topScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        botScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        setLayout(new GridLayout(0, 1));
        add(topScrollPane);
        add(botScrollPane);
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        int height = super.getPreferredSize().height;
        return new Dimension(PREF_W, height);
    }

    private static void createAndShowGui() {
        JFrame frame = new JFrame("ScrollPaneFun2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new ScrollPaneFun2());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
Comments