2008年8月12日

Swing JTable Editing without hit Enter


import javax.swing.JOptionPane;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

public class Main extends javax.swing.JFrame {

public Main() {
initComponents();
}

private void mockInputValue(int row, int column) {
String value = "test value";
JOptionPane.showMessageDialog(rootPane, "Input Value: " + value);
table.editCellAt(row, column);
TableCellEditor editor = table.getCellEditor();
editor.getTableCellEditorComponent(table, value, true, row, column);
}

private void stopCellEditingShowValue(int row, int column) {
if (table.isEditing()) {
TableModel model = table.getModel();
String value = "before stopCellEditing getValue: " + model.getValueAt(row, column);
JOptionPane.showMessageDialog(rootPane, value);
table.getCellEditor().stopCellEditing();
value = "after stopCellEditing getValue: " + model.getValueAt(row, column);
JOptionPane.showMessageDialog(rootPane, value);
}
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
table = new javax.swing.JTable();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

table.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null},
{null, null}
},
new String [] {
"Title 1", "Title 2"
}
));
jScrollPane1.setViewportView(table);

getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

pack();
}//


public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Main m = new Main();
m.setVisible(true);
int row = 0;
int column = 0;
m.mockInputValue(row, column);
m.stopCellEditingShowValue(row, column);
}
});
}

// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;
// End of variables declaration

}

沒有留言:

網誌存檔