Java 双击时,jtable数据将替换为以前的结果
我正在Swing中开发一个应用程序,在这个应用程序中,我扫描一些文件中的数据,并将其显示在UI上的JTable中,当我双击行时,相应的文件被打开。“扫描”按钮的代码如下:Java 双击时,jtable数据将替换为以前的结果,java,swing,jtable,double-click,mouseclick-event,Java,Swing,Jtable,Double Click,Mouseclick Event,我正在Swing中开发一个应用程序,在这个应用程序中,我扫描一些文件中的数据,并将其显示在UI上的JTable中,当我双击行时,相应的文件被打开。“扫描”按钮的代码如下: JButton btnStart = new JButton("Scan"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
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);
JButton btnStart=新JButton(“扫描”);
btnStart.addActionListener(新ActionListener(){
已执行公共无效操作(操作事件arg0){
File dir=新文件(chooser.getSelectedFile(),chooser.getSelectedFile().getAbsolutePath());
System.out.println(“dir”+dir);
File[]firstLevelFiles=chooser.getSelectedFile().listFiles();
if(firstLevelFiles!=null&&firstLevelFiles.length>0){
对于(文件:firstLevelFiles){
if(aFile.isDirectory()){
listDirectory(aFile.getAbsolutePath(),级别+1);
}否则{
fileExt=aFile.getName().substring(aFile.getName().lastIndexOf(“.”+1);
for(字符串:selectedExt){
如果(string.equals(fileExt)){
fileList.add(一个文件);
}
}
}
}
}
MyFileReader MyFileReader=新建MyFileReader();
新线程(newrunnable()){
@凌驾
公开募捐{
fileMap=myFileReader.fileReader(文件列表,文件路径);
如果(!fileMap.isEmpty()){
SwingUtilities.invokeLater(新的Runnable(){
@重写公共无效运行(){
int tableSize=myFileReader.getTableSize();
tableData=新字符串[tableSize][4];
迭代器it=fileMap.entrySet().Iterator();
int j=0;
int k=0;
while(it.hasNext()){
Map.Entry对=(Map.Entry)it.next();
List ls=(List)pair.getValue();
对于(int i=1;i
不要继续创建新组件。相反,您可以使用以下方法更新现有表的数据:
table.setModel( model );
model.setRowCount( 0 );
model.addRow(...);
.我想从私密扫描中清除表中的数据
或者,您可以使用现有模型,而不是创建全新的DefaultTableModel
您可以使用以下方法清除数据:
table.setModel( model );
model.setRowCount( 0 );
model.addRow(...);
您可以使用以下命令添加数据行:
table.setModel( model );
model.setRowCount( 0 );
model.addRow(...);
创建一个新的TableModel
,从现有的TableModel
中删除所有旧数据,发布一个可运行的示例,演示您的问题并帮助删除猜测工作。Thanx..我再次编辑了我的问题..希望它能有所帮助。1.不要在运行时添加新的JCompones,重新使用它,(Swing API-LayoutManager没有任何关于更改的通知程序,开发人员必须以编程方式通知),2.不要使用空布局,3.(正如Mad正确提到的)重用DefaultTableModel,4.您的EDT有问题