aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Best way to refresh Swing interface/add new components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Best way to refresh Swing interface/add new components" Watch "Best way to refresh Swing interface/add new components" New topic
Author

Best way to refresh Swing interface/add new components

Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Jun 13, 2009
Posts: 2183
    
    7
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

Joined: Dec 13, 2008
Posts: 112
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

Joined: Feb 10, 2013
Posts: 117

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

Joined: Feb 10, 2013
Posts: 117

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Feb 10, 2013
Posts: 117

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.

 
Consider Paul's rocket mass heater.
 
subject: Best way to refresh Swing interface/add new components