JTable中嵌套JButton并加入JButton的事件处理

版权声明:本文为博主原创文章,转载请保留原文链接 https://blog.csdn.net/SuperVictim/article/details/50492282

使用JTable还是挺麻烦的,特别是当需要嵌套其他的控件时,现在想在button上添加事件每次按钮一点击就会有获取到点击到的行和列,废话不多说,直接进入正题:

原理

要在JTable中嵌套其他的控件需要修改TableColumnModel的渲染器和编辑器,所以只要实现TableCellRenderer和TableCellEditor,然后通过表格获取TableColumnModel,再通过TableColumnModel设置渲染器和编辑器,分别是这两个方法,一个是setCellRenderer()方法,一个是setCellEditor()方法。

代码

TableCellRenderer的实现类MyButtonRender.java:

package test6;

import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

public class MyButtonRender implements TableCellRenderer{

    private JButton button;

    public MyButtonRender() {
        button = new JButton("确认");
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        return button;
    }

}

上面的没有什么需要介绍的,重点是下面的编辑器:MyButtonEditor.java:

package test6;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;

public class MyButtonEditor extends DefaultCellEditor{

    private MyButton button;

    private MyEvent event;

    public MyButtonEditor() {
        super(new JTextField());
        button = new MyButton("确认");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            //这里调用自定义的事件处理方法
                event.invoke(e);
            }

        });

    }

    public MyButtonEditor(MyEvent e) {
        this();
        this.event = e;
    }
    /*
    重写编辑器方法,返回一个按钮给JTable
    */
    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {
//      setClickCountToStart(1);
//将这个被点击的按钮所在的行和列放进button里面
        button.setRow(row);
        button.setColumn(column);
        return button;
    }


}

在那里面的,我引用了我自定义的MyButton,从JButton继承而来的,里面我方了两个属性,分别记录了每一个按钮的行和列。
MyButton.java:

package test6;

import javax.swing.JButton;

public class MyButton extends JButton{

    private int row;

    private int column;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public MyButton() {

    }

    public MyButton(String name) {
        super(name);
    }

}

在MyButtonEditor中还有一个我自己定义的MyEvent,里面只定义了一个方法,public abstract void invoke(ActionEvent e);这个方法是要在按钮的事件处理方法被调用,接下来是主函数:
Main.java;

package test6;

import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTable;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("test");
        JTable table = new JTable(5, 4);
        //实例化MyEvent,这里的invoke写你自己处理按钮被点击的逻辑
        MyEvent e = new MyEvent() {
            @Override
            public void invoke(ActionEvent e) {
                MyButton button = (MyButton)e.getSource();
                //打印被点击的行和列
                System.out.println("row:" + button.getRow() + "column :" + button.getColumn());
            }

        };
        //设置表格的渲染器
        table.getColumnModel().getColumn(2).setCellRenderer(new MyButtonRender());

        MyButtonEditor editor = new MyButtonEditor(e);
        //设置表格的编辑器table.getColumnModel().getColumn(2).setCellEditor(editor);
        frame.getContentPane().add(table);
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

这里写图片描述

点击之后的运行结果:

row:0column :2
row:0column :2
row:1column :2
row:1column :2
row:2column :2
row:2column :2
row:3column :2
row:3column :2
row:4column :2
row:4column :2
row:4column :2
展开阅读全文

没有更多推荐了,返回首页