0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DGolberg
  • 2,109
  • 4
  • 23
  • 31
  • Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow! – Andrew Thompson Apr 16 '13 at 02:18
  • I use indentations to display nested objects when working with UIs to make it easier to follow where objects are placed when they are inside of another object and make it easier to find where I need to add/edit/delete an item (though I do maybe get carried away with the item properties being indented, too). It's something I do for myself to make future edits easier, but I forgot to remove it for the post, sorry. – DGolberg Apr 16 '13 at 03:24

2 Answers2

2

Use JOptionPane instead.. it will be more easier to get the User selection result and then decide the logic code.. look here for example JOptionPane YES/No Options Confirm Dialog Box Issue -Java or here google search results: JOPtionPane examples

Community
  • 1
  • 1
Elior
  • 3,178
  • 6
  • 37
  • 67
0

I found the answer to my question here, and it was quite a bit simpler than I thought. As a result, I came up with the following code to return a boolean like I wanted without all the work involved with building my own window:

public static boolean confirmDelete(String msg) {
    String str = JOptionPane.showInputDialog(msg);
    if(str != null && str.toLowerCase().equals("delete")) return true;
    return false;
}

Elior's answer, while not quite what I was looking for, did point me in the right direction, so +1ed it. Not sure how I missed the showInputDialog() method for the JOptionPane, but it's basically what I was looking for (not a mere confirmation yes/no; the data is too sensitive for something as easy as that).

edit: added the str != null && otherwise it throws a nullPointerException on cancel.

DGolberg
  • 2,109
  • 4
  • 23
  • 31