Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get size of componets in JFrame with full screen

843805Aug 28 2006 — edited Aug 29 2006
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 GetComponentSize implements 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
	}

	public void actionPerformed(ActionEvent e)
	{
		System.out.println("Second size:"+subPanel.getSize());
	}

	public static void main(String arg[])
	{
		new GetComponentSize();
	}
};
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 26 2006
Added on Aug 28 2006
4 comments
336 views