19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

20 };<br />

21<br />

22 // Create a table<br />

23 private JTable jTable1 = new JTable(rowData, columnNames);<br />

24<br />

25 // Create two spinners<br />

26 private JSpinner jspiRowHeight =<br />

27 new JSpinner(new SpinnerNumberModel(16, 1, 50, 1));<br />

28 private JSpinner jspiRowMargin =<br />

29 new JSpinner(new SpinnerNumberModel(1, 1, 50, 1));<br />

30<br />

31 // Create a checkbox<br />

32 private JCheckBox jchkShowGrid = new JCheckBox("showGrid", true);<br />

33<br />

34 // Create a <strong>com</strong>bo box<br />

35 private JComboBox jcboAu<strong>to</strong>ResizeMode = new JComboBox(new String[]{<br />

36 "AUTO_RESIZE_OFF", "AUTO_RESIZE_LAST_COLUMN",<br />

37 "AUTO_RESIZE_SUBSEQUENT_COLUMNS", "AUTO_RESIZE_NEXT_COLUMN",<br />

38 "AUTO_RESIZE_ALL_COLUMNS"});<br />

39<br />

40 public TablePropertiesDemo() {<br />

41 JPanel panel1 = new JPanel();<br />

42 panel1.add(new JLabel("rowHeight"));<br />

43 panel1.add(jspiRowHeight);<br />

44 panel1.add(new JLabel("rowMargin"));<br />

45 panel1.add(jspiRowMargin);<br />

46 panel1.add(jchkShowGrid);<br />

47<br />

48 JPanel panel2 = new JPanel();<br />

49 panel2.add(new JLabel("au<strong>to</strong>ResizeMode"));<br />

50 panel2.add(jcboAu<strong>to</strong>ResizeMode);<br />

51<br />

52 add(panel1, BorderLayout.SOUTH);<br />

53 add(panel2, BorderLayout.NORTH);<br />

54 add(new JScrollPane(jTable1));<br />

55<br />

56 // Initialize jTable1<br />

57 jTable1.setAu<strong>to</strong>ResizeMode(JTable.AUTO_RESIZE_OFF);<br />

58 jTable1.setGridColor(Color.BLUE);<br />

59 jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);<br />

60 jTable1.setSelectionBackground(Color.RED);<br />

61 jTable1.setSelectionForeground(Color.WHITE);<br />

62<br />

63 // Register and create a listener for jspiRowHeight<br />

64 jspiRowHeight.addChangeListener(new ChangeListener() {<br />

65 public void stateChanged(ChangeEvent e) {<br />

66 jTable1.setRowHeight(<br />

67 ((Integer)(jspiRowHeight.getValue())).intValue());<br />

68 }<br />

69 });<br />

70<br />

71 // Register and create a listener for jspiRowMargin<br />

72 jspiRowMargin.addChangeListener(new ChangeListener() {<br />

73 public void stateChanged(ChangeEvent e) {<br />

74 jTable1.setRowMargin(<br />

75 ((Integer)(jspiRowMargin.getValue())).intValue());<br />

76 }<br />

77 });<br />

78<br />

79 // Register and create a listener for jchkShowGrid<br />

80 jchkShowGrid.addActionListener(new ActionListener() {<br />

81 @Override<br />

82 public void actionPerformed(ActionEvent e) {<br />

83 jTable1.setShowGrid(jchkShowGrid.isSelected());<br />

84 }<br />

85 });<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!