?
Solved

Get size of a JFrame

Posted on 2003-03-12
6
Medium Priority
?
223 Views
Last Modified: 2008-02-20
I need to get the Width and height of a J frame and pass those 2 integers to a method.  I have attempted this with myFrame.getHeight() but when I try to print the value all I get is a 0.  Can anyone show me what sort of code I should be using for this.

Thanks...
0
Comment
Question by:grhs0043
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8123767
make sure the frame is visible before you check getHeight and getWidth
0
 

Author Comment

by:grhs0043
ID: 8123940
It is .... i can see the frame when the program compiles but I cant get the height or width of it.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8123952
can you post your code?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:grhs0043
ID: 8124017
public class mainFrame extends Frame {
JPanel graphPanel = new JPanel();
graphPanel.setEnable(false);  // <--tried this enabled to
graphPanel.setLayout(xYLayout1);
contentPane.add(graphPanel, BorderLayout.CENTEr);

panelHeight=graphPanel.getHeight();   <--panel Height INT
panelWidth=graphPanel.getWidth();     <-- so is panelWidth
graphPanel.add(myGraph, new XYConstraints(0,0,350,350));
}

This is the just of the code.... hopefully it helps...
Any Idea whats wrong.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 8124074
i'm not sure if you are doing this, but like i said, the JFrame has to be VISIBLE on screen before calling getHeight and getWidth:

public class MainFrame extends JFrame {
   JPanel graphPanel;

   public MainFrame() {
      graphPanel = new JPanel();
      getContentPane().add(graphPanel, BorderLayout.CENTER);
      this.show(); // if the below lines are above this, they will both return 0
      System.out.println(graphPanel.getHeight());
      System.out.println(graphPanel.getWidth());
   }

   public static void main(String[] args) {
      MainFrame mf = new MainFrame();
   }
}
0
 

Author Comment

by:grhs0043
ID: 8124102
thanks for the help.  Thar one line completely solved my problem(     this.show();   )

Thanks
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question