swing_AbstractTableModel 创建表格

Posted 千彧

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swing_AbstractTableModel 创建表格相关的知识,希望对你有一定的参考价值。

 

 1 import javax.swing.table.AbstractTableModel;  
 2 
 3 public class MyTable extends AbstractTableModel {  
 4     /** 
 5      *  
 6      */  
 7     private static final long serialVersionUID = 1L;  
 8     Object[][] p = {  
 9             { "11", "Taipei", new Integer(66), new Integer(32),  
10                     new Integer(98), new Boolean(false), new Boolean(false) },  
11             { "22", "ChiaYi", new Integer(85), new Integer(69),  
12                     new Integer(154), new Boolean(true), new Boolean(false) },
13             { "33", "ChiaYi", new Integer(85), new Integer(69),  
14                         new Integer(154), new Boolean(true), new Boolean(false) },
15             { "44", "ChiaYi", new Integer(85), new Integer(69),  
16                             new Integer(154), new Boolean(true), new Boolean(false) }
17     };  
18     String[] n = { "姓名", "居住地", "语文", "数学", "总分", "及格" };  
19   
20     public int getColumnCount() {  
21         return n.length;  
22     }  
23   
24     public int getRowCount() {  
25         return p.length;  
26     }  
27   
28     public String getColumnName(int col) { 
29        return n[col];  
30     }  
31   
32     public Object getValueAt(int row, int col) {  
33         return p[row][col];  
34      //   return null;
35     }  
36   
37     public Class getColumnClass(int c) {  
38         return getValueAt(0, c).getClass(); 
39       //  return null;
40     }  
41   
42     public boolean isCellEditable(int rowIndex, int columnIndex) {  
43         return true;  
44     }  
45   
46     public void setValueAt(Object value, int row, int col) {  
47         p[row][col] = value;  
48        fireTableCellUpdated(row, col); // 该方法触发TableModelEvent 事件,tableChanged()会进行相应的处理  
49     }  
50   
51     public void mySetValueAt(Object value, int row, int col) {  
52         p[row][col] = value;  
53         //fireTableCellUpdated(row, col);  //此处不能有,否则会陷入对自身的调用,导致内存溢出。  
54     }  
55 }  

 

 1 import java.awt.BorderLayout;  
 2 import java.awt.Dimension;  
 3 import java.awt.event.WindowAdapter;  
 4 import java.awt.event.WindowEvent;   
 5 import javax.swing.DefaultCellEditor;  
 6 import javax.swing.JComboBox;  
 7 import javax.swing.JFrame;  
 8 import javax.swing.JLabel;  
 9 import javax.swing.JScrollPane;  
10 import javax.swing.JTable;  
11 import javax.swing.event.TableModelEvent;  
12 import javax.swing.event.TableModelListener;  
13   
14 public class TableEventHandle implements TableModelListener {  
15     JTable table = null;  
16     MyTable mt = null;  //tableModel
17     JLabel label = null;  
18   
19     public TableEventHandle() {  
20         JFrame f = new JFrame();  
21         mt = new MyTable();  
22         mt.addTableModelListener(this);  
23         table = new JTable(mt);
24         
25         JComboBox c = new JComboBox();  
26         c.addItem("Taipei");  
27         c.addItem("ChiaYi");  
28         c.addItem("HsinChu");  
29         table.getColumnModel().getColumn(1)  
30                 .setCellEditor(new DefaultCellEditor(c)); 
31         
32         table.setPreferredScrollableViewportSize(new Dimension(550, 100)); //设置表格默认展现视图大小 
33         
34         
35         JScrollPane s = new JScrollPane(table);  //滚动条
36         f.getContentPane().add(s, BorderLayout.CENTER);
37         
38         
39         label = new JLabel("修改字段位置:");   
40         f.getContentPane().add(label, BorderLayout.SOUTH);
41         
42         f.setTitle("TableEventHandle");  
43         f.pack();  
44         f.setVisible(true);  
45         f.addWindowListener(new WindowAdapter() {  
46             public void windowClosing(WindowEvent e) {  
47                 System.exit(0);  
48             }  
49         });  
50     } 
51     
52   @Override
53     public void tableChanged(TableModelEvent e) {  
54         int row = e.getFirstRow();  
55         int column = e.getColumn();  
56         
57         label.setText("修改字段位置:" + (row + 1) + "行" + (column + 1) + "列");  
58         
59         boolean cheat = ((Boolean) (mt.getValueAt(row, 6))).booleanValue();  
60         int grade1 = ((Integer) (mt.getValueAt(row, 2))).intValue();  
61         int grade2 = ((Integer) (mt.getValueAt(row, 3))).intValue();  
62         
63         int total = grade1 + grade2; 
64         
65         
66         
67         if (cheat) {  
68             if (total > 120)  
69                 mt.mySetValueAt(new Integer(119), row, 4); //只更新值,不触发事件。改为mt.setValueAt()也可。但会不断触发事件,java应该做了相应处理,否则会内存溢出。  
70             else  
71                 mt.mySetValueAt(new Integer(total), row, 4);  
72                 mt.mySetValueAt(new Boolean(false), row, 5);  
73         } else {  
74             if (total > 120)  
75                 mt.mySetValueAt(new Boolean(true), row, 5);  
76             else  
77                 mt.mySetValueAt(new Boolean(false), row, 5);  
78                 mt.mySetValueAt(new Integer(total), row, 4);  
79         } 
80         
81 
82         
83         table.repaint();  
84     } 
85   
86   
87   
88     public static void main(String args[]) {  
89           new TableEventHandle();  
90     }
91 
92  

 

以上是关于swing_AbstractTableModel 创建表格的主要内容,如果未能解决你的问题,请参考以下文章