/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import javax.swing.*; 

2. import javax.swing.table.*; 

3. import java.awt.*; 

4. import java.awt.event.*; 

5. import java.net.URL

6.  

7. public class Table 

8.     extends JApplet 

9.     implements ActionListener, MouseListener 

10.

11.   JTable tbl ; 

12.   JScrollPane sp ; 

13.  

14.   public void actionPerformed ( ActionEvent ev ) 

15.   { 

16.     if ( curcol == 3 ) return

17.     String str = tf.getText().trim() ; 

18.     if ( str.length( ) == 0 ) return

19.  

20.     tbl.setValueAt ( str , currow , curcol ); 

21.     tbl.repaint(); 

22.   } 

23.   private void onetobeamdown() 

24.   { 

25.     currow = tbl.getSelectedRow( ) ; 

26.     curcol = tbl.getSelectedColumn( ) ; 

27.  

28.     Object ob = tbl.getValueAt 

29.      ( currow , curcol  ); 

30.  

31.     String str = ""

32.     if ( ob instanceof String

33.       str = (String) ob ; 

34.  

35.     tf.setText ( str ) ; 

36.   } 

37.  

38.   public void mouseClicked ( MouseEvent ev ) 

39.   { 

40.     onetobeamdown ( ) ; 

41.  

42.     lb1.setText( "Row : " + currow  ); 

43.     lb2.setText( "Column : " + curcol ); 

44.   } 

45.  

46.   public void init() 

47.   { 

48.     try

49.     URL u = new URL ( getCodeBase( ), "tpic.gif" ) ; 

50.     imic = new ImageIcon ( u ) ; 

51.     } 

52.     catch ( Exception e ) 

53.     { System.err.println ( e ); } 

54.  

55.     tbl = new JTable ( new OurTableModel()  ); 

56.     tbl.setSelectionMode ( ListSelectionModel.SINGLE_SELECTION ) ; 

57.     tbl.setRowSelectionAllowed ( false ) ; 

58.     tbl.setColumnSelectionAllowed ( false ) ; 

59.     tbl.setRowHeight ( 50 ) ; 

60.  

61.     sp = new JScrollPane ( tbl ) ; 

62.     sp.setPreferredSize (new Dimension(450, 200)); 

63.  

64.     cp = getContentPane ( ) ; 

65.     cp.setLayout ( new FlowLayout( )  ); 

66.  

67.     cp.add ( sp ) ; 

68.  

69.     cp.add ( lb1 ); 

70.     cp.add ( lb2 ); 

71.     tf = new JTextField ( 20 ); 

72.     bt = new JButton ( "JLessons button" ) ; 

73.     cp.add ( tf ) ; 

74.     cp.add ( bt ); 

75.  

76.     tbl.addMouseListener ( this ) ; 

77.     bt.addActionListener ( this ) ; 

78.     tf.addActionListener ( this ) ; 

79.  

80.     cp.setBackground( Color.yellow ); 

81.   } 

82. //////inner class : ////// 

83.  

84. class OurTableModel 

85.     extends AbstractTableModel 

86.

87.    final String[]  headers = 

88.     { "First", "Last", "Telephone", "Pic" }; 

89.  

90.    Object[][] data = { 

91.       { "Hadto", "Tattoo", "0466533225", imic }, 

92.       { "Nononono", "Yes", "0894456665", imic }, 

93.       { "Kwark", "Oolerwahper", "065569998", imic }, 

94.       { "Mick", "Themouse", "0421133322", imic } 

95.    }; 

96.  

97.   public Object getValueAt (int row, int col) 

98.   {  return data[row][col]; } 

99.  

100.   public void setValueAt (Object ob, int row, int col) 

101.   {  data[row][col] = ob ; } 

102.  

103.   public Class getColumnClass(int c) 

104.   {  return getValueAt( 1, c ).getClass(); } 

105.  

106.   public boolean isCellEditable(int row, int col) 

107.   {  return getColumnClass(col) == String.class;} 

108.  

109.   public int getColumnCount() 

110.   {  return headers.length;   } 

111.  

112.   public int getRowCount() 

113.   {  return data.length;   } 

114.  

115.   public String getColumnName(int col) 

116.   {  return headers[col];   } 

117.  

118. }  // end inner class 

119. ////////////////////////// 

120.  

121.   JTextField tf ; 

122.   JButton bt ; 

123.   Container cp ; 

124.   ImageIcon imic ; 

125.   int currow = 1, curcol = 1

126.   JLabel lb1=new JLabel(); 

127.   JLabel lb2=new JLabel(); 

128.  

129.   public void mouseEntered(MouseEvent ev) {} 

130.   public void mouseExited(MouseEvent ev) {} 

131.   public void mousePressed(MouseEvent ev) {} 

132.   public void mouseReleased(MouseEvent ev){} 

133.

134.