gpt4 book ai didi

java - 双击时,jtable 数据将替换为之前的结果

转载 作者:行者123 更新时间:2023-12-02 13:33:12 29 4
gpt4 key购买 nike

我正在 Swing 中开发一个应用程序,在该应用程序中,我扫描一些文件中的数据并将其显示在 UI 上的 JTable 中,当我双击行时,相应的文件就会打开。 “扫描”按钮的代码如下,

JButton btnStart = new JButton("Scan");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {

File dir = new File(chooser.getSelectedFile(), chooser.getSelectedFile().getAbsolutePath());
System.out.println("dir " + dir);
File[] firstLevelFiles = chooser.getSelectedFile().listFiles();
if (firstLevelFiles != null && firstLevelFiles.length > 0) {
for (File aFile : firstLevelFiles) {
if (aFile.isDirectory()) {
listDirectory(aFile.getAbsolutePath(), level + 1);
} else {
fileExt=aFile.getName().substring(aFile.getName().lastIndexOf(".")+1);
for (String string : selectedExt) {
if(string.equals(fileExt)){
fileList.add(aFile);
}
}
}
}
}

MyFileReader myFileReader=new MyFileReader();

new Thread(new Runnable(){
@Override
public void run(){

fileMap=myFileReader.fileReader(fileList, filepath);
if(!fileMap.isEmpty()){
SwingUtilities.invokeLater(new Runnable(){
@Override public void run(){

int tableSize=myFileReader.getTableSize();

tableData=new String[tableSize][4];
Iterator it = fileMap.entrySet().iterator();
int j=0;
int k=0;
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
List<String> ls = (List<String>) pair.getValue();
for (int i = 1; i <= ls.size(); i++) {
if(k==1){
k++;
i--;
continue;
}

tableData[j][1]=pair.getKey().toString();
tableData[j][k]=ls.get(i-1).toString();
k++;
if(k==4){
k=0;
j++;
}
}
}
model = new DefaultTableModel(tableData, columns)
{
public boolean isCellEditable(int row, int column)
{
return false;//This causes all cells to be not editable
}
};

table = new JTable(model);
table.setEnabled(true);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.LIGHT_GRAY);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setCellSelectionEnabled(true);
table.getColumnModel().getColumn(0).setPreferredWidth(5);
table.getColumnModel().getColumn(1).setPreferredWidth(400);
table.getColumnModel().getColumn(2).setPreferredWidth(5);
table.getColumnModel().getColumn(3).setPreferredWidth(5);

pane = new JScrollPane(table);
pane.setBounds(70,250,1000,300);
pane.setVisible(true);
contentPane.add(pane,BorderLayout.CENTER);

table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int columnChk=table.getSelectedColumn();
if (columnChk == 1) {
String fpath = getSelectedData(table);

try {
Runtime runtime = Runtime.getRuntime();
runtime.exec("explorer " + fpath);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
scanProg.setText("Scanning Completed");
}
});
}else {
scanProg.setText("No codebase found");
}
}
}).start();
}


}
});
btnStart.setBounds(324, 154, 89, 23);
contentPane.add(btnStart);

现在,当我在不关闭框架的情况下重新扫描文件时,新数据将填充到 JTable 中,但是一旦我双击任何行,它的数据就会被替换为前一个数据,这是不需要的。我想从表中清除数据来自事先扫描。 已经尝试过 JTable.removeall() 但没有帮助。 Screenshot of table is here有什么想法吗?

最佳答案

table = new JTable(model);    

不要不断创建新组件。相反,您可以使用以下方法更新现有表的数据:

table.setModel( model );

.I want to clear data from table from privious scan

或者您可以使用现有模型,而不是创建全新的 DefaultTableModel。

您可以使用以下方法清除数据:

model.setRowCount( 0 );

然后您可以使用以下方法添加数据行:

model.addRow(...);

关于java - 双击时,jtable 数据将替换为之前的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43109684/

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