简体   繁体   中英

Java Swing reset dialog fields and close it when click Cancel button

Have a main frame that has a button.

Once that button on the main frame gets clicked, a custom dialog box with two buttons appears(setVisible(true)). That dialog box has a bunch of textboxs, spinners, radio buttons... One of the buttons on that dialog is called "Cancel".

What I'm trying to do is make it so when I click Cancel, all of the fields get cleared/reset to default values and then close the dialog(setVisible(false)).

Problem is it doesn't reset radio buttons and also I get exception when the dialog tried to reset the date because of IllegalArumentException(so I removed it).

Also is there a Date control in NetBeans? I use spinner with modified model but it's a little awkward, plus it makes it hard to reset the date?

private void btnAcceptActionPerformed(java.awt.event.ActionEvent evt) {

    String err = "";

    if(txtFirstName.getText() == "")
        err += "First Name is required";
    if(txtLastName.getText() == "")
        err += "Last Name is required";
    if(txtId.getText() == "")
        err += "Id is required";

    javax.swing.JOptionPane.showMessageDialog(this.CreateReservation, err);

}

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
    txtFirstName.setText("");
    txtLastName.setText("");
    spinAge.setValue(18);
    txtId.setText("");
    radio1.setSelected(false);
    radio2.setSelected(false);
    DialogCustom.setVisible(false);
}

Date Control: check out JXDatePicker

Design issue: why reset the fileds when the dialog is closed? Why not set the proper state before it's shown? (just my opinion)

radio1.setSelected(false) should work IMO - there must be another issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM