Many buttons

Hello. I have many buttons I wish to place in my JFrame.

They are all going to be the same width and height and will have the same label which brings me to the question of how can I add 81 buttons to my JFrame without having to write out:

JButton btn1 = new JButton("0");

....

JButton btn81 = new JButton("0");

and

pane.add(btn1);

...

pane.add(btn81);

theres also the case of setSize, etc, the list will go on.

Is there an easy way? Or will I have to type this all out!

Thank you

[552 byte] By [emdiessea] at [2007-11-27 4:43:51]
# 1

Well you can do it like this:

JButton btn[]=new JButton[81];

for(int i=0;i<btn.length; i++){

btn[i].setSize(100,25);

btn[i].setText("0");

pane.add(btn[i]);

}

>

pSala at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thank you, however when I compile and run it I get a blank frame.Why is this and how can I resolve it?Thanks again :D
emdiessea at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 3
You'll have to either use null layout manager and call setBounds() on each button (not recommended) or use another layout manager (grid, gridbag, form, ...) to have it position your buttons in some grid.
kirillga at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 4
see my response in [url= http://forum.java.sun.com/thread.jspa?threadID=5133566&messageID=9670350#9670350]this thread[/url].
petes1234a at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 5
ok. I had something called FlowLayout() I suppose this is not adequate?
emdiessea at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 6
I'll bet you want a grid of buttons...so gridlayout sounds like it will work better...
petes1234a at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 7

Yeah. If it helps I made a sudoku generator, i am trying to make it into a solver aswell and use a GUI.

public Sudoku()

{

super("Sudoku");

setSize(220, 260);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

Container pane = getContentPane();

GridLayout flo = new GridLayout(9,10);

pane.setLayout(flo);

JButton btnGen = new JButton("Generate");

JButton[][]btn = new JButton[9][9];

for(int r=0;r<9; r++)

{

for(int c=0;c<9; c++)

{

btn[r][c].setSize(25,25);

btn[r][c].setText("0");

pane.add(btn[r][c]);

}

}

btnGen.setActionCommand("Generate");

btnGen.addActionListener(this);

btnGen.setToolTipText("Click this button to Generate a Solution");

pane.add(btnGen);

setContentPane(pane);

}

I'm not sure on how to use grid layouts so I will have to look further into them, however if it is a simple case of changing flowlayout to gridlayout like I have done above, this also did not work.

Thanks :D

Message was edited by:

emdiesse

emdiessea at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 8

It looks like you have a good start there. A couple of suggestions:

At this spot:

Container pane = getContentPane();

GridLayout flo = new GridLayout(9,9);

pane.setLayout(flo);

1) You don't want to use the content pane here. Better to use a JPanel and add it to the content pane.

2) Is it a 9 x 10 layout? I'd try a pair of numbers that just seems more logical here.

here:

JButton[][]btn = new JButton[9][9];

for(int r=0;r<9; r++)

{

for(int c=0;c<9; c++)

{

btn[r][c].setSize(25,25);

btn[r][c].setText("0");

pane.add(btn[r][c]);

}

}

Although you initialized the button array here, you still have to initialize (call constructor) on each button as it is being created. Otherwise you will get a nullpointer error.

here:

btnGen.setActionCommand("Generate");

btnGen.addActionListener(this);

btnGen.setToolTipText("Click this button to Generate a Solution");

pane.add(btnGen);

getContentPane().add(pane);

Better not to add this last button to the 9x9 button grid. It will mess up the 9x9 grid. Better to add it and the 9x9 button grid into a parent grid.

petes1234a at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...
# 9

One more thing while I'm thinking about it, and this is a minor thing, except if you don't understand it, it will mess up your logic eventually. The code here:

for(int r=0;r<9; r++)

{

for(int c=0;c<9; c++)

{

btn[r][c] = new JButton();

btn[r][c].setSize(25,25);

btn[r][c].setText("0");

pane.add(btn[r][c]);

}

}

probably should be:

for(int c=0;c<9; c++)

{

for(int r=0;r<9; r++)

{

btn[c][r] = new JButton();

btn[c][r].setSize(25,25);

btn[c][r].setText("0");

pane.add(btn[c][r]);

}

}

The rows and column variables should be swapped since this is the way that the buttons are added to the grid. Again, this is no biggie and probably doesn't matter at this spot, but I'll bet it will matter later when you try to figure out which button was pushed.

Good luck!

/Pete

petes1234a at 2007-7-12 9:55:40 > top of Java-index,Desktop,Core GUI APIs...