1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
import java.awt.HeadlessException;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 20-Jun-2006
* Time: 20:27:10
* To change this template use File | Settings | File Templates.
*/
public class TableStopEditingTest extends JFrame {
public TableStopEditingTest() throws HeadlessException {
JTable myTable = new JTable(new MyTableModel());
myTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
add(new JScrollPane(myTable));
add(new JButton("Click me :-) "), BorderLayout.SOUTH);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
TableStopEditingTest t = new TableStopEditingTest();
t.setSize(400, 300);
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setVisible(true);
}
});
}
}
class MyTableModel implements TableModel {
private Object[][] datas = new Object[][] {
{"Col00", "Col01", "Col02"},
{"Col10", "Col11", "Col12"},
{"Col20", "Col21", "Col22"},
{"Col30", "Col31", "Col33"},
{"Col40", "Col41", "Col42"},
{"Col50", "Col51", "Col52"}
};
private String[] s = new String[]{"Col", "Col", "Col"};
public int getRowCount() {
return datas.length; //To change body of implemented methods use File | Settings | File Templates.
}
public int getColumnCount() {
return datas[0].length; //To change body of implemented methods use File | Settings | File Templates.
}
public String getColumnName(int columnIndex) {
return s[columnIndex]; //To change body of implemented methods use File | Settings | File Templates.
}
public Class<?> getColumnClass(int columnIndex) {
return String.class; //To change body of implemented methods use File | Settings | File Templates.
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
// everything is always editable...
return true; //To change body of implemented methods use File | Settings | File Templates.
}
public Object getValueAt(int rowIndex, int columnIndex) {
return datas[rowIndex][columnIndex]; //To change body of implemented methods use File | Settings | File Templates.
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
datas[rowIndex][columnIndex] = aValue;
//To change body of implemented methods use File | Settings | File Templates.
}
public void addTableModelListener(TableModelListener l) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void removeTableModelListener(TableModelListener l) {
//To change body of implemented methods use File | Settings | File Templates.
}
} |