import java.awt.Component\x0d\x0aimport java.awt.event.ActionEvent\x0d\x0aimport java.awt.event.ActionListener\x0d\x0a\x0d\x0aimport javax.swing.*\x0d\x0aimport javax.swing.table.*\x0d\x0a\x0d\x0apublic class ButtonTable extends JFrame{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JTable table = null\x0d\x0aprivate DefaultTableModel model=null\x0d\x0aprivate JScrollPane js=null \x0d\x0apublic ButtonTable(){\x0d\x0amodel = new DefaultTableModel(3,3)\x0d\x0atable = new JTable(model)\x0d\x0atable.getColumnModel().getColumn(1).setCellEditor(new MyRender())//设置
编辑器\x0d\x0atable.getColumnModel().getColumn(1).setCellRenderer(new MyRender() )\x0d\x0ajs = new JScrollPane(table)\x0d\x0a\x0d\x0athis.add(js)\x0d\x0athis.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)\x0d\x0athis.setVisible(true)\x0d\x0athis.setSize(399, 300)\x0d\x0athis.setLocationRelativeTo(null)\x0d\x0a}\x0d\x0a\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0anew ButtonTable()\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a//渲染 器 编辑器\x0d\x0aclass MyRender extends AbstractCellEditor implements TableCellRenderer,ActionListener, TableCellEditor{\x0d\x0a\x0d\x0aprivate static final long serialVersionUID = 1L\x0d\x0aprivate JButton button =null\x0d\x0apublic MyRender(){\x0d\x0abutton = new JButton("确定不?")\x0d\x0abutton.addActionListener(this)\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Object getCellEditorValue() {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn null\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellRendererComponent(JTable table, Object value,\x0d\x0aboolean isSelected, boolean hasFocus, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic void actionPerformed(ActionEvent e) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aJOptionPane.showMessageDialog(null, "渲染器学期", "消息", JOptionPane.OK_OPTION)\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic Component getTableCellEditorComponent(JTable table, Object value,\x0d\x0aboolean isSelected, int row, int column) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0areturn button\x0d\x0a}\x0d\x0a\x0d\x0a}这个可以用MouseListener来监听。
table.addMouseListener(....)
//括号内的东西根据实际填写
……
public
void
mouseClicked(MouseEvent
e)
{
int
index=table.getSelectedRow()
String
code=(String)table.getValueAt(index,0)//这一句是取出表格选中那行的第一列
text1.setText(code)
//其他列的数据类似,不逐一说明,自己研究即可。
}
评论列表(0条)