Vikram Gaur Vikram Gaur - 2 months ago 9
Java Question

Displaying many buttons in Swing

I am trying to create a java swing panel with variable number of buttons on it(1-10000 buttons).
However, it seems there is a limitation to the number of buttons I can show in the form and the buttons seem to repeat after a while.
I have looked into the tutorials available on Java Swing. However, it does not extend to the number of buttons I am expected to deal with.

This is the code I have written so far.

public JPanel createBlockGroup() {
JPanel blockPanel = new JPanel();
Dimension buttonDimension = new Dimension(40,70);
GroupBlockJNI group = new GroupBlockJNI();
System.out.println(group.getTotalBlockGroups());
blockPanel.setMaximumSize(new Dimension(group.getTotalBlockGroups()*50, 100));
blockPanel.setSize(group.getTotalBlockGroups()*50, 100);

for(int i=0; i<group.getTotalBlockGroups(); i++) { // getTotalBlockGroups() returns 6400
final int j=i;
JButton partition = new JButton("Block Group");
partition.setPreferredSize(buttonDimension);
partition.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2) {
updateLevel(gCurrentLevel+1);
gButtonZoomOut.setEnabled(true);
} else if(e.getClickCount() == 1) {
updateText("Block Group "+String.valueOf(j));
}
}
});
blockPanel.add(partition);
}

return blockPanel;
}


When running this code, I am able to display only 574 buttons.
This is the snapshot.

Any help is appreciated.

Answer

I am trying to create a java swing panel with variable number of buttons on it(1-10000 buttons). However, it seems there is a limitation to the number of buttons I can show in the form and the buttons seem to repeat after a while.

This is a sample I have coded to test whether it can handle 10,000 buttons. Everything is working fine on my side. It is still working fine even for 1000,000 buttons.

enter image description here


A test panel holding the scroll pane:

class MainPanel extends JPanel{

    private JScrollPane scrollPane;
    private JPanel scrollPanel;
    private JButton btnAddPage;
    private static int idx = 0;

    public MainPanel(){
        setPreferredSize(new Dimension(400, 140));
        setLayout(new BorderLayout());
        initComponents();
    }       
    private void initComponents(){
        scrollPanel = new JPanel();
        scrollPanel.setSize(new Dimension(300, 300));       
        scrollPane = new JScrollPane(scrollPanel);  //Let all scrollPanel has scroll bars
        btnAddPage = new JButton("Add New Page");
        btnAddPage.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                //for(int x=0; x<10000; x++) //uncomment to test with 10,000 buttons
                addButton(new JButton("Page " + (idx++)));
                scrollPanel.revalidate();
            }
        });             
        add(scrollPane, BorderLayout.CENTER);
        add(btnAddPage, BorderLayout.SOUTH);                
    }

    public void addButton(JButton btn){
        scrollPanel.add(btn);
    }
}

Test Runner class to run the codes:

class TestRunner{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                JFrame frame = new JFrame("Scrollable Panel");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);                 
            }   
        });     
    }
}

I have posted the full codes, I think it will be useful to you.

Comments