简体   繁体   English

如何根据单击的行获取jtable的单元格值

[英]How to get cell value of jtable depending on which row is clicked

I am trying to use an update method on my jtable that's connected to the database and would like to fill in the textfields on the form depending on which row the users clicks. 我试图在我的jtable上使用一个更新方法,它连接到数据库,并希望根据用户点击的哪一行填写表单上的文本字段。 I understand I will be needing a getValueAt() method however I am uncertain of how to fill in which row depending on which row the user clicks. 我知道我将需要一个getValueAt()方法但是我不确定如何根据用户点击的哪一行填写哪一行。 I am unable to find anything on Google or anything so any information would be helpful! 我无法在Google或其他任何地方找到任何内容,因此任何信息都会有所帮助!

private final UrTableModel urTableModel;
private JTable urTable;
...

// 1. Create your table model class that should extends from DefaultTableModel, instantiate it
urTableModel=new UrTableModel();

// 2. creates table
table = TableUtils.createStandardSortableTable(urTableModel);

// 3. customize your table
table.setBackground(Color.WHITE);
table.getTableHeader().setReorderingAllowed(false);

// 4. Add the mouse listner to it
table.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseClicked(final MouseEvent e) {
        if (e.getClickCount() == 1) {
            final JTable target = (JTable)e.getSource();
            final int row = target.getSelectedRow();
            final int column = target.getSelectedColumn();
            // Cast to ur Object type
            final UrObjctInCell urObjctInCell = (UrObjctInCell)target.getValueAt(row, column);
            // TODO WHAT U WANT!
        }
    }
});

Cheers, 干杯,

You will need to call getValueAt() your table's model to get the values you need. 您需要调用getValueAt()表的模型来获取所需的值。 You will also need a listener on the table to listen for selections. 您还需要一个监听器来监听选择。 So that once a user selects a row you call getValueAt() to get the value for the specific column of data in that row. 因此,一旦用户选择了一行,您就可以调用getValueAt()来获取该行中特定数据列的值。

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

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