Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java add buttons dynamically as an array [duplicate]

Possible Duplicate:
java - How would I dynamically add swing component to gui on click?

I want to add array of buttons dynamically. I tried like this:

this.pack();
    Panel panel = new Panel();
    panel.setLayout(new FlowLayout());
    this.add(panel);
    panel.setVisible(true);
    for (int i = 0; i < Setting.length; i++) {
        for (int j = 0; j < Setting.width; j++) {
            JButton b = new JButton(i+"");
            b.setSize(30, 30);
            b.setLocation(i * 30, j * 30);
            panel.add(b);
            b.setVisible(true);
        }
    }

but didn't get anything , what mistake did I make?

Edit

I have jFrame class "choices" on it i have a button , when I press the button, this is supposed to happen:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
       structure.Setting s = new Setting(8, 8, 3, 1, 1);
       Game g = new Game();
       g.setSetting(s);
       this.dispose();
       g.show();
    }

then i go to the Game class (also jFrame class) to the function setSetting and it is like this:

void setSetting(Setting s) {
        this.setting = s;
        structure.Game game = new structure.Game(setting);
        JPanel panel = new JPanel(new GridLayout(5, 5, 4, 4));
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                JButton b = new JButton(String.valueOf(i));
                panel.add(b);
            }
        }
        add(panel);
        pack();
        setVisible(true);
    }
    structure.Setting setting;
}
like image 461
William Kinaan Avatar asked Dec 19 '12 06:12

William Kinaan


3 Answers

You may use GridLayout to add equal height/width buttons:

enter image description here

public class Game extends JFrame {
    private JPanel panel;
    public Game(int rows,int cols,int hgap,int vgap){
        panel=new JPanel(new GridLayout(rows, cols, hgap, vgap));
        for(int i=1;i<=rows;i++)
        {
            for(int j=1;j<=cols;j++)
            {
                JButton btn=new JButton(String.valueOf(i));
                panel.add(btn);
            }
        }
        add(panel);
        pack();
        setVisible(true);
    }
}

and code in button's handler should be:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   Game g = new Game(5,5,3,3);
}

Note that you can also pass Setting object reference via Game constructor (when you may add widgets dynamically) instead of calling setSetting method.

like image 113
KV Prajapati Avatar answered Nov 04 '22 09:11

KV Prajapati


The JPanel is already under the control of a layout manager, setting the size and position of the buttons is irrelevant, as they will changed once the panel is validated.

Try, instead, adding the panel AFTER you've populated it with buttons..

UPDATED with Example

Without further evidence, we are only guessing...You now have two people who have no issues.

enter image description here

Panel panel = new Panel();
for (int i = 0; i < Setting.length; i++) {
    for (int j = 0; j < Setting.width; j++) {
        jButton b = new jButton(i + "");
        panel.add(b);
    }
}
this.add(panel);

public class BadBoy {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());

                int buttonCount = (int) Math.round(Math.random() * 20);
                int columnCount = (int) Math.round(Math.random() * 20);
                JPanel buttonPane = new JPanel(new GridLayout(0, columnCount));
                for (int i = 0; i < buttonCount; i++) {
                    JButton b = new JButton(i + "");
                    buttonPane.add(b);
                }

                frame.add(buttonPane);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ButtonPane extends JPanel {

        public ButtonPane() {
        }
    }
}
like image 30
MadProgrammer Avatar answered Nov 04 '22 08:11

MadProgrammer


I am guessing that this extens or is some kind of frame?

First step is to pack the frame by doing this.pack(); sets the frame size ti just fit all objects.

I assume you have set it to visible?

Now you should be able to see the buttons. If you want a different layout use panel.setLayout(new SomeLayout);

like image 1
Martin Larsson Avatar answered Nov 04 '22 09:11

Martin Larsson