Results 1 to 9 of 9
Thread: Odd JButton behavior
- 09-10-2011, 08:34 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Odd JButton behavior
I am currently creating a simple grid of JButtons using a 2 dimensional array and adding them to a JFrame.
The constructor creates the JFrame as follows.
createButtons() is as follows:Java Code:public Gui() { win1 = new JFrame("Player 1"); win1.setSize(700, 500); win1Canvas = new GCanvas(); win1.add(win1Canvas); createButtons(); win1.setVisible(true); }
But for some reason I only get 3 buttons on the screen. Interestingly enough, it's the buttons [0][0], [1][0], and [0][1]. They appear in the right spot, but nothing else shows up. It is a 15x8 grid, so I know I am missing something here. But the better of two hours has gotten to me.Java Code:public void createButtons() { int x = 0; int y = 0; int xGrid = 20; int yGrid = 20; p1 = new JButton[15][8]; //p2 = new JButton[15][8]; for(int ticker=0; ticker<120; ticker++) { p1[x][y] = new JButton(); p1[x][y].setSize(10,10); p1[x][y].addActionListener(this); p1[x][y].setActionCommand(x+" "+y); if(ticker == 0) { p1[x][y].setLocation(xGrid, yGrid); } else if((ticker%15) == 0) { xGrid = 20; p1[x][y].setLocation(xGrid, (yGrid+10)); } else { p1[x][y].setLocation(xGrid+10, yGrid); } win1Canvas.add(p1[x][y]); p1[x][y].setVisible(true); x = x+1; if(x == 15) { x = 0; y++; xGrid = 20; } System.out.println(ticker + ""); } }
Anything obviously wrong? Resizing the window does not bring the buttons out either. They seem to be MIA. Thanks!
- 09-10-2011, 01:46 PM #2
Re: Odd JButton behavior
What layout manager are you using? Or rather are you fighting with the layout manager over who gets to position the buttons?
- 09-10-2011, 04:27 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Odd JButton behavior
I'm actually not familiar with a layout manager at all. I shall google it and attempt to learn. I am just adding JButtons to a GCanvas using a for statement to increment it's location. I did a println to make sure the while loop was working as intended, and indeed I got my 119 loop like I intended, which means (barring any logic errors) I should get my buttons as intended. But instead I get three of the 120, and only the first two are consecutive; The third is the first box of the second line, despite the design of a 15x8 grid.
- 09-10-2011, 04:31 PM #4
Re: Odd JButton behavior
How does that design get implemented in the positioning of your buttons in the GUI?despite the design of a 15x8 grid.
You definitely need to look at how to use a layout manager.
Go to this site and Find Layout : The Really Big Index
- 09-10-2011, 05:40 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Odd JButton behavior
Within the createButtons() method I simply added the button width to the x location value. So if button [0][0] was at (20, 20), button [1][0] would be at (30,20). When it got to the 16th box, I brought the x value back to 20, and added 10 to the y value.
I don't understand how I could be missing so many boxes. I have less than 3% of the boxes I intended to create. Surely I have done something wrong, but I don't know where.
- 09-10-2011, 05:42 PM #6
Re: Odd JButton behavior
Did you read up on layout managers?
Many containers have default layout managers that might not do what you want when you add components to the container.
- 09-10-2011, 06:01 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Re: Odd JButton behavior
Reading up on them right now. But I don't understand how, with the lack of any "layout manager", my buttons do not get placed where told. I have set the size of the window, added a canvas to it, and that is it. I have done this before with similar designs with just as simple of a set up with buttons that took up more of the window.
- 09-10-2011, 06:09 PM #8
Re: Odd JButton behavior
Many containers have default layout managers that might not do what you want when you add components to the container.with the lack of any "layout manager"
If you don't want the current layout manager, use the container's set layout method to change it.
- 09-10-2011, 07:44 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Strange Arrays behavior
By amro in forum New To JavaReplies: 10Last Post: 11-03-2010, 07:08 AM -
different behavior between debug and run
By flaquitqm in forum EclipseReplies: 1Last Post: 03-04-2010, 03:01 PM -
different exceptions and behavior of try-catch
By arefeh in forum New To JavaReplies: 10Last Post: 02-06-2010, 10:13 PM -
Java Crossplatform Behavior
By wdavis in forum New To JavaReplies: 2Last Post: 11-05-2009, 01:22 PM -
strange refreshing behavior
By diggitydoggz in forum New To JavaReplies: 4Last Post: 12-27-2008, 04:51 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks