*
The moose likes Swing / AWT / SWT and the fly likes Runtime Changes to Your GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Runtime Changes to Your GUI" Watch "Runtime Changes to Your GUI" New topic
Author

Runtime Changes to Your GUI

Vicenç Aleix
Greenhorn

Joined: Jun 05, 2009
Posts: 4

Hello,

Can Swing components be generated dynamically at run time? A simple example would be, a mouse-click inside a Text Field would draw a new Text Field. Again, a mouse-click inside this new Text Field would draw a new Text Field again. There is no limit on number of created Text Fields.

Here they describe how to replace or hide a component but that means that number of components is know during coding (look for Runtime Changes to Your GUI):
http://java.sun.com/docs/books/tutorial/uiswing/layout/group.html

Thank you.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
This is a question that comes up a lot. It seems to be a poorly documented aspect of java. I understand it can be done, but i can't tell you how. I'm interested in how this thread turns out.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
I would suggest experimenting with an ArrayList<JTextField>. Add a listener to your first one and in the actionPerformed method add a new JTextField to the ArrayList as well as set up it's listener and add it to the panel. Just a thought, I haven't really ever tried to do something like this.


SCJP 6, SCWCD 5
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Can Swing components be generated dynamically at run time?

simple example using buttons

W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
using the method I suggested above (and a little free time i had this afternoon) i came up with this as a MouseListener



when i added that to my ArrayList<JTextField> it worked well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime Changes to Your GUI
 
Similar Threads
Attaching MouseListeners.....
Background color change help
JInternalFrames and focus
Updating a variable
Opening window by clicking Paint Area !