I want to open a JDialog (CustomerUpdateDialog) after clicking a JTable (part of a JDialog called: CustomerListDialog) row only once, however it takes two clicks to open CustomerUpdateDialog.
Here is the code for the 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);
}
}
and here is the code where I add the JTable dialog to the main menu:
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);
}
});
A sample list of customer data:
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]
Why do I need to click a row twice in order to open another dialog?
Thank you.
* UPDATE *
I've changed the MouseListener event handler to ListSelectionListener as per @AndrewThompson's comment and @Lunatic0's answer, but I'm still getting the same result.
I find that I click the table, then I can click any other application on my computer, and then the dialog will be displayed.
You can test this snippet and adapt to your custom dialog situation.
Basically, you just need to create the listener and then set it to your table by using
getSelectionModel().addListSelectionListener(listener)
Also, put the table inside a JFrame
instead of a JDialog
, I don't know for sure why but seems to only work properly with the 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();
}
}