Results 1 to 9 of 9
  1. #1
    gmn1 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Exclamation help with JOptionPane.showMessageDialog()

    hi can someone please help me with JOptionPane.showMessageDialog();
    all i want to do is set the dialog visible when a button is clicked. it must display a message, have the warning icon in the corner and have an option to click either yes or no buttons in the dialog. also i would like to write further code for when each of the butons are clicked.

    this is my code, its not working
    JOptionPane.showMessageDialog(null,"Are you sure you want to delete: " + selected, "Warning", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION);

    please help.
    much appreciated

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    What is the problem your seeing? What is not working? Imagine taking your car to the mechanic and saying "It's not working".... not much information for him to fix it.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

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

    Default

    My guess: you've got a problem with the button's action listener, either it's not set up correctly or it has not been added to the button,... but this is only a SWAG. As Chris correctly points out, you'd do well to provide us with more detail, a lot more detail.

    Best of luck.

  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

    I think he wants to pop-up a message box on a click event and depend on the message result want to handle the dialog/frame. Not clear on me too.

  5. #5
    gmn1 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    ok soz the error says: cannot find symbol : method showMessageDialog(<nulltype>,java.lang.String,java .lang.String,int,int)

    but i got that code from the java tutorial website: java.sun.com/docs/books/tutorial/uiswing/components/dialog.html]How to Make Dialogs (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)

    the original is:
    final JOptionPane optionPane = new JOptionPane(
    "The only way to close this dialog is by\n"
    + "pressing one of the following buttons.\n"
    + "Do you understand?",
    JOptionPane.QUESTION_MESSAGE,
    JOptionPane.YES_NO_OPTION);

    all i did was swap words to get what i wanted but it gives me that error message^^^.

    in my code...
    JOptionPane.showMessageDialog(null,"Are you sure you want to delete: " + selected, "Warning", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION);
    i even tried to take out the JOptionPane.WARNING_MESSAGE so that JOptionPane.YES_NO_OPTION was left, i ran the program and the message dialog did not have a yes no option, it just had a "ok" option.

    yes eranga, u do have an idea, also i dont know how to get the action listener of the clicked button so i can write code to act on that

    if there is anything else you need to help me i will glady provide it.
    thnx
    much appreciated

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    What made you add a null as the first parameter? The tutorial example you posted doesn't have that!

    And take a look at JOptionPane#showOptionDialog, it returns an int value by which you can determine which button was clicked, and take action accordingly. Read the API for the method to see what parameters it takes.

    Adding ActionListeners to a JOptionPane's buttons is a non-trivial exercise.

    db

  7. #7
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    there exist 3 showMessageDialog method
    showMessageDialog(Component parentComponent, Object message)
    showMessageDialog(Component parentComponent, Object message, String title, int messageType)
    showMessageDialog(Component parentComponent, Object message, String title, int messageType, Icon icon)

    what you use is
    showMessageDialog(Component parentComponent,java.lang.String,java .lang.String,int,int)
    which not one of above 3 method


    the original you stated was constructor, not a static method of the class
    i suggest to use a constructor if you want to use JOptionPane.QUESTION_MESSAGE with JOptionPane.YES_NO_OPTION
    Last edited by mtyoung; 03-30-2009 at 06:35 AM.

  8. #8
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    Probably best creating a JFrame and putting the dialog into that frame where 'null' is.

  9. #9
    gmn1 is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    thnx 4 help and effort but figured it out. my code is:

    int answer = JOptionPane.showConfirmDialog(null, "Are You Sure You Want to Delete: " + selected + "?", "Are you Sure?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
    if (answer == JOptionPane.YES_OPTION) {
    System.out.println("Deleted");}

Similar Threads

  1. Sick of duplicating JOptionPane.showMessageDialog
    By gdanelian in forum New To Java
    Replies: 2
    Last Post: 11-18-2008, 06:53 PM
  2. Replies: 1
    Last Post: 10-29-2008, 08:50 AM
  3. About JOptionPane.showMessageDialog
    By jhetfield18 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2007, 11:45 PM
  4. About JOptionPane.showMessageDialog
    By jhetfield18 in forum Advanced Java
    Replies: 0
    Last Post: 11-02-2007, 11:56 AM
  5. Replies: 1
    Last Post: 07-31-2007, 05:01 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
  •