How can I refresh a data in JTable

D

Dexter

I found a bit of code that solved my initial problem that was to
display two dimensional array data in a JTable. How can I change this
code if I want to display new data in a JTable which is showing the
data initially assigned to it.

Say the table was showing

Field 1 Field 2
USA D.C
UK London

And now that the program is running I want new data to be showing from
a 2 dimensional array

Field 1 Field 2
Aus Canberra
Ireland Dublin

This is the code snippet that helped me create the initial JTable

Thanks

Container contentPane = frame.getContentPane();
String headers[] = {"Leader", "Country"};
String data[][] = {
{"Tony Blair", "England"},
{"Thabo Mbeki", "South Africa"},
{"Megawati Soekarnoputri", "Indonesia"},
{"Hosni Mubarak", "Egypt"},
{"Vladimir Putin", "Russia"},
{"Vicente Fox", "Mexico"},
{"Ariel Sharon", "Israel"}
};
TableModel model =
new DefaultTableModel(data, headers) {
// Make read-only
public boolean isCellEditable(int x, int y) {
return false;
}
};
JTable table = new JTable(model);
// Set selection to first row
ListSelectionModel selectionModel =
table.getSelectionModel();
selectionModel.setSelectionInterval(0, 0);
selectionModel.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
// Add to screen so scrollable
JScrollPane scrollPane = new JScrollPane (table);
contentPane.add(scrollPane, BorderLayout.CENTER);
 
R

Roedy Green

I found a bit of code that solved my initial problem that was to
display two dimensional array data in a JTable. How can I change this
code if I want to display new data in a JTable which is showing the
data initially assigned to it.

have a look at the code for Vercheck.
https://wush.net/websvn/mindprod/listing.php?repname=mindprod&path=/com/mindprod/vercheck/
or http://mindprod.com/products1.html#VERCHECK to download it.

Basically, you lock the row, update your data in the ordinary way,
then trigger an event to let the GUI know what has changed.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"Don’t worry about the world coming to an end today.
It is already tomorrow in Australia."
~ Charles Schulz
 
P

pilotwithoutaplane

have a look at the code for Vercheck.https://wush.net/websvn/mindprod/listing.php?repname=mindprod&path=/c...
orhttp://mindprod.com/products1.html#VERCHECKto download it.

Basically, you lock the row, update your data in the ordinary way,
then trigger an event to let the GUI know what has changed.

--
Roedy Green Canadian Mind Productshttp://mindprod.com

"Don’t worry about the world coming to an end today.
 It is already tomorrow in Australia."
~ Charles Schulz

Mirko and Roedy

Thanks very much for your hints.
I finally got the code working with help from sources both you
mentioned.

I needed the code to work with a mortgage calculator that refreshed
the amortization schedule for new calculations. Its working now. No I
am not going to post the link as in past I been referred to by many
funny titles when doing so.

Thanks once again.

Asad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top