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

问题描述 投票:0回答:1

我只想单击一次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, [email protected], 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, [email protected], joinDate=2018-06-18]

为什么我需要单击两次以打开另一个对话框?

谢谢。

*更新*

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

我发现我单击了表,然后可以单击计算机上的任何其他应用程序,然后将显示对话框。

java swing jtable jdialog miglayout
1个回答
1
投票

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

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

也将表放在JFrame而不是JDialog中,我不确定为什么,但似乎只能在JFrame上正常工作。

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.