Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Adding JTextField to a JPanel at runtime

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I want to know 2 things
1) Adding JTextFields to a JPanel at runtime
2) Adding JTextFields to a JScrollPane at runtime
Both JPanel & JScrollPane have been added to the main GUI at compile time itself.
I need to add the JTextFields to the above 2 components at runtime.
Should I set the JPanel layout to null and set it's layout only at runtime with the JTextFields?
I have declared JPanel & JScrollPane at the beginning of the class as follows:
public static JPanel jp;
public static JScrollPane jsp;
I am using a lot of other files where data is constantly moving to & from them.So since I am creating the JPanel & JScrollPane in the beginning itself and adding them to other components,I am using the keyword static.
PROBLEM :

I want to just add the JTextFields to the JPanel
& JScrollPane at runtime.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not able to understand what exactly u r looking for,say if u want to dynamically change layout based on some condition you can setLayout at runtime else add layout manage at one time.and add the components as u desired.
using null Layout is not adviceble.always use someLayouts.
I don't know why u want to use static.
if u are creating only one object of that class,it won't affect the GUI visibility else,as u create new panels,and try to add it to container,it will be removed from the old one add it to new one.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are dynamically adding components to a visible GUI, you are going to need to call invalidate() and/or validate() on the parent component after you add the new textfields. The parent component is the component that is being added to.

Also, you won't be able to add the textfields directly to a scrollpane. You'll have to add them to a panel inside the scrollpane.
 
reply
    Bookmark Topic Watch Topic
  • New Topic