jbutton在jtable中选择行时不重新启用

w8ntj3qf  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(256)

我试图在创建按钮时将其设置为enabled(false),当我选择jtable上的任何一行时,该按钮变为enabled(true)。
这里的逻辑很简单,但由于某些原因,它不太起作用,按钮永远不会进入enabled(true)。

JButton btnIniciarReparacin = new JButton("INICIAR REPARACI\u00D3N");

    btnIniciarReparacin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tiempoStart = (int) (System.currentTimeMillis() / 1000L);
            btnIniciarReparacin.setEnabled(false);
        }
    });
    btnIniciarReparacin.setFont(new Font("SansSerif", Font.BOLD, 13));
    btnIniciarReparacin.setBackground(new Color(231, 111, 81));
    btnIniciarReparacin.setBounds(129, 625, 254, 50);
    frame.getContentPane().add(btnIniciarReparacin);

    int row = table.getSelectedRow();
    //Comprobamos si hemos cogido algo de la tabla y si los botones están encendidos
    if (table.isRowSelected(row)) {
        btnIniciarReparacin.setEnabled(true);
    } else {
        btnIniciarReparacin.setEnabled(false);
    }
2w3rbyxf

2w3rbyxf1#

swing gui的工作是将侦听器添加到事件中,然后响应侦听器中的状态更改。
在您创建组件的代码中,您似乎正在检查行选择状态,这永远不会起作用,因为它只检查一次状态,而且在用户有机会做出选择之前。相反,您需要在jtable上使用一个监听器,更具体地说,是一个listselectionlistener,您可以通过它添加到jtable的选择模型中 table.getSelectionModel().addListSelectionListener(...) 还要注意:

int row = table.getSelectedRow();
if (table.isRowSelected(row)) {
    btnIniciarReparacin.setEnabled(true);
} else {
    btnIniciarReparacin.setEnabled(false);
}

可缩短为:

int row = table.getSelectedRow();
btnIniciarReparacin.setEnabled(table.isRowSelected(row));

e、 g.类似于:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
    public void valueChanged(ListSelectionEvent event) {
        int row = table.getSelectedRow();
        btnIniciarReparacin.setEnabled(table.isRowSelected(row));
    }
});

例如,如果按钮删除一行:

import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public class Test01 extends JPanel {
    private static final String[] COLUMNS = {"One", "Two", "Three", "Four"};
    private DefaultTableModel tableModel = new DefaultTableModel(COLUMNS, 0);
    private JTable table = new JTable(tableModel);
    private JButton deleteRowButton = new JButton("Delete Row");

    public Test01() {
        int tableRows = 20;
        for (int i = 0; i < tableRows; i++) {
            Integer[] row = new Integer[COLUMNS.length];
            for (int j = 0; j < row.length; j++) {
                row[j] = (int) (100 * Math.random());
            }
            tableModel.addRow(row);
        }

        table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                int row = table.getSelectedRow();
                deleteRowButton.setEnabled(table.isRowSelected(row));
            }
        });

        deleteRowButton.addActionListener(e -> {
            int row = table.getSelectedRow();
            tableModel.removeRow(row);
        });
        deleteRowButton.setEnabled(false);
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(deleteRowButton);

        setLayout(new BorderLayout());
        add(new JScrollPane(table));
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui() {
        Test01 mainPanel = new Test01();
        JFrame frame = new JFrame("Test01");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
}

相关问题