简体   繁体   English

JTable单元不可编辑但可单击

[英]JTable cell not editable but clickable

I want to put an action whenever someone clicks a cell. 我想在有人单击单元格时采取措施。 open another gui for example. 例如,打开另一个GUI。 But how do i make a cell clickable BUT not editable? 但是如何使单元格可点击但不可编辑? These are results for an sql query. 这些是sql查询的结果。 I can't manage to make the table uneditable though. 我无法使表格不可编辑。 Do I need a listener or something? 我需要听众吗? and if yes where should I put it? 如果是的话,我应该放在哪里?

Here is my code: 这是我的代码:

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) {
        }


    }

}

Start by taking a look at How to use tables 首先看一下如何使用表格

The isCellEditable method the TableModel determines of a cell is editable or not. TableModel确定单元格的isCellEditable方法是否可编辑。 This method should return false 此方法应返回false

When you supply column/data information to the JTable directly, the JTable creates a DefaultTableModel internally. 当您直接向JTable提供列/数据信息时, JTable在内部创建一个DefaultTableModel This class's isCellEditiable method will return true by default. 此类的isCellEditiable方法默认情况下将返回true

By using something like DefaultTableModel , you can override this method without with to much trouble and set the model to the table directly. 通过使用类似DefaultTableModel东西,您可以毫不费力地覆盖此方法,并将模型直接设置为表。

Next, you need to attach a MouseListener to the table 接下来,您需要将MouseListener附加到表

Take a look at How to write a Mouse Listener 看看如何编写鼠标侦听器

You can then use getSelectedColumn , getSelectedRow to get the selected cell. 然后,您可以使用getSelectedColumngetSelectedRow获取选定的单元格。

You'll also need to use convertRowIndexToModel and convertColumnIndexToModel to convert between the view and model indices 您还需要使用convertRowIndexToModelconvertColumnIndexToModel在视图索引和模型索引之间进行转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM