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

  • Home
  • SEARCH
  • 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 6060183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:47:48+00:00 2026-05-23T08:47:48+00:00

I am using Swing and AWT (for the listeners) to make a small program.

  • 0

I am using Swing and AWT (for the listeners) to make a small program. I have a problem concerning getting the size of my JPanel (the class named Chess).
My Layout:

public class Main extends JFrame implements MouseListener, ActionListener{

    Chess chessPanel = new Chess ();
    JButton newGameButton = new JButton ("New Game");
    JButton loadGameButton = new JButton ("Load Game");
    JButton saveGameButton = new JButton ("Save Game");
    JButton exitButton = new JButton ("Exit");

    public static void main (String [] args) {
        new Main();
    }

    Main () {
        super ("Chess");
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dim);
        setLocation(0,0);
        setUndecorated(true);

        chessPanel.addMouseListener(this);
        add(chessPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());


        newGameButton.addActionListener(this);
        loadGameButton.addActionListener(this);
        saveGameButton.addActionListener(this);
        exitButton.addActionListener(this);

        buttonPanel.add(newGameButton);
        buttonPanel.add(loadGameButton);
        buttonPanel.add(saveGameButton);
        buttonPanel.add(exitButton);

        add(buttonPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    // ... Code ...
}

As you can see by the code, I have one JPanel in the CENTER, which takes nearly all the screen. In the bottom I have another JPanel (SOUTH), which has a row of buttons.

What I need is the size that the JPanel in the CENTER takes. When I call the getWidth(), getHeight() or getBounds() methods inherited from JPanel, they all return 0, because of the BorderLayout.
Any idea how to get the real values?

PS: The screen always takes up the entire screen, and will never be resized, if that helps.

  • 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-23T08:47:48+00:00Added an answer on May 23, 2026 at 8:47 am

    You’re likely calling getWidth before the JPanel has been rendered, and so it will be 0. The solution is to get the size after rendering, for instance after pack() or setVisible(true) has been called on the root container that holds this JPanel.

    Also, I recommend against calling setSize() on anything since most of the standard layout managers observe the preferred size of a component, not the size, and when you call pack() telling the layout managers to do their thing, the set sizes are usually ignored. You may want to make your JPanel that is in the center set its own size by overriding its setPreferredSize method if it needs to be a certain size. Then let the JFrame and its held containers set the bet fit size based on the their layout managers when you call pack.

    e.g.,

    import java.awt.*;
    import javax.swing.*;
    
    public class Main extends JFrame {
    
       Chess chessPanel = new Chess();
       JButton newGameButton = new JButton("New Game");
       JButton loadGameButton = new JButton("Load Game");
       JButton saveGameButton = new JButton("Save Game");
       JButton exitButton = new JButton("Exit");
    
       public static void main(String[] args) {
          new Main();
       }
    
       Main() {
          super("Chess");
          add(chessPanel, BorderLayout.CENTER);
    
          JPanel buttonPanel = new JPanel();
          buttonPanel.setLayout(new FlowLayout());
    
          buttonPanel.add(newGameButton);
          buttonPanel.add(loadGameButton);
          buttonPanel.add(saveGameButton);
          buttonPanel.add(exitButton);
    
          System.out.printf("chessPanel Size before rendering: %s%n", chessPanel.getSize());
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          add(buttonPanel, BorderLayout.SOUTH);
          pack();
          System.out.printf("chessPanel Size after rendering: %s%n", chessPanel.getSize());
          setLocationRelativeTo(null);
          setVisible(true);
       }
    
       // ... Code ...
    }
    
    @SuppressWarnings("serial")
    class Chess extends JPanel {
       private static final int CHESS_WIDTH = 600;
       private static final int CHESS_HEIGHT = CHESS_WIDTH;
       private static final int MAX_ROW = 8;
       private static final int MAX_COL = 8;
       private static final Color LIGHT_COLOR = new Color(240, 190, 40);
       private static final Color DARK_COLOR = new Color(180, 50, 0);
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(CHESS_WIDTH, CHESS_HEIGHT);
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          int panelWidth = getWidth();
          int panelHeight = getHeight();
          int sqrWidth = panelWidth / MAX_ROW;
          int sqrHeight = panelHeight / MAX_COL;
          for (int row = 0; row < MAX_ROW; row++) {
             for (int col = 0; col < MAX_COL; col++) {
                Color c = (row % 2 == col % 2) ? LIGHT_COLOR : DARK_COLOR;
                g.setColor(c);
                int x = (row * panelWidth) / MAX_ROW;
                int y = (col * panelHeight) / MAX_COL;
                g.fillRect(x, y, sqrWidth, sqrHeight);
             }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have swing app and I am using mybatis: public class MyAppCView
I am creating an touch screen application using Swing and have a request to
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class tictac2 implements ActionListener{ static
When using the SWT/AWT bridge to draw Swing components inside an SWT application, can
I am trying to create a custom component using Java AWT or Swing which
I'm working on a simple calculator program using java and javax.swing Basically, when you
Java applet code package M257Applet import java.applet.*; import javax.swing.*; import java.awt.*; public class HellowApplet
I have been using the Learning Java 2nd Edtion book to try make my
I was a problem with drawing rectangle using mouse. I have solve my problem
I have a problem with drawing. I have a frame with one button. Using

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.