Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Separating form code in a JFrame Class

 
Abhishek Kaushik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhishek Kaushik
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic