aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Refreshing a window Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Refreshing a window" Watch "Refreshing a window" New topic
Author

Refreshing a window

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Hello,

I have a JFrame that contains 3 JPanels. One of the panels contains a JScrollPane which contains a JTable.
How can I refresh the screen after I have added to the table?

Thank you.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Normally if you update the JTable via its table model, that happens automatically. Perhaps you aren't doing it that way? Or perhaps you wrote your own table model which doesn't notify the JTable of changes?
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
>> Normally if you update the JTable via its table model, that happens automatically. Perhaps you aren't doing it that way? Or >> perhaps you wrote your own table model which doesn't notify the JTable of changes?

I do have my own table model, and I tried calling a method that implements the fireTableDataChanged() method, but that didn't do any good. I have employe a ListSelectionListener to getValueAt(), I suppose I could try setValueAt().

G.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
>> I have employe a ListSelectionListener to getValueAt(), I suppose I could try setValueAt().

...eh' probably not. Don't know what I was thinking.
I don't understand why fireTableDataChanged() didn't work.

Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
... hey are Katydid's edible? One just landed on my lamp.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Geoff Jefferson wrote:>> I have employe a ListSelectionListener to getValueAt(), I suppose I could try setValueAt().

...eh' probably not. Don't know what I was thinking.
I don't understand why fireTableDataChanged() didn't work.


Hard to say without code. If you can distill your problem into an SSCCE, that would help our figuring this out immensely.


... hey are Katydid's edible? One just landed on my lamp.

Why not give it a try and find out!

Luck!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
I do have my own table model,


Why? Whats wrong with the DefaultTableModel?

and I tried calling a method that implements the fireTableDataChanged()


You don't need to implement this method its already implemented in the AbstractTableModel.

If you think you really need to create a custom model then look at the source code of the DefaultTableModel to see how it uses the fireXXX methods.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102

Here is my example. There are two files. I hope that is okay.
I'm trying to get the screen to refresh after entering data into textFields upon button click.







Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
You where asked for a SSCCE.

Your question is about refreshing a JTable. Whats with all the other code?
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
Previousely I was loading the table from a file. Here I am trying to load the table on the fly by clicking on the button
which should load the list.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
One problem is here:




If you create a new non-displayed "Table" object in this method and add something to it, what effect will this have on the completely
separate and distinct Table object (in other words "this") that is being displayed?

Edit: problem number 2: you are tring to fiddle directly with the List that is at the core of the table's model, a field that should be
private, rather than using a public addRow method or something similar that fires the table model's listeners.

problem number 3 (to echo rob camick): why use AbstractTableModel when a DefaultTableModel would be a much better fit and
would be much easier to use.

.....

possibly more to come...
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
If you create a new non-displayed "Table" object in this method and add something to it, what effect will this have on the completely
separate and distinct Table object (in other words "this") that is being displayed?

I see.

Edit: problem number 2: you are tring to fiddle directly with the List that is at the core of the table's model, a field that should be
private, rather than using a public addRow method or something similar that fires the table model's listeners.

So I shouldn't try to add to the list at all?

problem number 3 (to echo rob camick): why use AbstractTableModel when a DefaultTableModel would be a much better fit and
would be much easier to use.

My goal is to be able to use the list as an item file that can be edited. Wouldn't I need an Abstract model to do this?


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Geoff Jefferson wrote:
pete stein wrote:
Edit: problem number 2: you are tring to fiddle directly with the List that is at the core of the table's model, a field that should be
private, rather than using a public addRow method or something similar that fires the table model's listeners.


So I shouldn't try to add to the list at all?

Nope, that would be breaking encapsulation. Again, if you are using an AbstractTableModel, you want to give the model a public addRow(....) method that accepts an array of objects, or an object of your data class, and then have the data added to the model's core, to it's private List from within this method. You can then fire the listeners which I don't see you ever doing.

problem number 3 (to echo rob camick): why use AbstractTableModel when a DefaultTableModel would be a much better fit and
would be much easier to use.

My goal is to be able to use the list as an item file that can be edited. Wouldn't I need an Abstract model to do this?

Nope, it would be much easier to use the default model, even for this. Now when you say you want to edit this file, do you mean that it should be in text format and editable via a text editor? If so, then serialization won't work at all.

The DefaultTableModel class has an addRow method that fires listeners for you. It also has getDataVector if you want the Vector of Vectors that holds the data, but you may be better of just calling getValueAt(...) to extract your data from the model.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
The only disadvantage I see of using a DefaultTableModel is that you would have to "un-box" your ItemFile objects into an array of String to be able to add them into a row, and likewise you'd have to "box" the array of String into an ItemFile object to extract the data and use it as an ItemFile datum, but this problem is minor compared to the effort saved by using the pre-made DefaultTableModel.

e.g.,
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
1) 300+ lines of code is NOT a SSCCE.
2) Still using a custom model

I'm amazed someone took the time to glance through all that code to provide help. Too bad you can't make the effort.

Apparently I'm wasting my time since none of my suggestions have been followed. Good luck with future posting I won't contine to waste my time.
Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
>> Now when you say you want to edit this file, do you mean that it should be in text format and editable via a text editor? If >> so, then serialization won't work at all.

My intention was to store each item as an ItemFile object in an ArrayList, and then to store the ArrayList on file.
The editing would be done by changing what is in the table, adding, or deleting also from the table. Then to put
back into the list and then to file.

>> The DefaultTableModel class has an addRow method that fires listeners for you. It also has getDataVector if you want the >> Vector of Vectors that holds the data, but you may be better of just calling getValueAt(...) to extract your data from the
>> model.

So I need to figure out a way to copy my list into Vector for use in the model, and then from Vector to list for persistant
storage?




Geoff Jefferson
Ranch Hand

Joined: Apr 09, 2009
Posts: 102
300+ lines of code is NOT a SSCCE. Even after you where asked a 2nd time by two different people to post a SSCCE.

I'm amazed someone took the time to glance through all that code to provide help. Too bad you can't make the effort.

You actually learn more by creating the SSCCE.


I'm sorry. I'll not let it happen again. I just wanted to represent exactly what I was trying to do.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Refreshing a window