Results 1 to 3 of 3
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    245
    Rep Power
    8

    Default Creating a Dialog from within an ActionListener

    I am struggling to create a modal dialog at the moment.

    I want the dialog to be created when a button is pressed, therefore I used:

    Java Code:
    BUTTON.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                     //dialog code
                }
    However, the button is added to the JFrame that I want linked to the dialog. Thus I do not know how to link the two.
    This is because the constructor for the dialog is

    Java Code:
    JOptionPane.showMessageDialog(LinkedJFrame, "INSERT MESSAGE HERE");

    My initial thought was to use "this" but that just refers to the actionListener object.

    All of this code is in the constructor for the JFrame as this is all part of the frame's setup.

    How do I reference the JFrame from within itself?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Creating a Dialog from within an ActionListener

    You can usually qualify a this reference with the class you are talking about. E.g. JFrame.this. You can also declare either a local final variable to contain the reference of the JFrame or create an instance field to house the JFrame reference. Either of those could be used in your dialog invocation. If you have additional problems, please include a Short, Self Contained, Correct Example that demonstrates the problem.

    Note: You cannot use a this reference in a static context.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    245
    Rep Power
    8

    Default Re: Creating a Dialog from within an ActionListener

    Quote Originally Posted by jim829 View Post
    You can usually qualify a this reference with the class you are talking about. E.g. JFrame.this.
    Never knew you could do this, worked perfectly, thank you!

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. ActionListener w/ Dialog
    By BariMutation in forum New To Java
    Replies: 1
    Last Post: 12-03-2010, 04:50 PM
  3. Problem creating dialog box
    By SeanC in forum New To Java
    Replies: 2
    Last Post: 08-13-2010, 05:38 PM
  4. Creating a Number Input Dialog in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:53 PM
  5. Creating a dialog to input user/password
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 07:03 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
  •