01: package snow.sortabletable;
02:
03: import javax.swing.*;
04: import javax.swing.event.*;
05: import java.awt.*;
06: import java.awt.event.*;
07: import javax.swing.table.*;
08: import java.util.*;
09:
10: public class TableKeyBehaviourTest extends JFrame {
11: TestModel testModel = new TestModel();
12: JTable table = new JTable();
13:
14: public TableKeyBehaviourTest() {
15: super ("Test");
16: super .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17: setLayout(new BorderLayout());
18:
19: add(new JScrollPane(table), BorderLayout.CENTER);
20:
21: SortableTableModel stm = new SortableTableModel(testModel, 0,
22: true);
23: table.setModel(stm);
24: stm.installGUI(table);
25:
26: table.setDefaultEditor(Double.class,
27: new NumberTableCellEditor());
28: table.setDefaultRenderer(Object.class, new CellRenderer());
29:
30: table.setColumnSelectionAllowed(false);
31: table.setRowSelectionAllowed(false);
32:
33: setSize(800, 800);
34: setVisible(true);
35: }
36:
37: public static void main(String[] ignored) {
38: new TableKeyBehaviourTest();
39: }
40:
41: class CellRenderer extends DefaultTableCellRenderer {
42: @Override
43: public Component getTableCellRendererComponent(JTable table,
44: Object value, boolean isSelected, boolean hasFocus,
45: int row, int column) {
46: Component sup = super.getTableCellRendererComponent(table,
47: value, isSelected, hasFocus, row, column);
48:
49: if (hasFocus) {
50: sup.setForeground(table.getSelectionForeground());
51: sup.setBackground(table.getSelectionBackground());
52: } else {
53: sup.setForeground(table.getForeground());
54: sup.setBackground(table.getBackground());
55: }
56:
57: return sup;
58: }
59:
60: }
61:
62: }
|