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 8788663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:11:44+00:00 2026-06-13T22:11:44+00:00

i have a JFrame that contain a JButton when JFrame shows up, user will

  • 0

i have a JFrame that contain a JButton when JFrame shows up, user will resize JFrame, then click the JButton which will do:

System.out.print("actual size now = "+JFrame.getsize");

but the size will not result the actual size of JFrame on the screen.

The size still wrong when i use:

  • JFrame.getSize

  • JFrame.getPrefferedSize

  • JFrame.getMinimumSize

  • JFrame.getMaximumSize

  • JFrame.getBounds

  • JFrame.getHeight & getwidth

what should i do?

Thanks a lot for any kind of 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-06-13T22:11:45+00:00Added an answer on June 13, 2026 at 10:11 pm

    The size you are getting is the size of the JFrames contentPane and the size of the Insets added. Simply call JFrame#getInsets() and subtract them from the JFrames width and height which you can get via JFrame#getSize().

    Here is an example:

    import java.awt.Dimension;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
        public Test() {
            initComponents();
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Test();
                }
            });
        }
    
        private void initComponents() {
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new JPanel() {
                @Override
                public Dimension getPreferredSize() {//the frame should now return a width and height of 300
                    return new Dimension(300, 300);
                }
            };
    
            JButton button = new JButton("Get JFrame real width and height");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
    
                    Dimension frameSize = frame.getSize();
                    Insets insets = frame.getInsets();
    
                    System.out.println("Frame size via getSize(): " + frameSize);
                    System.out.println("Insets: " + insets);
    
                    double width = frameSize.getWidth();
                    double height = frameSize.getHeight();
    
                    //calculate real width/height
                    double realWidth = width - (insets.left + insets.right);
                    double realHeight = height - (insets.top + insets.bottom);
    
                    System.out.println("Width: " + realWidth + " Height: " + realHeight);
                }
            });
    
            panel.add(button);
            frame.add(panel);
    
            frame.pack();
            frame.setVisible(true);
    
        }
    }
    

    output:

    Frame size via getSize(): java.awt.Dimension[width=316,height=338]

    Insets: java.awt.Insets[top=30,left=8,bottom=8,right=8]

    Width: 300.0 Height: 300.0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent JFrame it contain a JButton . functionality of that button
I have a JFrame which contains a JApplet. There are shortcut keys that I
I have class main extends jframe , it has a button that calls /shows
I have a JFrame that shows a preview content, since the loading of preview
I have this class called PollFrame that extends JFrame in a file called PollFrame.java
I have a program that creates a JFrame and makes it visible. Is there
I have an application that is works fine and the JFrame for it is
I have a JFrame which on instantiation calls a custom JDialog(Login). If login is
I have a JFrame with some a JTextField and a JButton. I want it
I have created jframe in which jpanels are added dynamically what i can't do

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.