简体   繁体   中英

JScrollPane doesn't add components

I extend JScrollPane and add several components to it. When I add scroll pane to JFrame no components are displayed. In case when my class extends, for example, JPanel and then I add it to a standalone JScrollPane, everything works fine. I cannot understand this behaviour. Could anyone explain me, why it happens?

Here are both variants (the one that works and the one that doesn't):

This variant doesn't work:

public class MainScrollPanel extends JScrollPane {

    private JPanel verticalPanel;

    public MainScrollPanel() throws IOException, ParseException {
        initGUI();
        readData();
    }

    private void initGUI() {
        verticalPanel = new JPanel();
        verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));

        add(verticalPanel);
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    }

    private void readData() throws IOException, ParseException {
        //read data
        //...
        for(NewData message : messages) {
            verticalPanel.add(new JLabel(message.getMessage()));
        }
    }
}

public class MainGUI extends JFrame {
    private MainScrollPanel mainPanel;

    public MainGUI() throws IOException, ParseException {
        super("Scroll app");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        mainPanel = new MainScrollPanel();
        getContentPane().add(mainPanel);

        setSize(200, 200);
        setVisible(true);
    }

    public static void main(String[] args) throws IOException, ParseException {
        new MainGUI();
    }
}

This one works fine:

public class MainScrollPanel extends JPanel {

    public MainScrollPanel() throws IOException, ParseException {
        initGUI();
        readData();
    }

    private void initGUI() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    }

    private void readData() throws IOException, ParseException {
        //The same as in previous example
    }
}

public class MainGUI extends JFrame {
    private MainScrollPanel mainPanel;

    public MainGUI() throws IOException, ParseException {
        //...
        JScrollPane scrollPane = new JScrollPane(mainPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        getContentPane().add(scrollPane);
        //...
    }
}

You probably don't need or want to extend JScrollPane, but regardless, you almost never add components directly to a JScrollPane but rather to its Viewport. Else you lose the Viewport and its functionality.

This can be done via the JScrollPane method: setViewportView(Component comp) The other way is to pass the component into the JScrollPane's constructor (or here its super constructor) as this will automatically pass the component to the viewport. It's a little syntactic sugar is all.

eg,

private void initGUI() {
    verticalPanel = new JPanel();
    verticalPanel.setLayout(new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));

    setViewportView(verticalPanel); // ********** changed *******

    setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM