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 2 of 2

Thread: button positioning

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default button positioning

    hi im trying to create a frame with 3 buttons down the left hand side of the frame and a text area on the right hand side.
    I have been trying to use the GridBagLayout but i cant seem to position anything no matter where i say to put the buttons or what size i want the buttons to be when i run the program all i get is 1 button showing and if i resize the frame the button resizes with it.
    is the GridBagLayout able to do what i want and if so how do i go about positioning the buttons.
    if not could you tell me what layout is best suited.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: button positioning

    GridBagLayout has the ability to do what you need, but there are far easier Layout Managers available to do your task.

    I recommend using GridLayout for this task, as it is very easy and useful.

    The ways you could do this are:
    • Use a JFrame's default LayoutManager (BorderLayout) to add one JPanel to BorderLayout.WEST and add another JPanel to BorderLayout.EAST where each JPanel uses a GridLayout of 3x1 (Row x Column).
    • Another way would be to change the JFrame's LayoutManager to GridLayout of 1x2 and then add two JPanels like the above.
    • Or change the JFrame's LayoutManger to 3x2 and add all components to the JFrame.

    Which ever approach you take (from my suggestion), you will be using GridLayout, so follow the link I provided for a guide.

    Heres an example of what I mean.
        public JPanel jButtonPanel(){
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(3,1));
     
        buttonPanel.add(myButton);
        buttonPanel.add(myButton2);
        buttonPanel.add(myButton3);
     
        return buttonPanel;
        }

    You would then add jButtonPanel() to your JFrame, and do the same for jTextFieldPanel().
    Hope this helps.
    Last edited by newbie; January 29th, 2011 at 09:58 AM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    pds8475 (January 29th, 2011)

Similar Threads

  1. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 02:24 PM
  2. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Please help with Actionlistener-Button
    By ashleykathy in forum AWT / Java Swing
    Replies: 1
    Last Post: March 4th, 2010, 08:21 PM
  5. Content positioning on the screen
    By Drakenmul in forum AWT / Java Swing
    Replies: 1
    Last Post: July 27th, 2009, 09:02 AM