Ok so lets say I have any GUI that needs its contents changed while it is running, so that it will re-display the new contents. An example would be a JTable that just lists things from an ArrayList. So if I had an ArrayList<GroceryList> my JTable might have

Cheese
Crackers

But once the user highlights 'crackers' and clicks delete, I want the JTable to re-display and just say Cheese. I have been doing this by doing frame.setVisible(false) and then using createAndShowGUI(), but this does not seem to work as planned. Note that my JFrame is a static variable and that createAndShowGUI is invoked using invokeLater() and new Runnable like Java Sun suggests.

Also, please note that this problem is one I have been having for all my GUIs, not just the JTable one in particular. What I would LIKE is a way to simply 'delete' the GUI from existence, then simply re-create it using createAndShowGUI. I have been trying to do this using frame.dispose() and frame.setVisible(false), but these things do not seem to have the desired effect...

I have been trying to do this using frame.dispose() and frame.setVisible(false), but these things do not seem to have the desired effect...

nope, this sure won't do what you want it to do...
all you say is (hide me) (show me)
the thing you need to say is (change my data)

the last thing you want is to dispose of the frame and create a new one, since it's not efficient, and on a slow computer, it might show.

what you have to do, is just update your JTable. if your JTable is based on your ArrayList, make sure you have two ArrayLists, one that holds all the possible data in it, and one that contains all the current data.

if you select something from the currentData, and delete it, it 's still in the completeData. delete it from currentData, and set the data in the JTable to the new data of currentData, and you did your job.
no dispose, no new JFrame

For updating the GUI at runtime, use JPanel for storing the contents and use the revalidate() function. Also for deleting all the components use removeAll() method.

So if I have a static JFrame which has a JPanel which has a static JTable inside of it, should I do JTable.removeAll() ? And if so, how should I re-add all of the data to it? Simply say (if table is my JTable) table = new JTable(data, columns)?


Actually, judging by what you said, I'm thinking I should use JFrame.removeAll(), since things are being added to the JFrame each time... and there will still be components in it if I only use Jtable.removeAll()

thanks man


Also, if I just do what I said above, and removeAll() of the components, then add them back in their updated form, do I still need to revalidate() ?

What exactly do you mean by static JFrame or static JTable? You need to create objects of both of them to have them on the screen.

If the problem is JTable specific u can refer to the JTable API for more details.

If the problem is updating the GUI at Runtime, here's how u can go about:
Add the components in a JPanel. Then call the revalidate() method on the JPanel object. revalidate() is not available for JFrame.

And then if you want to delete all the components on the JFrame or JPanel use removeAll().

Perhaps I am missing the point. If so, I apologize, but here's some more info so can you can put what you said in context for me please?

I have two GUIs that I was to interact with each other. What happens in one GUI is dependent on what happens in the other GUI. For example, in the one GUI, I have a list of Teams being displayed in a JTable. In the other GUI, I have a list of Games that one of the Teams (from the previous GUI) has played. When I delete one of these games, I want the first GUI to respond by updating the entire GUI so that it will reflect the changes.

So you're saying all I have to do to accomplish this task is by saying JPanelFromFirstGUI.revalidate()? Where would the revalidate code go? In the second GUI? I apologize, but I can't find any information about this in the java Sun tutorials or anywhere else... all I can find are easy examples that I know how to implement.

thanks

You will have to either add new components to the JPanel object or remove all existing components from the JPanel object and then add new components based on the option in the other GUI. After adding or deleting the components you need to call the revalidate() method on the JPanel object so that the changes are reflected on the screen. Unless you call revalidate() the GUI will not get updated.

You can remove all the components on the other GUI once the Team is deleted from the first GUI. And then once a new team is selected in the first GUI, you can add the required components on to the second GUI and then call revalidate()

revalidate() is there in JPanel. So use JPanel for holding the components. U can organise your GUI by using multiple JPanels.

This is a general way of updating the GUI dynamically. You will have to change it based on the project requirements. I have been using the same method for my current project

commented: Helpful. +1

Excellent man. Thanks for the help. I'm adding you reputation pts for what thats worth.

Also, your explanation would require that my JPanel in the first class is static, would it not? Because if it isn't static, then my other GUI class has no way of revalidating the JPanel from the first class.

use Object references for revalidating. The revalidate() method is non static. So u can use the object of JPanel to revalidate().

Basically in GUI u create objects to represent the components.

Thanks for the repo points

Yeah, no problem.

The revalidate description says it is unnecessary in most cases, though. And trying to use it has caused my program to become non-responsive, although this could be my fault. This sucks - I've been working to try to figure out how this stuff works all day and I've literally made backwards progress - my program works worse than it did when I started.


Edit:


I give up unless somebody wants to help out some more or go into more detail about what needs to be done. It isn't for lack of effort, since as anyone can see, I've been at this all day. And I've read almost all of the Sun tutorials on this subject so I don't understand why I can't figure out whats wrong.

Yeah, no problem.

The revalidate description says it is unnecessary in most cases, though. And trying to use it has caused my program to become non-responsive, although this could be my fault. This sucks - I've been working to try to figure out how this stuff works all day and I've literally made backwards progress - my program works worse than it did when I started.


Edit:


I give up unless somebody wants to help out some more or go into more detail about what needs to be done. It isn't for lack of effort, since as anyone can see, I've been at this all day. And I've read almost all of the Sun tutorials on this subject so I don't understand why I can't figure out whats wrong.

I have been using it all through for my project. And yeah to mention my project is building a mini relational database. This requires a lot of GUI work as i am doing a GUI based one and not command line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.