Forums Register Login

problem putting button in jpanel

+Pie Number of slices to send: Send
hi
i`m trying to add buttons in a loop to my but when i compile my program i can`t see my buttons
i can see other stuff that added to jpanel

static JButton blocks[][];
for (int i = 0; i < block.length; i++) {

for (int j = 0; j < block[i].length; j++) {
block[i][j] = new Block(i, j);
blocks[i][j] = new JButton(i+"");
blocks[i][j].setLayout(new GridLayout((i+1) * 10, (i+1) * 10, 10, 10));
pageAxisPanel.add(blocks[i][j]);
}

}
+Pie Number of slices to send: Send
Why have you got anything static in there?
+Pie Number of slices to send: Send
What layout have you given the panel? You appear to be giving the buttons grid layout; why?
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:Why have you got anything static in there?



because i call a method from main() then i call this method from that method
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:What layout have you given the panel? You appear to be giving the buttons grid layout; why?



my panel layout is null and yes my buttons layout is GridLayout i did that in order to be able to choose my button position
1
+Pie Number of slices to send: Send
 

yashar nesabian wrote:

Campbell Ritchie wrote:What layout have you given the panel? You appear to be giving the buttons grid layout; why?


my panel layout is null and yes my buttons layout is GridLayout i did that in order to be able to choose my button position


Unfortunately you are doing it wrong.

To start with some terminology to help clear things up
Your panel is the parent
Your buttons are the children
You add children to the parent

1) Never set a null layout. Always use a properly LayoutManager
2) The layout is set to parent.
3) You add children to the parent and then the parent uses the layout manager to figure out where and how to place the children.

e.g.
new JPanel(new FlowLayout(FlowLayout.LEFT)).add(new JButton("OK"));
Here the JPanel has an overriden constructor to accept the LayoutManager which in this case is a FlowLayout instance with LEFT alignment.
When you run this, the panel(parent) will display the child(ok button) which is aligned to the left of the panel.

More on layout managers here http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
+Pie Number of slices to send: Send
 

Maneesh Godbole wrote:

yashar nesabian wrote:

Campbell Ritchie wrote:What layout have you given the panel? You appear to be giving the buttons grid layout; why?


my panel layout is null and yes my buttons layout is GridLayout i did that in order to be able to choose my button position


Unfortunately you are doing it wrong.

To start with some terminology to help clear things up
Your panel is the parent
Your buttons are the children
You add children to the parent

1) Never set a null layout. Always use a properly LayoutManager
2) The layout is set to parent.
3) You add children to the parent and then the parent uses the layout manager to figure out where and how to place the children.

e.g.
new JPanel(new FlowLayout(FlowLayout.LEFT)).add(new JButton("OK"));
Here the JPanel has an overriden constructor to accept the LayoutManager which in this case is a FlowLayout instance with LEFT alignment.
When you run this, the panel(parent) will display the child(ok button) which is aligned to the left of the panel.

More on layout managers here http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html



thanks for your help but what should i do if i want to choose where to put my buttons?
1
+Pie Number of slices to send: Send
 

yashar nesabian wrote:
thanks for your help but what should i do if i want to choose where to put my buttons?


Set the layout to the panel (parent) and then add the buttons (children) to it.
+Pie Number of slices to send: Send
 

Maneesh Godbole wrote:

yashar nesabian wrote:
thanks for your help but what should i do if i want to choose where to put my buttons?


Set the layout to the panel (parent) and then add the buttons (children) to it.



can you show e an example? for example my loop
thanks
1
+Pie Number of slices to send: Send
Start in the Java Tutorials.
Do not set lab on fire. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 632 times.
Similar Threads
Tiled Based Map Game in Java
help me create Go! game please
JAVA Compilation problem. Problem with importing code using the Get Method
Fill a GridLayout vertically instead of horizontally
How to know which button being clicked?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:51:01.