Reputation: 891
Have a simple JTable, from that trying to get the latest column value after edited.
"Add File to Table" button will load the file name and path from JTextField into the JTable
"Get Values from the table" button would (trying) get all the column values from the JTable.
After the last column edited and press the "Get Values from the table" button is returns only first three columns but not the edited column, the last column returns nothing!.
How to get the edited column value please?
CODE
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;
}
}
Output:
getItem : 01.
getItem : Temp.docx
getItem : C:\Temp\Temp.docx
getItem : ?
pageNo : ?
Please give me directions on this! Thanks
Upvotes: 1
Views: 984
Reputation: 2924
When editing a cell in a JTable the table doesn’t know when a user is finished editing the cell. Therefore it is the users responsibility to tell the table when to stop editing. This is normally done by:
using the enter key or tabbing to the next cell or clicking on another cell with the mouse
You need to add some code to the ActionListener
if (table.isEditing())
table.getCellEditor().stopCellEditing();
A good article from my bookmarks here :
Upvotes: 6