Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Pop-out box using a JFrame.

  1. #1
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Question Pop-out box using a JFrame.

    Hi all,

    I'm building a simple application to store and retrieve student records (sort of a limited database). What I have is a help box that appears from a drop-down menu, appearing as a JFrame. Here is the source Java code for the HelpFrame class:

    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    public class HelpFrame extends JFrame
    {
      public HelpFrame()
      {
        super(" Quick guide ");
      }
    }

    What I want to do is to stop multiple quick-help guides appearing (ie, if you click 100 times on that menu item, you end up with 100 boxes) - actually I've solved this. What I haven't solved is the opposite: that only one HelpFrame will appear until it's closed. What I want it to do is that when you click the menu again, and it appears again... but it doesn't. I've tried putting lots of conditions in this anonymous inner-class:

    public class ControlGUI
    {
      public static int help=0;
      /* Rest of code here until... */
      /* Uses an anonymous inner-class */
    helpAction.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent arg0)
      {
        help=help+1;  // This sets up a condition to stop multiple help-boxes appearing
        if (help==1)  // We know help is declared as zero, so on the first click...
        {
          /* This sets the parameters for the pop-out box... */
          HelpFrame popOut0=new HelpFrame();
          popOut0.setSize(600,400);
          popOut0.setResizable(false);
          popOut0.setBackground(Color.white);
          popOut0.setForeground(Color.black);
          popOut0.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
          popOut0.setAlwaysOnTop(true);
          popOut0.setLocation(20,40);
          popOut0.show(true);
        }
      }
    });

    Is there any way to check that the JFrame HelpFrame has been closed, so that the value 'help' can be reset to zero??? Ie, once you've closed it, you may re-open the quick help guide only once at a time.

    There must be a simple way, I've tried lots of if variants, but can't seem to get it to work.

    Any help will be greatly appreciated. You will win... a potatoe crisp (or potatoe chip as you may say in North America!).

    Many thanks,

    Shaun.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pop-out box using a JFrame.

    Instead of making a new HelpFrame each time, why don't you just keep track of a single HelpFrame declared outside of the actionPerformed() method?

    Or use a dialog.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Pop-out box using a JFrame.

    Quote Originally Posted by KevinWorkman View Post
    Instead of making a new HelpFrame each time, why don't you just keep track of a single HelpFrame declared outside of the actionPerformed() method?
    Why didn't I think of that at 2am this morning? Good call. PM me your address and I'll send your crisp :-)

    As for using a dialog... I'll search for some examples. Thanks.

    Regards,

    Shaun.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pop-out box using a JFrame.

    Haha no problem. If you use only a single instance of HelpFrame, you don't have to check anything before setting its visibility- if it's already visible, nothing will happen. So you can get rid of that int variable and if statement.

    A dialog is the way to go if you want, say, a message box to pop up that will block its parent frame until you close it, or if you want to get user input. There are a ton of other uses for dialogs, so they're worth investigating: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    I actually did send you my address, but I'd be thrilled with a postcard! I love postcards! :p
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Cool Re: Pop-out box using a JFrame.

    And the solution is...

    /* Put this at the beginning somewhere... */
    final HelpFrame popOut0=new HelpFrame();
    popOut0.setSize(600,400);
    popOut0.setResizable(false);
    popOut0.setBackground(Color.white);
    popOut0.setForeground(Color.black);
    popOut0.setAlwaysOnTop(true);
    popOut0.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    popOut0.setLocation(20,40);
    popOut0.setVisible(false);
    /* Rest of code until the task bar thingy is clicked, and so... */
    helpAction.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent arg0)
      {
        popOut0.show(true);
      }
    });

    It has to be declared as final so that there are no new instances set up (as Kevin suggested). Also, all of the code is now outside the inner-class, also as Kevin hinted at.

    Simples! It makes sense now I've had some sleep.

    Regards,

    Shaun.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Pop-out box using a JFrame.

    It has to be final so that it can be accessed inside your ActionListener, which is called an anonymous inner class. Another way to do it, without the final (actually I'd recommend the final anyway, just because you don't plan on changing the variable ever), and to narrow the scope, would be something like this:

    helpAction.addActionListener(new ActionListener()
    {
     
    final HelpFrame popOut0=new HelpFrame();
    popOut0.setSize(600,400);
    popOut0.setResizable(false);
    popOut0.setBackground(Color.white);
    popOut0.setForeground(Color.black);
    popOut0.setAlwaysOnTop(true);
    popOut0.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    popOut0.setLocation(20,40);
    popOut0.setVisible(false);
     
      public void actionPerformed(ActionEvent arg0)
      {
        popOut0.show(true);
      }
    });

    That way your outer class doesn't have to worry about the HelpFrame at all. Another way to do this would be to just write another outer class for the ActionListener.

    Edit- Okay, that stuff might not be allowed to go there, so you could put it in a constructor or a declaration block. But you get the gist.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Pop-out box using a JFrame.

    Quote Originally Posted by KevinWorkman View Post
    It has to be final so that it can be accessed inside your ActionListener, which is called an anonymous inner class. Another way to do it, without the final (actually I'd recommend the final anyway, just because you don't plan on changing the variable ever), and to narrow the scope, would be something like this:

    helpAction.addActionListener(new ActionListener()
    {
     
    final HelpFrame popOut0=new HelpFrame();
    popOut0.setSize(600,400);
    popOut0.setResizable(false);
    popOut0.setBackground(Color.white);
    popOut0.setForeground(Color.black);
    popOut0.setAlwaysOnTop(true);
    popOut0.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    popOut0.setLocation(20,40);
    popOut0.setVisible(false);
     
      public void actionPerformed(ActionEvent arg0)
      {
        popOut0.show(true);
      }
    });

    That way your outer class doesn't have to worry about the HelpFrame at all. Another way to do this would be to just write another outer class for the ActionListener.

    Edit- Okay, that stuff might not be allowed to go there, so you could put it in a constructor or a declaration block. But you get the gist.
    Thanks again - we did something about anonymous inner-classes at some point during my studies, I'm just showing that I understand what's meant by that by implementing it in my project.

    Still so much to do, probably more questions to follow!

    Regards,

    Shaun.

Similar Threads

  1. JFrame
    By M3ss1ah in forum AWT / Java Swing
    Replies: 3
    Last Post: February 23rd, 2011, 05:00 PM
  2. JFrame
    By M3ss1ah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 04:24 PM
  3. FileDialog with JFrame
    By nigi10 in forum AWT / Java Swing
    Replies: 3
    Last Post: December 3rd, 2010, 10:20 AM
  4. Print out a JFrame
    By ellias2007 in forum AWT / Java Swing
    Replies: 8
    Last Post: June 17th, 2010, 06:15 AM
  5. JFrame help
    By Uden in forum AWT / Java Swing
    Replies: 0
    Last Post: August 14th, 2009, 01:37 PM