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

Thread: JPanel GridLayout isn't working

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel GridLayout isn't working

    I am trying to make JPanels that add components vertically instead of horizontally but I can't get it to work. I used new GridLayout(0,1) and it works for my JFrame so I am confused as to why it won't work for the JPanel as well. My code is posted below

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class GUIClass{
      GUIClass(){
        JFrame jfrm = new JFrame("Testing");
        jfrm.setLayout(new GridLayout(0,1));
        jfrm.setSize(500,500);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
        Ballot[] ballotArray = new Ballot[3];
        for(int i=0;i<2;i++){
          ballotArray[i] = new Ballot("Button "+i);
          jfrm.add(ballotArray[i]);
        }
     
        jfrm.setVisible(true);
      }
    }

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class Ballot extends JPanel{
     
      Ballot(String name){
        new JPanel(new GridLayout(0,1));
        JButton jbtn = new JButton(name);
        JLabel jlbl = new JLabel();
        jlbl.setText(name + 1);
        add(jbtn);
        add(jlbl);
      }
     
    }

    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    class Project4 extends JFrame{
      public static void main(String []args) throws IOException{
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new GUIClass();
          }
        });
      }
    }

    Any help is appreciated.
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: JPanel GridLayout isn't working

    won't work for the JPanel
    Please explain what happens. What does "won't work" mean?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel GridLayout isn't working

    Technically it works I don't get any errors but the components in all my JPanels, 1 button and 1 label, are added horizontally instead of vertically. I want the label below the button on each seperate JPanel and it is showing up to the left instead. My gridlayout format looks correct to me so I am confused as to why it isn't following the format I set.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel GridLayout isn't working

    *right not left my mistake their. The button is on the left, the label is on the right.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel GridLayout isn't working

    Nevermind I figured it out.

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. GridLayout help
    By sambar89 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2011, 11:00 PM
  3. Images in GridLayout
    By BuhRock in forum AWT / Java Swing
    Replies: 4
    Last Post: November 5th, 2011, 12:15 AM
  4. [SOLVED] how to get the row and column of a component gridlayout
    By prettynew in forum AWT / Java Swing
    Replies: 0
    Last Post: March 13th, 2011, 06:39 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM