• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Separating form code in a JFrame Class

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic