简体   繁体   English

为什么单击 JTable 单元后没有响应?

[英]Why there is no response after I clicked in a JTable cell?

I have the following sample Java Swing JTable app:我有以下示例 Java Swing JTable 应用程序:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class JTableListSelectionListener
{
  public static void main(String[] a)
  {
    JFrame frame=new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table;

    String[] columnTitles={"A","B","C","D"};
    Object[][] rowData={{"11","12","13","14"},{"21","22","23","24"},{"31","32","33","34"},{"41","42","43","44"}};

    table=new JTable(rowData,columnTitles);

    table.setCellSelectionEnabled(true);
    ListSelectionModel cellSelectionModel=table.getSelectionModel();
    cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    cellSelectionModel.addListSelectionListener(new ListSelectionListener()
    {
      public void valueChanged(ListSelectionEvent e)
      {
        String selectedData=null;

        int[] selectedRow=table.getSelectedRows();
        int[] selectedColumns=table.getSelectedColumns();

        for (int i=0;i<selectedRow.length;i++)
          for (int j=0;j<selectedColumns.length;j++)
            selectedData=(String)table.getValueAt(selectedRow[i],selectedColumns[j]);
        System.out.println("Selected: "+selectedData);
      }

    });

    frame.add(new JScrollPane(table));
    frame.setLocationRelativeTo(null);
    frame.setSize(300,200);
    frame.setVisible(true);
  }
}

It won't respond if I click on the same row: If I click on "12" then on "13" or first click on "21" then click on "23", why?如果我点击同一行,它不会响应:如果我点击“12”然后点击“13”或者先点击“21”然后点击“23”,为什么?

[1] How to fix it so no matter where I click in which ever order, it will always output the value in that cell? [1] 如何修复它,所以无论我在哪个位置单击哪个顺序,它总是 output 该单元格中的值?

[2] How to be able to tell if I have clicked with left mouse button or the right mouse button? [2] 如何判断我点击的是鼠标左键还是鼠标右键?

The problem is, the ListSelectionModel for the JTable managers the row selection only, from the JavaDocs问题是,来自JavaDocsJTableListSelectionModel只管理行选择

Returns the ListSelectionModel that is used to maintain row selection state.返回用于维护行选择 state 的 ListSelectionModel。

This means that of the row selection doesn't change, you won't get a notification.这意味着行选择不会改变,您不会收到通知。

Instead, you probably want to use the ListSelectionModel used by the TableColumnModel , for example...相反,您可能希望使用TableColumnModel使用的ListSelectionModel ,例如...

table.getColumnModel().getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println("Selected column");
    }
});

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

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