Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JButton Positioning:

  1. #1
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default JButton Positioning:

    import java.awt.GridLayout;
    import javax.swing.*;
     
    public class Screen
    {
        JButton start;
        JButton reset;
        JButton box[][] = new JButton[20][20];
     
        Screen()
        {
            JFrame j = new JFrame();
            j.setSize(700,500);
            j.setLayout(new GridLayout(20,20));
            j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //create buttons
            for(int row=0;row<20;row++)
            {
                for(int col=0;col<20;col++)
                {
                    box[row][col] = new JButton();
                    j.add(box[row][col]);
                }
            }
     
            //create start button
            start = new JButton("Start");
            j.add(start);
            start.setLocation(0, 0);
     
            //create reset button
            reset = new JButton("Reset");
            j.add(reset);
            reset.setLocation(2, 0);
     
            j.setVisible(true);
     
        }
     
        public static void main(String[] args)
        {
            new Screen();
        }
     
    }

    I am trying to place the buttons on the bottom. I tried a few different things but the grid layout keeps grabbing it and making them a part of the grid at the button. Any advice?


  2. #2
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: JButton Positioning:

    Don't have alot of experience with it, but i think you should work with a container,
    so that your frame is your frame, inside your frame you have your jbuttons, and a JPanel where you add your start/reset buttons to,
    I've got it working and i'm a noob ;p

    Java Swing tutorial
    This might get you underway.
    If what I said is horribly wrong, forgive my ignorance
    Last edited by Time4Java; May 20th, 2014 at 11:51 PM.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JButton Positioning:

    Spend some more time looking at layout managers. Adding to Time4Java's advice, you may want to use a BorderLayout in the main container, use a GridLayout in a JPanel to which the box[][] elements are added, and then add that grid JPanel to the center of the BorderLayout. Then add the Start/Reset buttons to a JPanel which is then added to the South part of the BorderLayout.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: JButton Positioning:

    Cross-post: JButton positioning:

Similar Threads

  1. Positioning problem HELP!?
    By R4DiCaL in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 6th, 2012, 10:42 AM
  2. [SOLVED] Positioning/Resizing JList Help
    By KILL3RTACO in forum AWT / Java Swing
    Replies: 2
    Last Post: October 3rd, 2011, 01:00 PM
  3. button positioning
    By pds8475 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 29th, 2011, 09:35 AM
  4. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 02:24 PM
  5. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM