1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import javax.swing.event.*;
5
6 public class TablePropertiesDemo extends JApplet {
7
8 private String[] columnNames =
9 {"Country", "Capital", "Population in Millions", "Democracy"};
10
11
12 private Object[][] rowData = {
13 {"USA", "Washington DC", 280, true},
14 {"Canada", "Ottawa", 32, true},
15 {"United Kingdom", "London", 60, true},
16 {"Germany", "Berlin", 83, true},
17 {"France", "Paris", 60, true},
18 {"Norway", "Oslo", 4.5, true},
19 {"India", "New Delhi", 1046, true}
20 };
21
22
23 private JTable jTable1 = new JTable(rowData, columnNames);
24
25
26 private JSpinner jspiRowHeight =
27 new JSpinner(new SpinnerNumberModel(16, 1, 50, 1));
28 private JSpinner jspiRowMargin =
29 new JSpinner(new SpinnerNumberModel(1, 1, 50, 1));
30
31
32 private JCheckBox jchkShowGrid = new JCheckBox("showGrid", true);
33
34
35 private JComboBox jcboAutoResizeMode = new JComboBox(new String[]{
36 "AUTO_RESIZE_OFF", "AUTO_RESIZE_LAST_COLUMN",
37 "AUTO_RESIZE_SUBSEQUENT_COLUMNS", "AUTO_RESIZE_NEXT_COLUMN",
38 "AUTO_RESIZE_ALL_COLUMNS"});
39
40 public TablePropertiesDemo() {
41 JPanel panel1 = new JPanel();
42 panel1.add(new JLabel("rowHeight"));
43 panel1.add(jspiRowHeight);
44 panel1.add(new JLabel("rowMargin"));
45 panel1.add(jspiRowMargin);
46 panel1.add(jchkShowGrid);
47
48 JPanel panel2 = new JPanel();
49 panel2.add(new JLabel("autoResizeMode"));
50 panel2.add(jcboAutoResizeMode);
51
52 add(panel1, BorderLayout.SOUTH);
53 add(panel2, BorderLayout.NORTH);
54 add(new JScrollPane(jTable1));
55
56
57 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
58 jTable1.setGridColor(Color.BLUE);
59 jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
60 jTable1.setSelectionBackground(Color.RED);
61 jTable1.setSelectionForeground(Color.WHITE);
62
63
64 jspiRowHeight.addChangeListener(new ChangeListener() {
65 public void stateChanged(ChangeEvent e) {
66 jTable1.setRowHeight(
67 ((Integer)(jspiRowHeight.getValue())).intValue());
68 }
69 });
70
71
72 jspiRowMargin.addChangeListener(new ChangeListener() {
73 public void stateChanged(ChangeEvent e) {
74 jTable1.setRowMargin(
75 ((Integer)(jspiRowMargin.getValue())).intValue());
76 }
77 });
78
79
80 jchkShowGrid.addActionListener(new ActionListener() {
81 @Override
82 public void actionPerformed(ActionEvent e) {
83 jTable1.setShowGrid(jchkShowGrid.isSelected());
84 }
85 });
86
87
88 jcboAutoResizeMode.addActionListener(new ActionListener() {
89 @Override
90 public void actionPerformed(ActionEvent e) {
91 String selectedItem =
92 (String)jcboAutoResizeMode.getSelectedItem();
93
94 if (selectedItem.equals("AUTO_RESIZE_OFF"))
95 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
96 else if (selectedItem.equals("AUTO_RESIZE_LAST_COLUMN"))
97 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
98 else if (selectedItem.equals
99 ("AUTO_RESIZE_SUBSEQUENT_COLUMNS"))
100 jTable1.setAutoResizeMode(
101 JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
102 else if (selectedItem.equals("AUTO_RESIZE_NEXT_COLUMN"))
103 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
104 else if (selectedItem.equals("AUTO_RESIZE_ALL_COLUMNS"))
105 jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
106 }
107 });
108 }
109
110 public static void main(String[] args) {
111 TablePropertiesDemo applet = new TablePropertiesDemo();
112 JFrame frame = new JFrame();
113 //EXIT_ON_CLOSE == 3
114 frame.setDefaultCloseOperation(3);
115 frame.setTitle("TablePropertiesDemo");
116 frame.getContentPane().add(applet, BorderLayout.CENTER);
117 applet.init();
118 applet.start();
119 frame.setSize(400,320);
120 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
121 frame.setLocation((d.width - frame.getSize().width) / 2,
122 (d.height - frame.getSize().height) / 2);
123 frame.setVisible(true);
124 }
125 }