如何保存对 jtable 所做的更改
how to save changes made to a jtable
我创建了一个 jtable,它承载一个单元格,它是一个组合框,我可以让组合框填充 jtable,但是一旦我重新启动程序,单元格就会完全变空。我需要一种方法来保存更改,以便在程序重新启动后所做的更改将保留。(注意:我已经为此搜索了解决方案,但没有进展。)
String path ="C:\Users\GrantAJ\Documents\Comment Matrix";
File folder = new File(path);'File[] listOfFiles= folder.listFiles();
////// filters file objects in java to populate jcombobox with just the name /////
List<String> fileNames = new ArrayList<String>();
for(File files1: listOfFiles){
if(files1.isFile()){
fileNames.add(files1.getName());
}else if (files1.isDirectory())
{ System.out.print("Directory : );
}
final JComboBox jList1 = new JComboBox(listOfFiles);
jList1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, files1.getName());
}
});
TableColumn col = jTable_Files_Name.getColumnModel().getColumn(4);
col.setCellEditor(new DefaultCellEditor(jList1));
}
Object[] row = new Object[6];
// fill the rows and columns
row[0] = file.getName();
row[1] = file.getAbsolutePath();
row[2]= dt;
row[3]=sb.toString();
row[4]=files1.getName();
row[5]=hostname;
model.addRow(new Object []{row[0],row[1],row[2],row[3],"",row[5]});
}
}catch(Exception e){e.printStackTrace();}
尝试构建一个 "AbstractTableModel"。
此 table 模型将包含您的数据并可以保存。
要加载模型,请调用 new JTable(your_model)。
您可以在这里查看更多帮助:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
祝你好运。
我创建了一个 jtable,它承载一个单元格,它是一个组合框,我可以让组合框填充 jtable,但是一旦我重新启动程序,单元格就会完全变空。我需要一种方法来保存更改,以便在程序重新启动后所做的更改将保留。(注意:我已经为此搜索了解决方案,但没有进展。)
String path ="C:\Users\GrantAJ\Documents\Comment Matrix";
File folder = new File(path);'File[] listOfFiles= folder.listFiles();
////// filters file objects in java to populate jcombobox with just the name /////
List<String> fileNames = new ArrayList<String>();
for(File files1: listOfFiles){
if(files1.isFile()){
fileNames.add(files1.getName());
}else if (files1.isDirectory())
{ System.out.print("Directory : );
}
final JComboBox jList1 = new JComboBox(listOfFiles);
jList1.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(null, files1.getName());
}
});
TableColumn col = jTable_Files_Name.getColumnModel().getColumn(4);
col.setCellEditor(new DefaultCellEditor(jList1));
}
Object[] row = new Object[6];
// fill the rows and columns
row[0] = file.getName();
row[1] = file.getAbsolutePath();
row[2]= dt;
row[3]=sb.toString();
row[4]=files1.getName();
row[5]=hostname;
model.addRow(new Object []{row[0],row[1],row[2],row[3],"",row[5]});
}
}catch(Exception e){e.printStackTrace();}
尝试构建一个 "AbstractTableModel"。 此 table 模型将包含您的数据并可以保存。
要加载模型,请调用 new JTable(your_model)。
您可以在这里查看更多帮助:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data
祝你好运。