Results 1 to 9 of 9
Like Tree2Likes
  • 2 Post By Norm

Thread: Odd JButton behavior

  1. #1
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default 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.
    Java Code:
    public Gui()
        {
            win1 = new JFrame("Player 1");
            win1.setSize(700, 500);
            
            win1Canvas = new GCanvas();
            win1.add(win1Canvas);
         
            createButtons();
            win1.setVisible(true);
        }
    createButtons() is as follows:

    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 + "");
            }
        }
    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.

    Anything obviously wrong? Resizing the window does not bring the buttons out either. They seem to be MIA. Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default 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?

  3. #3
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Odd JButton behavior

    despite the design of a 15x8 grid.
    How does that design get implemented in the positioning of your buttons in the GUI?

    You definitely need to look at how to use a layout manager.
    Go to this site and Find Layout : The Really Big Index

  5. #5
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Re: Odd JButton behavior

    Quote Originally Posted by Norm View Post
    How does that design get implemented in the positioning of your buttons in the GUI?
    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default 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.

  7. #7
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Re: Odd JButton behavior

    Quote Originally Posted by Norm View Post
    Did you read up on layout managers?
    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.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,878
    Rep Power
    25

    Default Re: Odd JButton behavior

    with the lack of any "layout manager"
    Many containers have default layout managers that might not do what you want when you add components to the container.
    If you don't want the current layout manager, use the container's set layout method to change it.
    DarrylBurke and Fubarable like this.

  9. #9
    SiX
    SiX is offline Member
    Join Date
    Jul 2011
    Posts
    24
    Rep Power
    0

    Default Re: Odd JButton behavior

    Ok, thank you for the insight! I shall try changing it.

Similar Threads

  1. Strange Arrays behavior
    By amro in forum New To Java
    Replies: 10
    Last Post: 11-03-2010, 08:08 AM
  2. different behavior between debug and run
    By flaquitqm in forum Eclipse
    Replies: 1
    Last Post: 03-04-2010, 04:01 PM
  3. different exceptions and behavior of try-catch
    By arefeh in forum New To Java
    Replies: 10
    Last Post: 02-06-2010, 11:13 PM
  4. Java Crossplatform Behavior
    By wdavis in forum New To Java
    Replies: 2
    Last Post: 11-05-2009, 02:22 PM
  5. strange refreshing behavior
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 12-27-2008, 05:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •