如何确定选择了 JTable 中的哪个单元格?

发布于 2024-10-17 20:01:18 字数 616 浏览 0 评论 0原文

我在 GUI 中有一个 JTable,我想根据用户单击的单元格的值返回一个数字。这是代码:

ListSelectionModel newmodel = mytable.getSelectionModel();
newmodel.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        int row = mytable.getSelectedRow();
        int column = mytable.getSelectedColumn();

        int cell = getNewNum();
        datefield.setText(String.valueOf(cell));
    }
});

我对此有几个问题。首先,这个方法使我的表格可编辑。在使用此方法之前,我无法编辑表格,但现在我可以删除条目。我查看了API,但不知道为什么会这样。其次,如果我单击第 3 行中的一个单元格,然后单击第 3 行中的另一行,则不会注册任何事件。如何通过单击当前选定行上的单元格来创建事件?

I have a JTable in a GUI and I want to return a number based on the value of the cell that a user clicks on. This is the code:

ListSelectionModel newmodel = mytable.getSelectionModel();
newmodel.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        int row = mytable.getSelectedRow();
        int column = mytable.getSelectedColumn();

        int cell = getNewNum();
        datefield.setText(String.valueOf(cell));
    }
});

I have a couple of problems with this. Firstly this method makes my table editable. Before I used this method I couldn't edit the table but now I can delete entries. I looked in the API but I don't know why this is. Secondly, if I click on a cell in row 3, say, and then I click on another row in cell 3, no event is registered. How can I make an event from clicking in a cell on the currently selected row?

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

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

发布评论

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

评论(1

内心荒芜 2024-10-24 20:01:18

一种常见的方法是通过事件获取用户单击的点:

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = jTable1.rowAtPoint(evt.getPoint());
        int col = jTable1.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }
    }
});

这是使用选择模式的第二个选项:

jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
       @Override
       public void mouseClicked(java.awt.event.MouseEvent evt) {
           ...
           int row = jTable1.getSelectedRow();
           int col = jTable1.getSelectedColumn());
           if (evt.getClickCount() > 1) { // double-click etc...
              ...

如果您选择:

public boolean isCellEditable(int row, int col) {
   return false;
}

那么您的 JTable 将不可编辑。

最后,为了获取您想要的值,您只需调用 JTable 模型的 getValueAt(row,col) 即可,或者获取如下内容:

Object foo = jTable1.getModel().getValueAt(row, col); 

A common method is to get the point where the user clicked through the event:

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = jTable1.rowAtPoint(evt.getPoint());
        int col = jTable1.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }
    }
});

Here is a second option using selection mode:

jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
       @Override
       public void mouseClicked(java.awt.event.MouseEvent evt) {
           ...
           int row = jTable1.getSelectedRow();
           int col = jTable1.getSelectedColumn());
           if (evt.getClickCount() > 1) { // double-click etc...
              ...

If you go:

public boolean isCellEditable(int row, int col) {
   return false;
}

Then your JTable will not be editable.

Finally in order to get the value you want, you just need to call the getValueAt(row,col) of your JTable Model, or get the contents like this:

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