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);
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);