gpt4 book ai didi

java - 将一个 JTable 替换为其他(导入)Java

转载 作者:行者123 更新时间:2023-11-30 02:50:12 25 4
gpt4 key购买 nike

我正在尝试用存储在文件中的相同特征的其他 Jtable 替换已定义为对应 vector 数据 vector 的 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 更改为新的 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 组件。只需更改模型即可。

编辑:

查看Saving content of Interactive JTable to .txt file to read it upon next run寻找允许您保存/恢复表的解决方案。

关于java - 将一个 JTable 替换为其他(导入)Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38938346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com