Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with visual editor/visual class

 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to separate my GUI from other code.

I created Visual Class, and I would like to place main method in main class. How do I connect these too ? If I have this in main class:


MyFrame myFrame = new MyFrame();
myFrame.setVisible(true);

It will not create an instance of my visual class MyFrame.

My Visual Class:



When I place main method in this class I cannot even run it. Since I have never used visual editor, is there as well, somthing else what I should know from start.

Any help is welcome and thank You!
 
Sveta Nartano
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nicol!

Main problem here - that you create your class extending JFrame, but you never add any components to the instance of THIS class. Besides you add all the components to the frame that is created in the getJFrame method, which is never called in your code.

You should replace your method



with constructor:


and call it from such main method:




Having your code you can use such main method:


But it's NOT appropriate in this context, because you don't really EXTEND JFrame class (you even don't need to write "extends JFrame" in declaration). You just create some GUICreator class that constructs and fill JFrame instance in getJFrame method.

 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sveta!

Now I see where is the problem! Thank you for quick response!

I haven't use Visual Editor before, so I assumed that "all is included"
 
Nicol Green
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to replace that code with constructor, but it says "The methods setTitle, setSize and setContentPane are undefined for type MyFrame."

I thought it is bad practice to mix components from Visual Editor and components programme ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic