1

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.

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 22 '16 at 20:32
  • 2
    It "looks" like you're mixing heavy and lightweight components, which is never a good idea. Also, don't mess with the preferred/min/max size of the components, let the layout managers do there jobs – MadProgrammer Mar 22 '16 at 20:51
  • 1
    @Vikram Gaur I tested with my own codes. It is able to handle 100,000 buttons. Further more, I added them at runtime. Everything is showing up smoothly. Perhaps you want to check on your implementation.. – user3437460 Mar 22 '16 at 21:07
  • @MadProgrammer : Could you explain a bit more about the lightweight and heavyweight components or if you could point me to a resource. Thanks. – Vikram Gaur Mar 26 '16 at 02:11
  • 1
    Awt (heavy) based components have their own native peer and concept of z-ordering, Swing (light) components share the native peer with the window they are added to and can be ordered by depth. Mixing heavy weight components (java.awt.Button) in light weight containers (javax.swing.JPanel) is unadvisable as it can generate painting issues – MadProgrammer Mar 26 '16 at 02:19
  • That is probably my problem. I am adding the generated output to ScrollPane instead of JScrollPane. I'll change that. Thanks for the help. – Vikram Gaur Mar 26 '16 at 02:26

1 Answers1

4

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.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • 2
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) and start learning to use `Scrollable` instead – MadProgrammer Mar 22 '16 at 21:59
  • 1
    you forgot to change scroll increment for natural scrolling, but upvoted – mKorbel Mar 23 '16 at 19:23
  • Sorry for the delayed reply. I did not recieve mail updates :(. Anyways, I tried running the same code after uncommenting the line. It is not able to add 10000 buttons in my system. Seems the problem lies with my system then. I am running Linux 64-bit, Ubuntu 15.04. I-3 processor – Vikram Gaur Mar 25 '16 at 20:53
  • @VikramGaur When you add the 10,000 buttons, it may load for a few seconds (or longer). If you cannot see any buttons after adding, then that is a different story.. – user3437460 Mar 25 '16 at 22:26
  • 1
    yeah took me a few seconds to get through. And it is showing properly. Probably some other problem or as @MadProgrammer mentioned problem with mixing heavyweight and lightweight components. Trying to rewrite the whole thing now. Hope it works :) Update: Solved the problem. It was due to the clash of JScrollPane and ScrollPane. There is some problem with ScrollPane which doesn't let it draw beyond a certain range. – Vikram Gaur Mar 26 '16 at 02:55