有一个简单的JTable,试图在编辑后获取最新的列值。
"将文件添加到表格"按钮将从JTextField加载文件名和路径到JTable
"从表中获取值"按钮将(尝试)获取JTable中的所有列值。
在last column edited之后,按下"从表中获取值"按钮只返回前三列但不返回已编辑的列,最后一列不返回任何内容!
如何获取编辑后的列值?
代码
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.nio.file.Path;
import java.util.Vector;
public class viewTable {
private JFrame frame;
private JTextField textField;
private JTable table;
private static DefaultTableModel model;
private static File file;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
viewTable window = new viewTable();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public viewTable() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.getContentPane().setLayout(null);
textField = new JTextField("C:\\Temp\\Temp.docx");
textField.setBounds(159, 23, 393, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnAddFileTo = new JButton("Add File to Table");
btnAddFileTo.setBounds(383, 54, 165, 23);
frame.getContentPane().add(btnAddFileTo);
btnAddFileTo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
addFileToTable();
}
private void addFileToTable() {
file = new File(textField.getText());
final Object[] rowEntries = new Object[4];
int tableIndex = table.getRowCount();
tableIndex = tableIndex + 1;
if (tableIndex <= 9){rowEntries[0] = "0"+tableIndex+".";}
else {rowEntries[0] = tableIndex+".";}
Path fileNameAlone = file.toPath();
fileNameAlone = fileNameAlone.getFileName();
rowEntries[1] = fileNameAlone;
rowEntries[2] = file;
rowEntries[3] = "";
model.addRow(rowEntries);
}
});
table = new JTable(5,4);
Object[] columnNames = { "Sl.no.", "File", "Path/Name", "Page no. to be added" };
model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
table.setModel(model);
JScrollPane scrollTable = new JScrollPane(table);
scrollTable.setBounds(111, 100, 464, 213);
frame.getContentPane().add(scrollTable);
JButton btnNewButton = new JButton("Get Values from the table");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel model1 = (DefaultTableModel)table.getModel();
Vector pageNoData = model1.getDataVector();
String pageNo;
pageNo = getPageNoValueFromTable(pageNoData, table);
System.out.println("pageNo : "+pageNo);
}
});
btnNewButton.setBounds(319, 349, 256, 23);
frame.getContentPane().add(btnNewButton);
frame.setBounds(100, 100, 725, 494);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static String getPageNoValueFromTable(Vector itemData, JTable table){
table.setRowSelectionInterval(0, 0);
table.repaint();
System.out.println(table.getModel().getValueAt(0, 3));
//int myListCount = 0;
DefaultTableModel model1 = (DefaultTableModel)table.getModel();
Vector pageNoData = model1.getDataVector();
String mySelectedPageNo = null;
for(int i=0; i<pageNoData.size(); i++){
Vector getItem = (Vector) pageNoData.get(i);
for (int j=0; j<getItem.size(); j++){
if (j == 3)
mySelectedPageNo = getItem.get(j).toString();
System.out.println("getItem : "+getItem.get(j).toString());}
}
return mySelectedPageNo;
}
}
输出
getItem : 01.
getItem : Temp.docx
getItem : C:\Temp\Temp.docx
getItem : ?
pageNo : ?
请告诉我这方面的指示!感谢
答案 0 :(得分:6)
在JTable中编辑单元格时,表格不知道用户何时完成单元格的编辑。因此,用户有责任告诉表何时停止编辑。这通常由以下人员完成:
使用回车键或Tab键到下一个单元格或用鼠标单击另一个单元格
您需要向ActionListener
添加一些代码if (table.isEditing())
table.getCellEditor().stopCellEditing();
我的书签here中的一篇好文章: