如何添加的JTable中单元格内的按钮,并给它的行动 [英] How to add buttons inside a cell of jtable and give it action

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

问题描述

我知道这之前已经有了答案,但考虑到即时通讯仍然是一个新手,我不能想出一个办法给行动和阻断细胞的编辑,我已经在几个方面尝试过,我可以成功地呈现按钮但是当我点击它,它编辑pssing按钮的单元格,而不是$ p $,我知道,为了避免单元格编辑我应该创建一个抽象表,并覆盖此方法的方法:

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,所以这是code我使用的面板和单元格渲染器:

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;

    }

这就是code我使用检索SQL到JTable数据(我定制了DefaultJTable code)

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);

所以我呈现在此基础上螺纹表里面的按钮:
<一href=\"http://stackoverflow.com/questions/2069641/adding-buttons-inside-cell-of-jtable-along-with-data\">Adding里面有数据一起JTable中的单元格按钮?

我的问题是非常简单的,我怎么能禁用行编辑(就像中的isCellEditable()方法的使用),以及按钮给行动?
这里的任何帮助是极大的AP preciated,并请把它考虑IM仍然新手所以请详细信息或样品需要!
亲切的问候,
罗慕洛·罗梅罗

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

推荐答案

您需要的两个的渲染器的的和编辑,如本的例如的。见的如何使用表:编辑器和渲染器的对的详细信息。四角切圆,你应该覆盖的方法的isCellEditable()的TableModel 而不是扩展的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天全站免登陆