java swt代码demo下载地址:http://www.zuidaima.com/share/kswt-p1-s1.htm
项目用到一个需求就是在Jtable单元格中加复选框Jcheckbox或者Jbutton,在网上终于找到了解决的办法。可惜,加两个Jbutton的问题还没有解决。
先记下如何在如何在Jtable单元格中加复选框Jcheckbox和JComboBox。
第一种:自定义自己的TableModel类。
这种方法核心是需要实现getColumnClass()方法,只要覆盖tablemodel的getColumnClass返回一个boolean的class, jtable会自动画一个Jcheckbox给你。说直白一些就是你返回一个boolean类型的值,它会自动在Jtable中画一个checkBox。true选示被选中,false表示未选中。
package com; import javax.swing.JFrame; import javax.swing.JComboBox; import javax.swing.JCheckBox; import javax.swing.JTextField; import java.awt.Container; import javax.swing.table.AbstractTableModel; import javax.swing.JTable; import javax.swing.JScrollPane; import javax.swing.DefaultCellEditor; import javax.swing.table.TableColumn; @SuppressWarnings("serial") public class AbstractTableModelTest extends JFrame { private JTable table = null; private JScrollPane jsp = null; private JComboBox cob = null; private JCheckBox ckb = null; private JTextField txt = null; public AbstractTableModelTest() { this.setTitle("JTable的ListSelectionModeld的监听事件"); this.setSize(400, 360); // 获取窗体的内容面板 Container container = this.getContentPane(); MyAbstractTableModel1 myModel = new MyAbstractTableModel1(); // JTable table = new JTable(myModel); // 获得表格的表格列类 TableColumn tc1 = table.getColumnModel().getColumn(2); TableColumn tc2 = table.getColumnModel().getColumn(4); TableColumn tc3 = table.getColumnModel().getColumn(5); // 实例化JCheckBox ckb = new JCheckBox(); tc1.setCellEditor(new DefaultCellEditor(ckb)); // 实例化JComboBox cob = new JComboBox(); cob.addItem("HN"); cob.addItem("HB"); cob.addItem("BJ"); tc2.setCellEditor(new DefaultCellEditor(cob)); // 实例化JTextField txt = new JTextField(""); txt.setSize(100, 26); tc3.setCellEditor(new DefaultCellEditor(txt)); // JScrollPane jsp = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); container.add(jsp); // 获得自定义的抽象表格模型 this.setLocationRelativeTo(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public static void main(String[] args) { new AbstractTableModelTest(); } } @SuppressWarnings("serial") class MyAbstractTableModel1 extends AbstractTableModel { // 定义表头数据 String[] head = { "ID", "NAME", "SEX(boy)", "AGE", "ADDRESS", "DEMO" }; // 创建类型数组 // Class[] // typeArray={Object.class,Object.class,Boolean.class,int.class,Object.class,Object.class}; // 定义表的内容数据 Object[] data1 = { "200913420125", "SUMMER", new Boolean(true), new Integer(20), "1", "NULL" }; Object[] data2 = { "200913420124", "WULEI", new Boolean(true), new Integer(20), "2", "NULL" }; Object[] data3 = { "200913420125", "BOOK", new Boolean(false), new Integer(20), "3", "NULL" }; Object[] data4 = { "200913420125", "CUP", new Boolean(true), new Integer(20), "4", "NULL" }; Object[] data5 = { "200913420125", "MOUSE", new Boolean(true), new Integer(20), "5", "NULL" }; // 定义表格每一列的数据类型 Class[] typeArray = { Object.class, Object.class, Boolean.class, Integer.class, JComboBox.class, Object.class }; Object[][] data = { data1, data2, data3, data4, data5 }; // 获得表格的列数 public int getColumnCount() { return head.length; } // 获得表格的行数 public int getRowCount() { return data.length; } // 获得表格的列名称 @Override public String getColumnName(int column) { return head[column]; } // 获得表格的单元格的数据 public Object getValueAt(int rowIndex, int columnIndex) { return data[rowIndex][columnIndex]; } // 使表格具有可编辑性 @Override public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } // 替换单元格的值 @Override public void setValueAt(Object aValue, int rowIndex, int columnIndex) { data[rowIndex][columnIndex] = aValue; fireTableCellUpdated(rowIndex, columnIndex); } // 实现了如果是boolean自动转成JCheckbox /* * 需要自己的celleditor这么麻烦吧。jtable自动支持Jcheckbox, * 只要覆盖tablemodel的getColumnClass返回一个boolean的class, jtable会自动画一个Jcheckbox给你, * 你的value是true还是false直接读table里那个cell的值就可以 */ public Class getColumnClass(int columnIndex) { return typeArray[columnIndex];// 返回每一列的数据类型 } }
第二种方法:自定义表格渲染和表格编辑,使其拥有相应的形状和事件。
1,自定义表格编辑
主要是实现getTableCellEditorComponent()方法,强制返回JcheckBox对象
package com; import java.awt.Component; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.DefaultCellEditor; import javax.swing.JCheckBox; import javax.swing.JTable; @SuppressWarnings("serial") class CheckBoxEditor extends DefaultCellEditor implements ItemListener { private JCheckBox button; public CheckBoxEditor(JCheckBox checkBox) { super(checkBox); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { if (value == null) return null; button = (JCheckBox) value; button.addItemListener(this); return (Component) value; } public Object getCellEditorValue() { button.removeItemListener(this); return button; } public void itemStateChanged(ItemEvent e) { super.fireEditingStopped(); } }
主要是实现getTableCellRendererComponent()方法,如果不自义表格将不会看到JcheckBox效果。
package com; import java.awt.Component; import javax.swing.JTable; import javax.swing.table.TableCellRenderer; class CheckBoxRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (value == null) return null; return (Component) value; } }
package com; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.event.TableModelEvent; import javax.swing.table.DefaultTableModel; public class Tables implements MouseListener { JTable table = new JTable(); public Tables() { JFrame frame = new JFrame("sjh"); frame.setLayout(null); table = this.gettable(); table.addMouseListener(this); JScrollPane src = new JScrollPane(table); src.setBounds(0, 0, 400, 200); frame.setSize(new Dimension(400, 200)); frame.add(src); frame.setVisible(true); } public JTable gettable() { DefaultTableModel dm = new DefaultTableModel(); dm.setDataVector(new Object[][] { { new JCheckBox("111"), new JCheckBox("111"), new JCheckBox("111"), new JCheckBox("111"), new JCheckBox("111"), new JCheckBox("111") }, { new JCheckBox("222"), new JCheckBox("222"), new JCheckBox("222"), new JCheckBox("222"), new JCheckBox("222"), new JCheckBox("222") }, { new JCheckBox("333"), new JCheckBox("333"), new JCheckBox("333"), new JCheckBox("333"), new JCheckBox("333"), new JCheckBox("333") }, }, new Object[] { "选择", "结果物", "说明", "类型", "发包要求文档", "操作" }); JTable table = new JTable(dm) { public void tableChanged(TableModelEvent e) { super.tableChanged(e); repaint(); } }; table.getColumn("选择").setCellEditor( new CheckBoxEditor(new JCheckBox())); table.getColumn("结果物").setCellEditor( new CheckBoxEditor(new JCheckBox())); table.getColumn("说明").setCellEditor( new CheckBoxEditor(new JCheckBox())); table.getColumn("类型").setCellEditor( new CheckBoxEditor(new JCheckBox())); table.getColumn("发包要求文档").setCellEditor( new CheckBoxEditor(new JCheckBox())); table.getColumn("操作").setCellEditor( new CheckBoxEditor(new JCheckBox())); // // table.getColumn("选择").setCellRenderer(new RadioButtonRenderer()); // table.getColumn("选择").setCellRenderer(new CheckBoxRenderer()); // table.setCellEditor(new CheckButtonEditor(new JCheckBox ())); table.getColumn("选择").setCellRenderer(new CheckBoxRenderer()); table.getColumn("结果物").setCellRenderer(new CheckBoxRenderer()); table.getColumn("说明").setCellRenderer(new CheckBoxRenderer()); table.getColumn("类型").setCellRenderer(new CheckBoxRenderer()); table.getColumn("发包要求文档").setCellRenderer(new CheckBoxRenderer()); table.getColumn("操作").setCellRenderer(new CheckBoxRenderer()); return table; } public static void main(String args[]) { new Tables(); } public void mouseClicked(MouseEvent arg0) { } public void mouseEntered(MouseEvent arg0) { } public void mouseExited(MouseEvent arg0) { } public void mousePressed(MouseEvent arg0) { } public void mouseReleased(MouseEvent arg0) { } }
最终的效果图如图.
它既有JcheckBox的事件,也有JcheckBox的形状。
同理加入Jbutton也是如
/* * TableButton3.java * * Created on __DATE__, __TIME__ */ package com; import java.util.Date; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; /** * * @author __USER__ */ public class TableButton3 extends javax.swing.JFrame { public TableButton3() { String[] columnNames = { "Date ", "String ", "Integer ", "Number ", " " }; Object[][] data = { { new Date(), "A ", new Integer(1), new Double(5.1), "Delete0 " }, { new Date(), "B ", new Integer(2), new Double(6.2), "Delete1 " }, { new Date(), "C ", new Integer(3), new Double(7.3), "Delete2 " }, { new Date(), "D ", new Integer(4), new Double(8.4), "Delete3 " } }; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); getContentPane().add( scrollPane ); ButtonColumn buttonsColumn = new ButtonColumn(table, 4); } public static void main(String[] args) { TableButton3 frame = new TableButton3(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
package com; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractCellEditor; import javax.swing.JButton; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumnModel; public class ButtonColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener { JTable table; JButton renderButton; JButton editButton; String text; public ButtonColumn(JTable table, int column) { super(); this.table = table; renderButton = new JButton(); editButton = new JButton(); editButton.setFocusPainted(false); editButton.addActionListener(this); TableColumnModel columnModel = table.getColumnModel(); columnModel.getColumn(column).setCellRenderer(this); columnModel.getColumn(column).setCellEditor(this); } public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { if (hasFocus) { renderButton.setForeground(table.getForeground()); renderButton.setBackground(UIManager.getColor("Button.background")); } else if (isSelected) { renderButton.setForeground(table.getSelectionForeground()); renderButton.setBackground(table.getSelectionBackground()); } else { renderButton.setForeground(table.getForeground()); renderButton.setBackground(UIManager.getColor("Button.background")); } renderButton.setText((value == null) ? " " : value.toString()); return renderButton; } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { text = (value == null) ? " " : value.toString(); editButton.setText(text); return editButton; } public Object getCellEditorValue() { return text; } public void actionPerformed(ActionEvent e) { fireEditingStopped(); System.out.println(e.getActionCommand() + " : " + table.getSelectedRow()); } }
OVER~~~