• 0

[JAVA] Passing arguments to a JFrame that is instanced inside run()


Question

So I have a class that extends JFrame and has a main method in it.

Main looks like this:

	public static void main( String args[] )  {


		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new MyJFrame().setVisible(true);
			}
		});
	}

The problem is, I want to pass the arguments that main receives to the MyJFrame constructor, but it won't work since I can't access the main() variables inside an "inner class" (according to netbeans). I admit that I don't know exactly how that code I just posted works, since it's an example that is everywhere and works.

Anyway, I belive that doing "new Runnable()" is like declaring a class that implements Runnable, and public void run() is just the implementation of the only method that is in the Runnable interface.

But it won't let me pass arguments to it, and I don't know if should make the class MyJFrame to implement Runnable. I don't even know how that would work out anyway.

I think that it will only let me pass final static arguments.

Any ideas on how I can pass those arguments that main receives to the MyJFrame constructor inside run() ?

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0
class MyFrame extends JFrame implements Runnable() {

  public MyFrame(String[] args) {
	// read your args
  }

  public void run() {
	 setVisible(true);
  }
}
.....

public static void main( String args[] )  {

EventQueue.invokeLater(new MyFrame(args));

}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.