5

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;
}
2
  • 1
    What are Setting.length and width? Commented Dec 19, 2012 at 6:04
  • @irrelephant setting.length returns number 8 Commented Dec 19, 2012 at 6:11

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

where is the comments? i cann't see if you are writing
I'd like to suggest that you should pass settings via constructor parameter of Game class.
please see the code from here , it is just on Game class , and i called it from choice class, it is very simple mediafire.com/download.php?96cj26hlxb7nxl4
i make this : instead on calling my game class i called your test class and it works :) thank you very much
@tottiroma - I've removed/flagged nonconstructive comments.
|
2

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() {
        }
    }
}

7 Comments

i did what you said but still have nothing :( :(
Then your problem is else where, you need to share more code. You need to provide a SSCCE which we can run that demonstrates the particular problem you are facing.
the code i gave to u is all my code , really it is just a jFrame , and i tried to print something on the loop ,it does so the loop is working
Well, now, you have two people who can produce output, but without a working example of your problem, we are just guessing at what is wrong
AVD got there first, only fair
|
1

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

1 Comment

this is jFrame, i tried like you said (see the edited code) and still have nothing
1
Try to use setBounds(x,y,width,heigth) method

setBound method

for (int i = 0; i < 1; i++) {
                for (int j = 0; j <1; j++) {
                    JButton b = new JButton("button");
                    b.setBounds(500, 500, 100, 20);               
                    this.add(b);

               }
            }


this.repaint();
this.validate()

1 Comment

i added what you said after panel.add(b); but still have nothing ;(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.