• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem putting button in jpanel

 
yashar nesabian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
}

}
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got anything static in there?
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What layout have you given the panel? You appear to be giving the buttons grid layout; why?
 
yashar nesabian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
yashar nesabian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
yashar nesabian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
yashar nesabian
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Marshal
Posts: 52580
119
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start in the Java Tutorials.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!