Results 1 to 12 of 12
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default problem on returning values..

    hello

    i am trying to make an application that has two objects, the textfield and the clear button..

    made up of two classes the main class which where the textfield and button stays and another class that is called everytime i click the clear button(a confirmation dialog box)..

    everytime i would click the clear button, a dialog box should pop up asking the user if he really is sure about clearing the texfield..

    if the user would choose yes, the callable class would return a digit to the main class which is going to be the cue of what the program should do.

    is that possible? making a sub class return a value to the class that called it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, I believe it is, but you may do well to give us more information on what context you are doing this coding in. Are you coding in Swing? AWT? SWT? JSP?

  3. #3
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default hi

    first of all, i would like to thank you for viewing this thread.. really appreciate it.

    we are using awt

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok now, what you have tried so far? I mean how did you call the second class on button click event and so on.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Have you looked at JOptionPane.showConfirmDialog()...?

  6. #6
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default hello

    we aren't really that familiar with javax objects mr. neil



    i tried to call the confirmation dialog box from the main class's actionPerformed method.. and it does pop up whenever i click the clear button..

    just have a problem on how to return a value to the main class that called..

    i tried this

    class called extends Dialog implements ActionListener
    {
    public static int called()

    {

    **called dialog box**

    **can't return a value**

    }

    public void actionPerformed(ActionEvent e)
    {

    ** **

    }
    }

    calling the called method wont return any value because it'll just be popping on the screen and i still need to get the result of the user if he clicked the yes or cancel button...

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    a cheap and easy way is to have a field in the parent class that the inner class updates. The calling method can then look at this field after the listener has executed.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by kulangotski View Post
    calling the called method wont return any value because it'll just be popping on the screen and i still need to get the result of the user if he clicked the yes or cancel button...
    How did you return the value? Can you show your code here.

  9. #9
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default ei

    ei OD, tnx for the post..

    now i get the point, but should that field be declared static?


    this was what i did before

    class called extends Dialog implements ActionListener
    {
    public static int called()

    {

    **called dialog box**

    return *number*

    }

    public void actionPerformed(ActionEvent e)
    {

    ** **

    }
    }

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    What happen when you return static/non-static variable? Did you get any error message, and did you read it carefully?

  11. #11
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Using JOptionPane is very simple (see java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#button).

    For example:
    Java Code:
    //default icon, custom title
    int n = JOptionPane.showConfirmDialog(
        frame,
        "Are you sure you want to delete the text?",
        "Deletion Confirmation",
        JOptionPane.YES_NO_OPTION);
    
    if (n == JOptionPane.YES_OPTION) {
       deleteTheText();
    }
    The JOptionPane itself can be the second class you describe, which returns the result of the question to the main class as above.

  12. #12
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default ei

    we really aint introduced to that jOption thing that your saying but it does look easier because creating a dialog box in awt means creating another class and this dialog that you've showed me here looks much more simpler

    tnx for ya'll who gave me answers, now i get it..
    appreciate it..

Similar Threads

  1. Problem in printing JTable values
    By shanssat in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2009, 09:15 AM
  2. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  3. Replies: 11
    Last Post: 12-08-2008, 01:29 AM
  4. Returning array problem.
    By Chase in forum New To Java
    Replies: 4
    Last Post: 10-21-2008, 10:07 PM
  5. Replies: 0
    Last Post: 12-01-2007, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •