how to implement confirm box using java.awt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mayur1234
    New Member
    • Mar 2007
    • 15

    how to implement confirm box using java.awt

    hi,
    i am new to java.Can annybody tell me how to implement confirm box using java.awt package.
    i tried by extending dialog class with 2 buttons (buttonSave and buttonDiscard) on dialog box but as ActionEventList ener has void return type i could not implement it.
    Can anybody tell me any other method.

    thanks in advance
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Mayur1234
    hi,
    i am new to java.Can annybody tell me how to implement confirm box using java.awt package.
    i tried by extending dialog class with 2 buttons (buttonSave and buttonDiscard) on dialog box but as ActionEventList ener has void return type i could not implement it.
    Can anybody tell me any other method.

    thanks in advance
    Your ActionListeners could set a boolean member variable to false or true,
    depending on which button was clicked. Add a nice convenience method to
    your class (which extends the Dialog class) that basically does just this:
    Code:
    private boolean confirmed= false; // set by the ActionListeners
    ...
    public boolean isConfirmed() {
       this.setVisible(true); // this thread will be suspended by the modal dialog.
       return confirmed;
    }
    In your calling code you just have to do this:
    Code:
    if ((new YourDialog(...)).isConfirmed())
       // user pressed the confirm/ok/save whatever button
    Your constructor initializes the entire YourDialog object but doesn't set it
    visible yet; that's the task of the isConfirmed() method.

    kind regards,

    Jos

    Comment

    • Mayur1234
      New Member
      • Mar 2007
      • 15

      #3
      thanks friend.
      it solved the problem. The solution is really good.
      Last edited by Mayur1234; Apr 15 '07, 02:58 PM. Reason: better response

      Comment

      Working...