【问题标题】:JTable - Selected Row click eventJTable - 选定行点击事件
【发布时间】:2012-04-25 01:32:14
【问题描述】:

我有一个 Jtable,它通过 AbstractTableModel 填充了链表。

我想要做的是当我单击(鼠标左键单击)JTable 中的一行时,链接列表是搜索(在这种情况下它包含电影标题)并在 Jtextboxes 中显示链接列表中的值

我该怎么做?

这里是代码

我猜它会将所选行中的数据检索到一个数组中,将其拆分,然后将其放入 jtextareas 中。我该怎么做?

【问题讨论】:

  • 我提供了类似的答案here希望对您有所帮助

标签: java swing linked-list jtable jtextfield


【解决方案1】:

来自source,经过一些增强和编辑:

public class RowSelectionListener implements ListSelectionListener {

    @Override
    public void valueChanged(ListSelectionEvent event) {

        int viewRow = table.getSelectedRow();

        if (!event.getValueIsAdjusting() && viewRow != -1) {

            int columnIndex = 1;

            // Better to access table row using modelRow rather than viewRow
            int modelRow = table.convertRowIndexToModel(viewRow);

            // Access value at selected row at the second column (columnIndex = 1)
            Object modelvalue = table.getModel().getValueAt(modelRow, columnIndex);

            // Not recommended: same as above but access row using viewRow
            Object tablevalue = table.getValueAt(viewRow, columnIndex);

            // Print cell value
            System.out.println(modelvalue + "=" + tablevalue);
        }
    }
}

然后将ListSelectionListener添加到JTable

 table.getSelectionModel().addListSelectionListener(new RowSelectionListener());

重要提示:

viewRowmodelRow 在应用TableRowSorter 时变得有效不同。

【讨论】:

    【解决方案2】:
     private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
         JTable source = (JTable)evt.getSource();
                int row = source.rowAtPoint( evt.getPoint() );
                int column = source.columnAtPoint( evt.getPoint() );
                String s=source.getModel().getValueAt(row, column)+"";
    
                JOptionPane.showMessageDialog(null, s);
    
    
    } 
    

    如果你想点击 jtable 中的单元格或行,请使用这种方式

    【讨论】:

      【解决方案3】:

      我是这样做的:

      table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
              public void valueChanged(ListSelectionEvent event) {
                  // do some actions here, for example
                  // print first column value from selected row
                  System.out.println(table.getValueAt(table.getSelectedRow(), 0).toString());
              }
          });
      

      此代码对鼠标单击和键盘上的项目选择作出反应。

      【讨论】:

      • 您可以在 System.out.println(table.getValueAt(table.getSelectedRow(), 0) 之前检查是否 (!e.getValueIsAdjusting() && table.getSelectedRow() != -1)。 toString());
      • @RodrigoGarcia - 你能解释一下为什么他们应该添加这张支票吗?
      • 对不起@Ascalonian,我不记得了,我应该解释得更好。
      • @RodLima,你怎么了 Rod,你为什么不记得,我们一起编码,你找到了防止每次选择调用事件两次的解决方案!
      【解决方案4】:

      您可以使用MouseClicked 事件:

      private void tableMouseClicked(java.awt.event.MouseEvent evt) {
       // Do something.
      }
      

      【讨论】:

      • 这仅回答了问题中最明显的部分。
      【解决方案5】:

      要了解选择了哪一行,请添加ListSelectionListener,如示例SimpleTableSelectionDemo 中的How to Use Tables 所示。可以直接从链表的toArray()方法构造一个JList,具体可以添加合适的监听器。

      【讨论】:

      • 如果您在实现侦听器时遇到问题,请编辑您的问题以包含一个显示问题的sscce
      【解决方案6】:

      我建议为此使用Glazed Lists。它使得将数据结构映射到表模型变得非常容易。

      要对 JTable 上的鼠标单击做出反应,请使用 ActionListener:ActionListener on JLabel or JTable cell

      【讨论】:

      • 所以我尝试了mouselistener的代码,在getSelectedRow()和getSelectedColumn()之后如何存储在数组中?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-28
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多