-1

So I have a class that extends JFrame and implements a mouselistener that looks something like this :

public class CollageGallery extends JFrame implements MouseListener{
            :
            :     //stuff

   CollageGallery(){
            :
            : 
       addMouseListener(this)
   }
}

I need the mouse listener to go back to focus onto the previously created JFrame and not this one.

How can I achieve this?

harshalizee
  • 129
  • 1
  • 3
  • 12

1 Answers1

2

Assuming that you mean focus in the same way as used by the Java AWT / Swing Documentation, you would simply call requestFocusInWindow() or requestFocus() on the component that you want to have focused.

By the way, extending JFrame is not recommended. Are you really creating a new class of UI components, or is your intention to merely use JFrame in order to display your stuff? If the latter is the case, extension is the wrong thing.

As MadProgrammer commented, if you want to display a frame temporarily, consider using JDialog or JOptionPane instead of JFrame. JOptionPane has some nice static methods to which you could pass a JPanel in order to get it displayed in a dialog until the user presses a button like Ok or Cancel.

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
  • I just want to display stuff in a JFrame; I guess now in hindsight I should just create a JFrame inside the class rather than extend it. Also, how should the requestFocusInWindow() be called, especially since I need it only after a window is closed? – harshalizee Dec 07 '14 at 05:06
  • 2
    @harshalizee Perhaps consider using a `JDialog` or `JOptionPane` instead – MadProgrammer Dec 07 '14 at 06:32