Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:41:41+00:00 2026-05-27T17:41:41+00:00

I have this class which creates a grid: class GridPane extends JPanel{ public GridPane(int

  • 0

I have this class which creates a grid:

 class GridPane extends JPanel{ 

    public GridPane(int row,int col){

      setLayout(new GridLayout(row,col));

      setBorder(BorderFactory.createEmptyBorder(1,1,1,1));

      for (int i =1; i<=(row*col); i++)
      {
         JPanel pan = new JPanel();
         pan.setBackground(Color.RED);
         pan.setPreferredSize(new Dimension(3,3));
         pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
         add(pan);
       } 
} 

And this which sets up the empty main frame with the group layout:

public MainFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    GroupLayout gl_contentPane = new GroupLayout(contentPane);
    gl_contentPane.setHorizontalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGap(0, 440, Short.MAX_VALUE)
    );
    gl_contentPane.setVerticalGroup(
        gl_contentPane.createParallelGroup(Alignment.LEADING)
            .addGap(0, 268, Short.MAX_VALUE)
    );
    contentPane.setLayout(gl_contentPane);
}

I am not very experienced with SWING and I have been trying to add a GridPane object to the Right of the Main Frame for some time. It keeps telling me something like :

java.lang.IllegalStateException: GridPane[,0,0,0×0,invalid,layout=java.awt.GridLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.EmptyBorder@6ba7508a,flags=9,maximumSize=,minimumSize=,preferredSize=] is not attached to a vertical/horizontal group

Does anyone know what is going on? What should I do to accomplish what I need? Sorry for posting lots of code. Thanks for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:41:42+00:00Added an answer on May 27, 2026 at 5:41 pm

    I would suggest that in this case you make use of BorderLayout.

    contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    
    contentPane.add(pan, BorderLayout.LINE_END);
    

    Here is the full code. I hope this is what you are looking for.

    public class Main {
    
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setLayout(new BorderLayout());
            GridPane gp = new GridPane(5, 5);
    
            frame.add(gp, BorderLayout.EAST);
    
            frame.setVisible(true);
            frame.setSize(250, 250);
        }
    }
    
    class GridPane extends JPanel {
    
        public GridPane(int row, int col) {
    
            setLayout(new GridLayout(row, col));
            setPreferredSize(new Dimension(125, 125));
            setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    
            for (int i = 1; i <= (row * col); i++) {
                JPanel pan = new JPanel();
                pan.setBackground(Color.RED);
                pan.setPreferredSize(new Dimension(3, 3));
                pan.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                add(pan);
            }
        }
    }
    

    Useful links:

    • A Visual Guide to Layout Managers
    • How to Use BorderLayout
    • How to Use GroupLayout
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which looks something like this: public class Test { private
I have a class which is marked with a custom attribute, like this: public
I have a class MinMax which among other things contains this: public class MinMax
I have this script which basically toggles a bgColor class on and off so
I have this code which compiles and works as expected: class Right {}; class
I have a Customer class which has a representative field....this field is initially blank
I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have this sample text, which is retrieved from the class name on an
I may be going about this backwards... I have a class which is like
I have an application App1 which defines class A and uses instances of this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.