如何在 jtable 的单元格内添加按钮并给它操作 [英] How to add buttons inside a cell of jtable and give it action

查看:18
本文介绍了如何在 jtable 的单元格内添加按钮并给它操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经回答过这个问题,但考虑到我仍然是一个新手,我无法找到一种方法来执行操作并阻止单元格编辑,我已经尝试了多种方法,但是当我单击时,我可以成功呈现按钮它,它编辑单元格而不是按下按钮,我知道为了避免单元格编辑,我应该创建一个抽象表并覆盖此方法:

i know this has been answered before but considering im still a newbie i cant figure out a way to give actions and block the cell editing, i have tried it in several ways,i can successfully render the buttons however when i click on it, it edits the cell instead of pressing the button, i know that in order to avoid the cell editing i should create an abstract table and overide this method:

public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (col < 3) {
            return false;
        } else {
            return true;
        }
    }

但是我使用的是默认的 JTable,所以这是我用于面板和单元格渲染器的代码:

however im using a default JTable,so this is the code i am using for the Panel and cell renderer:

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {

    public Component getTableCellRendererComponent(
                        final JTable table, Object value,
                        boolean isSelected, boolean hasFocus,
                        int row, int column) {


            if(column < 3)
            {
             JLabel campo =   new JLabel(value.toString());


            this.add(campo);

            }
            if(column > 2)
            {

                 //this is a button
                 this.add(botaoteste);





            materialtable.revalidate();
            materialtable.repaint();


            }
            return this;

    }

这是我用来从 sql 检索数据到 Jtable 的代码(我自定义了 DefaultJTable 代码)

and this is the code i am using to retrieve data from sql to the Jtable(I customised the DefaultJTable code)

String[] columnNames={"teste","abc","def"};                 
     Object[][] data = new Object[1][4];
            if(createConnection())
            {
                try {
                    Statement statemt = conLogin.createStatement();
                    ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
                    //ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
                    rsmtdata = rs.getMetaData();

                    //int columns = rsmtdata.getColumnCount();
                     columnNames = new String[]{rsmtdata.getColumnName(1),rsmtdata.getColumnName(2),rsmtdata.getColumnName(3),"Deletar"};
                     if(rs.next())
                     {
                        data[0][0] = rs.getString(1);
                        data[0][1] = rs.getString(2);
                        data[0][2] = rs.getString(3);
                        data[0][3] = new Boolean(false);
                     }
                     while (rs.next())
                     {

                        Object[][] temp = new Object[data.length+1][4];
                        for(int i=0;i < data.length;i++)
                        {
                            for(int j = 0;j < 4;j++)
                            {
                                temp[i][j] = data[i][j];
                            }

                        }
                        temp[data.length][0] = rs.getString(1);
                        temp[data.length][1] = rs.getString(2);
                        temp[data.length][2] = rs.getString(3);
                        temp[data.length][3] = new Boolean(false);
                        data = temp;



                     }
materialtable = new javax.swing.JTable(data, columnNames);
materialtable = new javax.swing.JTable(data, columnNames){ 
            public TableCellRenderer getCellRenderer( int row, int column ) {
                return new PlusMinusCellRenderer();
            }
         };

materialtable.setRowHeight( 32 );

                } catch (SQLException ex) {
                    Logger.getLogger(ProfessorForm.class.getName()).log(Level.SEVERE, null, ex);
                }

                 }

//Create the scroll pane and add the table to it.
materialtable.setBackground(new java.awt.Color(153, 255, 51));

materialtable.setSelectionBackground(new java.awt.Color(255, 255, 51));

materialtable.setSelectionForeground(new java.awt.Color(255, 102, 0));



jScrollPane3.setViewportView(materialtable);

因此要根据此线程在表中呈现按钮:在 JTable 的单元格内添加按钮和数据?

so to render the button inside the table i based on this thread: Adding Buttons inside cell of JTable along with data?

我的问题很简单,如何禁用行编辑(就像 isCellEditable() 方法的用法一样)并对按钮进行操作?非常感谢这里的任何帮助,请考虑我仍然是新手,所以请提供详细信息或样本!亲切的问候,罗慕洛·罗梅罗

my question is very straight forward, how can i disable Row editing(just like the isCellEditable() method usage) and give action to the buttons? any help here is greatly appreciated and please take it to consideration im still novice so please detailed information or samples is needed ! Kind regards, Romulo Romero

推荐答案

你需要both一个渲染器和编辑器,如下图所示示例.请参阅如何使用表:编辑器和渲染器,了解详情.切线地,您应该覆盖 TableModel 中的方法 isCellEditable() 而不是扩展 JTable.

You need both a renderer and and editor, as shown in this example. See How to Use Tables: Editors and Renderers for details. Tangentially, you should override the method isCellEditable() in your TableModel rather than extending JTable.

这篇关于如何在 jtable 的单元格内添加按钮并给它操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆