I'm trying to replace one Jtable already defined with tis correponding Vector of vector data by other with the same carateristics stored in a file. Here my code:
else if (e.getActionCommand().equals("import"))
{
JFileChooser file = new JFileChooser();
int i = file.showOpenDialog(this);
if(i == JFileChooser.APPROVE_OPTION)
{
File f = file.getSelectedFile();
String filePath = f.getPath();
try
{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(filePath));
Vector vectorData = (Vector)input.readObject();
data = new DefaultTableModel(vectorData, columNames);
table = new JTable(data);
labelStatus.setText("Archivo exitosamente importado.");
} catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
Problem here is that when i make the import and select the file that contains the Jtable data the Actual Table no change by the Imported one, how can i make the Switch?
Here code where the Jtable is added into ContentPane(Jpanel):
//data & columnNames are the data tha i used originally with the old Jtable
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); //Sorter Descending or Ascending the data.
table.setRowSorter(sorter);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setBounds(22, 78, 764, 177);
scrollTable.setViewportView(table);
contentPane.add(scrollTable);
Note: I'm using a single Jtable, DefaaultTableModel using it as Global Variable and referencing from the Import method to change the odl one by the new one but using the same .
New Update, whole functional code:
public class Demo extends JFrame implements ActionListener
{
private JPanel contentPane;
DefaultTableModel data;
JTable table;
Vector<String> dataRow;
Vector<String> columnNames;
JScrollPane scrollTable;
public Demo() throws FileNotFoundException, IOException, ClassNotFoundException
{
columnNames = new Vector<>();
columnNames.addElement("Name");
columnNames.addElement("Cc");
columnNames.addElement("Age");
columnNames.addElement("Phone");
columnNames.addElement("Date");
columnNames.addElement("Amount");
ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:/Users/Harry/Desktop/AA gym Database.txt"));
Vector data = (Vector)in.readObject(); //Add try catch instead of THROWS DECLARATION.
//rowData
DefaultTableModel model = new DefaultTableModel(data, columnNames);
table = new JTable(model);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); //Sorter Descending or Ascending the data.
table.setRowSorter(sorter);
scrollTable = new JScrollPane(table);
scrollTable.setBounds(22, 78, 764, 177);
scrollTable.setViewportView(table);
contentPane.add(scrollTable);
public void actionPerformed(ActionEvent e)
{
else if (e.getActionCommand().equals("import"))
{
JFileChooser file = new JFileChooser();
int i = file.showOpenDialog(this);
if(i == JFileChooser.APPROVE_OPTION)
{
File f = file.getSelectedFile();
String filePath = f.getPath();
try
{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(filePath));
Vector vectorData = (Vector)input.readObject();
data = new DefaultTableModel(vectorData, columnNames);
table.setModel(data);
}
}
}
}
data = new DefaultTableModel(vectorData, columNames);
table = new JTable(data);
You are creating a new TableModel and a new JTable.
The problem is you never add the new table to the GUI. You can't just change the reference to the "table" variable and expect the table to be added to the GUI.
So the solution is to NOT create a new JTable. Instead you just reset the TableModel of the existing JTable:
data = new DefaultTableModel(vectorData, columNames);
table.setModel( data );
Basically you should not be creating new Swing components when you want to change the data. Just change the model.
Edit:
Check out Saving content of Interactive JTable to .txt file to read it upon next run for a solution that will allow your to save/restore a table.