Get size of componets in JFrame with full screen

Hi Guys,

I need to get componets(JPanel, JSplitPane, etc) size in main JFrame.

First the main frame default restore size is set by

"mainFrame.setSize(100,100);"

And then it is full-screened by

"mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);"

And when I get the size of jpanel inside the main frame,

the size is coming as default restore frame not full screen frame.

My purpose is to get the size when the frame is full size when the frame is opening(initiating).

You may understand if see the below code.

Thank you in advance.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class GetComponentSizeimplements ActionListener

{

JFrame mainFrame;

JPanel subPanel;

JButton bt;

public GetComponentSize()

{

mainFrame =new JFrame();

subPanel =new JPanel();

bt =new JButton("Get Panel Size");

bt.addActionListener(this);

subPanel.add(bt);

mainFrame.getContentPane().setLayout(new BorderLayout());

mainFrame.getContentPane().add(subPanel, BorderLayout.CENTER);

mainFrame.setSize(100,100);//Default Restore size

mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH);//Make frame full screen

mainFrame.setVisible(true);

System.out.println("First size:"+subPanel.getSize());//Print component JPanel size

}

publicvoid actionPerformed(ActionEvent e)

{

System.out.println("Second size:"+subPanel.getSize());

}

publicstaticvoid main(String arg[])

{

new GetComponentSize();

}

};

[2608 byte] By [Gilotina] at [2007-10-3 3:39:54]
# 1
This is what I get when I run the code:First size:java.awt.Dimension[width=115,height=66]Second size:java.awt.Dimension[width=1024,height=712]Those are the numbers I would expect, so I'm not sure what you are asking.
camickra at 2007-7-14 21:35:25 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi Camickr,

Sorry for the confusing.

I meant that the application is always running with full screen state.

And I need to get the size of inner component.

But as you saw, the two size is different.

I expected it would be same because in the constructor, the "subPanel.getSize()" was called after "mainFrame.setExtendedState(Frame.MAXIMIZED_BOTH)".

But the inner panel size is coming as "[width=115,height=66]", why not [width=1024,height=712].

And after opening the window, if I push the button, Ican get the expected size. I don't know when exactly "setExtendedState()" is executed.

So my aim is to get the inner component size with full screen frame when opening time.

Thank you.

Message was edited by:

Gilotin

Gilotina at 2007-7-14 21:35:25 > top of Java-index,Desktop,Core GUI APIs...
# 3

Sorry, I understand what you are saying now.

Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html]How to Use Threads[/url]. All Swing code should execute in the EDT. Some methods will add code to the end of the EDT to execute later. It would appear that the setExtendedState(...) is one of these methods. So the System.out.println(...) actually executes after the method. You can add your code to the end of the EDT by using:

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

System.out.println("First size:"+subPanel.getSize());

}

});

Or maybe a better approach would be to add a WindowListener to the frame and then handle the windowOpened() event.

camickra at 2007-7-14 21:35:25 > top of Java-index,Desktop,Core GUI APIs...
# 4
Hi camickr,Thank you so much for the solution.Have a nice day!
Gilotina at 2007-7-14 21:35:25 > top of Java-index,Desktop,Core GUI APIs...