Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime Changes to Your GUI

 
Vicenç Aleix
Greenhorn
Posts: 4
Firefox Browser Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can Swing components be generated dynamically at run time?

simple example using buttons

 
W Pearce
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic