tags:

views:

59

answers:

4

How would i call this function in my main?

    private JFrame getMainpageframe1() {
    if (mainpageframe1 == null) {
        mainpageframe1 = new JFrame();
        mainpageframe1.setSize(new Dimension(315, 306));
        mainpageframe1.setContentPane(getMainpage());
        mainpageframe1.setTitle("Shopping For Less: Main Page");
        mainpageframe1.setVisible(true);
        mainpageframe1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    return mainpageframe1;
}

public static void main(String[] args) {        
   //call that function to output the JFrame?
}

thanks

A: 
YOurClass instance = new YourClass();
instance.getMainpageframe1();

However this code is really really bad. You should move at least the setVisible() out of it - othwerwise the code will block at the point and the method would not return until the frame is not visible anymore.

YOurClass instance = new YourClass();
instance.getMainpageframe1().setVisible(true);
Bozhidar Batsov
i tried that, i get an error saying "your class getMainpageframe1() is not an instance of JFrame. JFrame instance = new JFrame(); instance.getMainpageframe1();
bat
bat, youre getting this error because new JFrame() instantiates a JFrame, wich does not implement getMainpageframe1()see here : YourClass instance = new YourClass();this instantiates the yourclass wich implements the getMainpageframe1() method
Lucass
huh so what is the correct code?
bat
why would the code block at setVisible???
josefx
@josefx: I'd be curious to find out too because Bozhidar seems to know his stuff when it comes to Swing.
James P.
A: 
public class YourClass {
    public static void main(String[] args) {
        YourClass instance = new YourClass();
        JFrame frame = instance.getMainpageframe1();
    }
}

Reason why you have to create an instance of the whole class in main is because you can't invoke a non-static function from a static function.

Coding District
ya i added the public class. i get an error saying: The method getMainpageFrame1() is undefined for the type JFrame
bat
+1  A: 

For one, you'll want to place your GUI stuff on the EDT. The Java library provides you with some helper methods that'll make your life a whole lot easier with SwingUtilities.

Second I'd try to refactor the code a bit and possibly move the JFrame you are building into a separate class. In this code example I made it part of the same class that contains the main method and I am extending JFrame here.

public class YourApp extends JFrame {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        YourApp app = new YourApp();
        app.setupFrame();
      }
    });
  }

  private setupFrame() {
    this.setSize(new Dimension(315, 306));
    this.setContentPane(getMainpage());
    this.setTitle("Shopping For Less: Main Page");
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 }
Herminator
why SwingUtilities.invokeLater ?
Lucass
GUI creation code should take place in the event dispatch thread, henceforth the SwingUtilities.invokeLater(or alternatively EventQueue.invokeLater). The code will work without it as well, but complex setups might have event dispatching problems.
Bozhidar Batsov
thanks that helped kinda =]
bat
A: 
public class YourClass{

  JFrame mainpageframe1;

  private JFrame getMainpageframe1() {
    if (mainpageframe1 == null) {
      mainpageframe1 = new JFrame();
      mainpageframe1.setSize(new Dimension(315, 306));
      mainpageframe1.setContentPane(getMainpage());
      mainpageframe1.setTitle("Shopping For Less: Main Page");
      mainpageframe1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      mainpageframe1.setVisible(true);
    }
    return mainpageframe1;
  }

  public static void main(String[] args) {
    YourClass yourClass = new YourClass();
    yourClass.getMainpageframe1();
  }

}
Lucass