wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Separating form code in a JFrame Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Separating form code in a JFrame Class" Watch "Separating form code in a JFrame Class" New topic
Author

Separating form code in a JFrame Class

Abhishek Kaushik
Greenhorn

Joined: Mar 03, 2010
Posts: 14
Dear friends,

I am working on a desktop program (Multi frame app) where i have a form with several JTextFields and a Button. First I created one JInternalFrame and placed all form elements there but later I found that i need to create exactly same form on a different JInternal window. So tried to separate the form code from Frame code and create another class (Sub-Class of JComponent) so that i can reuse the form code. But my code is not working properly

here is the simple code

-- Window Class --


-- Form Class --

Here i cannot see the textfield in the widow after running but its invisibility present there..I came to know this when i click on the window everywhere and textfield area started appearing....similar thing happens when i have several textfields some appear and some dont.

Please help me
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
Like most newbies your problem is that you are NOT using a Layout Manager. Forget about trying to use a "null layout" and learn how to use Layout Managers.

Read the section from the Swing tutorial on Using Layout Managers. The tutorial is full of examples to get you started with proper Java coding techniques.

Also, learn to use proper class names. Class names should be upper cased.
Abhishek Kaushik
Greenhorn

Joined: Mar 03, 2010
Posts: 14
Thanks Rob,

I will try to learn Layout Managers. I had removed "null" layout from the code and it should run with default FlowLayout ....but i see no difference in result. I guess it has more to do with the following cases. I tried that too but partial success only..

http://stackoverflow.com/questions/2317273/why-does-this-swing-list-not-display-its-elements
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10268
    
    8

Abhishek,
Welcome to the Ranch.

Why are you subclassing JComponent? You should be subclassing JPanel.
You are adding the children after making the frame visible. The call to set visible should be made after adding all the children.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Abhishek Kaushik
Greenhorn

Joined: Mar 03, 2010
Posts: 14
Maneesh Godbole wrote:Abhishek,
Welcome to the Ranch.

Why are you subclassing JComponent? You should be subclassing JPanel.
You are adding the children after making the frame visible. The call to set visible should be made after adding all the children.


Hi ,Maneesh,

SubClassing JPanle seems more logical but i have the same result....
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
I had removed "null" layout from the code and it should run with default FlowLayout ....but i see no difference in result.


How do you know FlowLayout is the default? Did you check the layout using the getLayout() method?

And how does your code look like the code examples found in the tutorial? Where in the tutorials do you see the getRootPane() method used.

I pointed you to the tutorials so you could "learn by example".
Abhishek Kaushik
Greenhorn

Joined: Mar 03, 2010
Posts: 14
How do you know FlowLayout is the default? Did you check the layout using the getLayout() method?


I did checked using getLayout and it says this is FlowLayout moreover Sun Tutorial site also says default layout for JPanels are same

result
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2179
    
    7
I did checked using getLayout and it says this is FlowLayout moreover Sun Tutorial site also says default layout for JPanels are same


My comment was about your original code which extended JComponent.
 
wood burning stoves
 
subject: Separating form code in a JFrame Class