我正试图用存储在文件中的具有相同特征的另一个Jtable来替换已经用相应向量数据的向量定义的一个Jtable。这是我的代码:
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();
}
这里的问题是,当我进行导入并选择包含Jtable数据的文件时,实际表不会被导入的表更改,我如何进行切换?
这里是将Jtable添加到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);
注意:我使用的是一个Jtable,DefaaultTableModel将其用作全局变量,并从Import方法中引用以逐个更改odl,但使用相同的方法。
新更新,整个功能代码:
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);
您正在创建一个新的TableModel和一个新JTable。
问题是您永远不会将新表添加到GUI中。您不能仅仅更改对"table"变量的引用并期望将该表添加到GUI中。
因此,解决方案是不创建新的JTable。相反,您只需重置现有JTable:的TableModel
data = new DefaultTableModel(vectorData, columNames);
table.setModel( data );
基本上,当您想要更改数据时,不应该创建新的Swing组件。只需更改型号即可。
编辑:
查看将Interactive JTable的内容保存到.txt文件,以便在下次运行时读取该文件,以获得允许您保存/还原表的解决方案。