JScrollPane for JPanel inside JPanel

After reading this answer , I came to using getPreferredSize instead of setPreferredSize . But I still can’t use it @Override getPreferredSize, but this is not the main problem that I am currently facing.

I have an application CardLayoutthat calls a class called HiraganaData strong>

HiraganaData is a class that extends JPanel, so it can be used CardLayout, but it also has 2 more JPanelon it, one for the back button and one for the other buttons, before using this idea, I used JTable, but ran into problems when creating cells as buttons, so I abandoned this idea and came up with this new one using GridLayout. Some of the buttons will be disabled, one way or another I can do this and will not include this code, since it is not related.

So my actual question or problem:

  • As I can add JScrollPane only to buttonsPanel, I did my best trying to add it even to the entire "global" area without success.

This is the very aproximate GUI that I can do with the same code in my class. I just added a JFrame to it.

Not sure if this is appropriate, but I use CardLayoutwith different sizes, in the form in which @MadProgrammer suggested this answer .

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.DefaultCellEditor;
import java.awt.Dimension;

public class HiraganaPage extends JPanel {
    JFrame frame = new JFrame("Hello");
    JButton kanas[][] = new JButton[26][5];
    JButton backButton = new JButton("back");
    JPanel backPanel = new JPanel();
    JPanel buttonsPanel = new JPanel();

    public static void main(String args[]) {
        new HiraganaPage();
    }

    public HiraganaPage() {
        JPanel pane = new JPanel();
        backPanel.add(backButton);
        buttonsPanel.setLayout(new GridLayout(0, 5));
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        pane.add(backPanel);

        //pane.setPreferredSize(new Dimension(500, 500));

        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 5; j++) {
                kanas[i][j] = new JButton("1");
                buttonsPanel.add(kanas[i][j]);
            }
        }

        JScrollPane scroll = new JScrollPane(buttonsPanel);
        pane.add(buttonsPanel);
        this.add(pane, BorderLayout.CENTER);
        frame.add(this);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(50, 50);
        frame.setSize(300, 300);
    }
}

This is how it looks in my full application

enter image description here

And this is how it looks in MCVE.

enter image description here

+4
source share
1 answer

The main problem you are facing is that you remove buttonsPanefrom the scrollbar when you add it to pane...

JScrollPane scroll = new JScrollPane(buttonsPanel);
pane.add(buttonsPanel);
this.add(pane, BorderLayout.CENTER);

And the fact that you never add a scrollbar to anything doesn't help either.

, , buttonsPane pane,

HiraganaPage BorderLayout, buttonsPanel CENTER HiraganaPage, pane NORTH HiraganaPage

Buttons

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPane {

    public static void main(String[] args) {
        new TestScrollPane();
    }

    public TestScrollPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new HiraganaPage());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class HiraganaPage extends JPanel {

        JFrame frame = new JFrame("Hello");
        JButton kanas[][] = new JButton[26][5];
        JButton backButton = new JButton("back");
        JPanel backPanel = new JPanel();
        JPanel buttonsPanel = new JPanel();

        public HiraganaPage() {
            setLayout(new BorderLayout());

            JPanel pane = new JPanel();
            backPanel.add(backButton);
            buttonsPanel.setLayout(new GridLayout(0, 5));
            pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
            pane.add(backPanel);

//            pane.setPreferredSize(new Dimension(500, 500));

            for (int i = 0; i < 26; i++) {
                for (int j = 0; j < 5; j++) {
                    kanas[i][j] = new JButton("1");
                    buttonsPanel.add(kanas[i][j]);
                }
            }

            add(pane, BorderLayout.NORTH);
            add(new JScrollPane(buttonsPanel));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    }

}
+4

All Articles