日期:2014-05-20 浏览次数:20713 次
package test; import java.applet.Applet; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; import javax.swing.table.TableModel; public class Test extends Applet implements ActionListener{ JTable jTablesecond; public Test() { Object[][] cells = { { Boolean.FALSE, "必选", "", "" }, { Boolean.FALSE, "可选", "", "" }, { Boolean.FALSE, "必选", "", "" }, { Boolean.FALSE, "可选", "", "" }, { Boolean.FALSE, "必选", "", "" }, }; String[] columnNames = { "选择", "重要级", "文件名", "说明" }; DefaultTableModel model = new DefaultTableModel(cells, columnNames){ public Class getColumnClass(int columnIndex) { return columnIndex == 0 ? Boolean.class : String.class; } }; jTablesecond = new JTable(model); JScrollPane sp = new JScrollPane(jTablesecond); // 设置Table列宽 TableColumnModel tcm = jTablesecond.getColumnModel(); int columnCount = jTablesecond.getColumnCount(); TableColumn[] tc = new TableColumn[columnCount]; tc[0] = tcm.getColumn(0); tc[0].setPreferredWidth(50); tc[1] = tcm.getColumn(1); tc[1].setPreferredWidth(75); tc[2] = tcm.getColumn(2); tc[2].setPreferredWidth(75); tc[3] = tcm.getColumn(3); tc[3].setPreferredWidth(240); jTablesecond.getTableHeader().setReorderingAllowed(false); // 设置头不可变 jTablesecond.getTableHeader().setResizingAllowed(false); // 设置列宽不可变 jTablesecond.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// 设置为不自动调整大小 jTablesecond.setBackground(Color.white); add(new JScrollPane(jTablesecond)); // 显示表头 JButton button = new JButton("aa"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent e) { TableModel model = jTablesecond.getModel(); int count = model.getRowCount(); for(int i = 0 ; i < count ; i++){ boolean checked = (Boolean)model.getValueAt(i, 0); if(checked){ System.out.println(i); } } } }