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

And this is how it looks in MCVE.
