I'm trying to come up with a confirmation window for deleting objects that requires that the user enter the word "Delete" into a text field to confirm their action and then click a button labeled "Delete". Additionally it would have the standard "Cancel" button as well.
Below is the basic idea of what I want, but I'm not sure how to return the boolean properly:
public static boolean confirmDelete(String msg) {
JPanel panel = new JPanel();
JPanel sPanel1 = new JPanel();
JPanel ssPanel1 = new JPanel();
ssPanel1.setLayout(new BoxLayout(ssPanel1, BoxLayout.Y_AXIS));
JLabel lbl = new JLabel(msg);
confirm = new JTextField(10);
ssPanel1.add(lbl);
ssPanel1.add(confirm);
JPanel ssPanel2 = new JPanel();
ssPanel2.setLayout(new BoxLayout(ssPanel2, BoxLayout.Y_AXIS));
JButton ok = new JButton("Delete");
ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if(confirm.getText().toLowerCase().equals("delete")) {
//Set the returned value to true;
} else {
alertMsg("Invalid input. Please try again.");
}
}
});
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
//Set the returned value to false;
}
});
ssPanel2.add(ok);
ssPanel2.add(cancel);
sPanel1.add(ssPanel1);
sPanel1.add(ssPanel2);
panel.add(sPanel1);
JFrame deleteFrm = new JFrame("Confirm Delete");
deleteFrm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//Add content to the window.
deleteFrm.setContentPane(panel);
//Display the window.
deleteFrm.pack();
deleteFrm.setVisible(true);
}
Basically, I'd like to call up this window using the line boolean deleteItem = ClassName.confirmDelete(msg);
and have it return the boolean to state whether or not they properly confirmed the deletion status. How can I set this up to return the boolean as depicted in the code sample (which is kind of a pseudo-code as it's obviously incorrect). Is this even possible with a single-line call like mentioned?