JTable单元格不可编辑但可点击

时间:2013-10-01 10:45:18

标签: java swing jtable clickable

我想在有人点击一个单元格时执行操作。打开另一个gui例如。但是如何使单元格可点击但是不可编辑?这些是sql查询的结果。我无法设法使表无法编辑。我需要听众吗?如果是,我应该把它放在哪里?

这是我的代码:

public class AllResultsFromDB extends JFrame
{

    GUI ins = new GUI();

    public AllResultsFromDB(GUI x)
    {
        Vector columnNames = new Vector();

        Vector data = new Vector();
        this.ins = x;

        try
        {   
            // Initializing GUI class in order to call getSelectedTable() method.
//            GUI ins = new GUI();
            //System.out.println(ins.getSelectedTable());
            Login sgui = new Login();

            String dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
            Connection connection = DriverManager.getConnection( dburl, sgui.getUsername(), sgui.getPassword() );


            //  Fetch data from table specified by user

            String query = "SELECT * FROM "  + ins.getSelectedTable() + " ORDER BY id";
            System.out.println(query);
            Statement stmt = connection.createStatement();
            ResultSet rset = stmt.executeQuery(query);   
            ResultSetMetaData metad = rset.getMetaData();
            int columns = metad.getColumnCount();



            //  This loop gets the names of the columns

            for (int i = 1; i <= columns; i++)
            {
                columnNames.addElement( metad.getColumnName(i) );
                //columnNames.addElement("PROFILES");
            }




            //  This loop gets the data inside the rows

            while (rset.next())
            {
                Vector row = new Vector(columns);

                //Vector b = new Vector((Collection)button);

                for (int i = 1; i <= columns; i++)
                {
                    row.addElement( rset.getObject(i) );
                }

                data.addElement( row );
                //data.addElement(b);
            }

            rset.close();
            stmt.close();
            connection.close();




            //  Create table with results

        JTable table = new JTable(data, columnNames)
        { 

            public Class getColumnClass(int column)
            { 
                for (int row = 0; row < getRowCount(); row++)
                {
                    Object obj = getValueAt(row, column);


                    if (obj != null)
                    {
                        return obj.getClass();
                    }
                }

                return Object.class;
            }

        };


        JScrollPane scroll = new JScrollPane( table );
        getContentPane().add( scroll );
        //table.addMouseListener(l);
        //table.setEnabled(false);
        //table.setDragEnabled(true);



        JPanel panel = new JPanel();
        getContentPane().add( panel, BorderLayout.SOUTH );

    } catch (SQLException e) {
        }


    }

}

1 个答案:

答案 0 :(得分:4)

首先看一下How to use tables

isCellEditable确定单元格的TableModel方法是否可编辑。此方法应返回false

当您直接向JTable提供列/数据信息时,JTable会在内部创建DefaultTableModel。默认情况下,此类的isCellEditiable方法将返回true

通过使用类似DefaultTableModel的内容,您可以覆盖此方法,而不会遇到太多麻烦,并直接将模型设置为表格。

接下来,您需要将MouseListener附加到表格

查看How to write a Mouse Listener

然后,您可以使用getSelectedColumngetSelectedRow来获取所选单元格。

您还需要使用convertRowIndexToModelconvertColumnIndexToModel来转换视图和模型索引