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

发布于 2024-10-29 00:06:59 字数 129 浏览 1 评论 0原文

我正在尝试在连接到数据库的 jtable 上使用更新方法,并希望根据用户单击的行填写表单上的文本字段。我知道我将需要 getValueAt() 方法,但是我不确定如何根据用户单击的行填写哪一行。我在谷歌上找不到任何东西,所以任何信息都会有帮助!

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. 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. I am unable to find anything on Google or anything so any information would be helpful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-11-05 00:06:59
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!
        }
    }
});

干杯,

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,

摘星┃星的人 2024-11-05 00:06:59

您将需要调用表模型的 getValueAt() 来获取所需的值。您还需要桌面上有一个侦听器来侦听选择。因此,一旦用户选择一行,您就可以调用 getValueAt() 来获取该行中特定数据列的值。

You will need to call getValueAt() your table's model to get the values you need. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文