This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Best way to refresh Swing interface/add new components

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the windows for my looks like this:

JLabel
JTextField JTextField JTextField JTextField JTextField
JLabel
JTextField
JLabel
JTextField

Now, what I need the user to be able to do is add another set of JTextFields, as such:

JLabel
JTextField JTextField JTextField JTextField JTextField
JTextField JTextField JTextField JTextField JTextField

JLabel
JTextField
JLabel
JTextField


So far I've only been able to add an additional five text fields to the bottom of the panel, but I need everything to go in-between. I thought about using a List to store all the different componentsa, nd then just redraw the whole window when the list is updated. Is there a better way?









 
Paul Clapham
Sheriff
Pie
Posts: 20762
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in my opinion the best way to do that is to add the new fields when you initialize your GUI, but make them not visible. Then when the user asks to "add" them, you simply make them visible.

Others may have other ideas.
 
Rob Camick
Ranch Hand
Posts: 2542
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I need everything to go in-between


Instead of using;



you can use:



Check out the Container API for more information.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not answering the question, but criticizing JOptionPane's UI class that why it lays out every component even after when only button text is changed?

Are these kind of old coding styles beeing ever updated only by better LookAndFeel packages.

Is there need to do hard coding changes, not only adding new ones. Then make compilers that updates the old stuff to new one.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, in my opinion the best way to do that is to add the new fields when you initialize your GUI, but make them not visible. Then when the user asks to "add" them, you simply make them visible.

Others may have other ideas.


Thanks.

I tried your idea and used setVisible(false), but that doesn't really solve my problem because there's just an extra line of blank space. The components are actually there, just not visible and I can't predict how many fields the user will actually need. I've come up with a better solution though, but it will require me to redesign the GUI.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
but I need everything to go in-between


Instead of using;



you can use:



Check out the Container API for more information.


I did try that as well, but it just seemed to add extra components instead of inserting them between two existing ones. Perhaps I'm wrong, but the best solution that I see is to use a List and redraw as needed.




 
Paul Clapham
Sheriff
Pie
Posts: 20762
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Mrozik wrote:... and I can't predict how many fields the user will actually need.


I thought that might be the case. But that wasn't what you said in your original post, so I just answered what you asked.

In general I would avoid GUI designs which allow the user to make arbitrary modifications (like adding 12,000 text fields for example) as the results can be ugly.
 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

I thought that might be the case. But that wasn't what you said in your original post, so I just answered what you asked.

In general I would avoid GUI designs which allow the user to make arbitrary modifications (like adding 12,000 text fields for example) as the results can be ugly.


You're right, I'll try to make things clearer next time.

As I'd mentioned, I've come up with a different solution. There will only be one text field, but the user will be able to see what has been added in a separate panel.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic