java swing 在表格每行后添加按钮,要如何实现啊!

java swing 在表格每行后添加按钮,要如何实现啊!,第1张

// 我们需要给 JTable 指定我们自己定义的 Table Cell Editor.

JTable 工作过程如下:

当一个表格显示之前,JTable 会询问每个单元格,getCellRender().getTableCellRendererComponent() 得到一个 Swing 组件后,就用它来在指定单元格显示出来。

当某个单元格即将获得焦点,比如单击或键盘tab 移动,JTable 会询问是否目标单元格允许编辑,如果允许就会询问 getCellEditor().getTableCellEditorComponent() 得到一个编辑器,通常,默认的编辑器是一个 JTextField 类型的,只要我们给一个 JButton 类型的就可以了。

table.setCellEditor(new TableCellEditor() {

    private JButton editor = new JButton()

    private JTextField dephaut = new JTextField()

    

    {//相当于构造函数。

        editor.addActionListener() {

            /* 业务方法 */

        }        

    }    

    /* 此处省略 N 多待实现方法*/

    public Component getTableCellEditorComponent(

JTable table,

Object value,

boolean isSelected,

int row,

int column) {

if (column == 3) {

            return this.editor

        } else {

            return this.dephaut

        }            

    }

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}

对你的需求,简单一点,定义一个类,继承DefaultTableCellRenderer,在其中方法getTableCellRendererComponent中直接返回单元格内的按钮即可。

事实上,我们不一定需要将按钮直接放到表格中,放一个需要显示的字符串就可以了,也省去了定义Renderer的功夫。如果需要实现单元格的点击事件,直接写一个TableCellEditor就可以。

你现在的方式,即使能够显示成按钮的样子,也无法点击,还是要写TableCellEditor.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11704317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存