触发 ListSelectionListener 操作,但在第二次单击后显示 JDialog

ListSelectionListener Action is Fired, but JDialog Displayed After 2nd Click

我只想在单击 JTable(JDialog 的一部分,称为:CustomerListDialog)行后打开 JDialog (CustomerUpdateDialog),但是需要单击两次才能打开 CustomerUpdateDialog。

这是 JTable 的代码:

public class CustomerListDialog extends JDialog {

private void initComponents(ArrayList<Customer> customers) {

    ...

    ListSelectionListener listener = new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            try {
                SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy");

                Date date = format.parse (tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 8).toString());
                LocalDate joinDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();

                Customer customer = new Customer.Builder(Validator.Id(tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 0).toString()),
                        tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 1).toString(),
                        tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 2).toString(),
                        tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 6).toString()) //
                                .streetName(tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 3).toString()) //
                                .city(tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 4).toString()) //
                                .postalCode(tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 5).toString()) //
                                .emailAddress(Validator.Email(tbl_Settings.getValueAt(tbl_Settings.getSelectedRow(), 7).toString()))
                                .joinDate(joinDate)//
                                .build();

                System.out.println(customer.toString());
                System.out.println(e.toString());

                CustomerUpdateDialog customerUpdateDialog = new CustomerUpdateDialog(customer);
                customerUpdateDialog.setDefaultCloseOperation(CustomerUpdateDialog.DISPOSE_ON_CLOSE);
                customerUpdateDialog.setVisible(true);

            }
            catch (ApplicationException | ParseException e1) {
                System.out.println(e1.getMessage());
            }

        }
    };

    tbl_Settings.getSelectionModel().addListSelectionListener(listener);
}

private JTable createTable(ArrayList<Customer> customers) {

    String[] columnNames = "Customer ID,First Name,Last Name,Street Name,City,Postal Code,Phone Number,Email Address,Join Date".split(",");

    int rows = customers.size();
    int cols = columnNames.length;
    String[][] data = new String[rows][cols];

    for(Customer customer : customers) {
        int i = customers.indexOf(customer);
        for(int j=0; j<cols; j++) {
            String cellData = null;
            switch (j) {
                case 0:
                    cellData = String.valueOf(customer.getId());
                    break;
                case 1:
                    cellData = customer.getFirstName();
                    break;
                case 2:
                    cellData = customer.getLastName();
                    break;
                case 3:
                    cellData = customer.getStreetName();
                    break;
                case 4:
                    cellData = customer.getCity();
                    break;
                case 5:
                    cellData = customer.getPostalCode();
                    break;
                case 6:
                    cellData = customer.getPhoneNumber();
                    break;
                case 7:
                    cellData = customer.getEmailAddress();
                    break;
                case 8:
                    cellData = customer.getJoinDateString();
            }
         data[i][j] = cellData;
         } 
    }

    JTable table = new JTable(data, columnNames);

    return table;
}

public CustomerListDialog(ArrayList<Customer> customers) {
    setBounds(100, 100, 1000, 500);
    initComponents(customers);
}

}

这里是我将 JTable 对话框添加到主菜单的代码:

JMenuItem mntmCustomersList = new JMenuItem("List");
    mnCustomers.add(mntmCustomersList);
    mntmCustomersList.setHorizontalAlignment(SwingConstants.LEFT);
    mntmCustomersList.setContentAreaFilled(false);
    mntmCustomersList.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            CustomerListDialog dialog = new CustomerListDialog(customerList);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        }
    });

客户数据示例列表:

Customer [id=1739, firstName=Kaitlin, lastName=Oneil, streetName=P.O. Box 329, 9608 Tortor Road, city=Diegem, postalCode=64447, phoneNumber=834-890-3976, emailAddress=enim.sit.amet@sedlibero.edu, joinDate=2014-04-12]
Customer [id=2210, firstName=Octavius, lastName=Joseph, streetName=Ap #342-9819 Quis St., city=San Leucio del Sannio, postalCode=55477-429, phoneNumber=671-872-7563, emailAddress=nisi.sem@orcilobortis.ca, joinDate=2018-06-18]

为什么我需要点击一行两次才能打开另一个对话框?

谢谢。

* 更新 *

我已根据@AndrewThompson 的评论和@Lunatic0 的回答将 MouseListener 事件处理程序更改为 ListSelectionListener,但我仍然得到相同的结果。

我发现我点击table,然后我可以点击我电脑上的任何其他应用程序,然后就会显示对话框。

您可以测试此代码段并适应您的自定义对话情况。

基本上,您只需要创建侦听器,然后使用 table 将其设置为您的 table getSelectionModel().addListSelectionListener(listener)

此外,将 table 放在 JFrame 而不是 JDialog 中,我不确定为什么但似乎只能与 [=12= 一起正常工作].

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TableExample {
    JFrame f;

    TableExample() {
        f = new JFrame();

        String data[][] = { { "101", "Amit", "670000" }, { "102", "Jai", "780000" }, { "101", "Sachin", "700000" } };
        String column[] = { "ID", "NAME", "SALARY" };
        JTable jt = new JTable(data, column);
        jt.setBounds(30, 40, 200, 300);
        JScrollPane sp = new JScrollPane(jt);
        f.add(sp);
        f.setSize(300, 400);
        f.setVisible(true);

        ListSelectionListener listener = new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                JOptionPane.showInputDialog("Hello");
                System.out.println(jt.getValueAt(jt.getSelectedRow(), 0).toString());

            }
        };

        jt.getSelectionModel().addListSelectionListener(listener);

    }

    public static void main(String[] args) {
        new TableExample();
    }
}