Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTable:没有选择行_Java_Swing - Fatal编程技术网

Java JTable:没有选择行

Java JTable:没有选择行,java,swing,Java,Swing,每当jTable中没有选择任何行时,我想禁用一个按钮。有什么方法可以做到这一点吗?将选择侦听器添加到表中。如果出现选择,请启用该按钮。默认情况下禁用该按钮 在JTable上使用 JTable table = new JTable(); JButton button = new JButton(); button.setEnabled(false); ListSelectionModel listSelectionModel = table.getSelectionModel(); listSe

每当jTable中没有选择任何行时,我想禁用一个按钮。有什么方法可以做到这一点吗?

将选择侦听器添加到表中。如果出现选择,请启用该按钮。默认情况下禁用该按钮

在JTable上使用

JTable table = new JTable();
JButton button = new JButton();
button.setEnabled(false);

ListSelectionModel listSelectionModel = table.getSelectionModel();
listSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) { 
            ListSelectionModel lsm = (ListSelectionModel)e.getSource();
            button.setEnabled(!lsm.isSelectionEmpty());
});

像这样的方法应该会奏效:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
{
    @Override
    public void valueChanged(ListSelectionEvent e)
    {
        if (!e.getValueIsAdjusting())
        {
            boolean rowsAreSelected = table.getSelectedRowCount() > 0;
            button.setEnabled(rowsAreSelected);
        }
    }
});

没有ListSelectionEvent被激活?如果是这样的话,我会非常惊讶……更好的是,删除if/else,只需按钮.setEnabled(!lsm.isSelectionEmpty());该链接指向旧版本的API