Problems with 'background' JFrame focus when adding a modal JDialog

Hi all,
I'm trying to add a modal JDialog to my JFrame (to be used for data entry), although I'm having issues with the JFrame 'focus'. Basically, at the moment my program launches the JFrame and JDialog (on program load) fine. But then - if I switch to another program (say, my browser) and then I try switching back to my program, it only shows the JDialog and the main JFrame is nowhere to be seen.
In many ways the functionality I'm looking for is that of Notepad: when you open the Find/Replace box (albeit it isn't modal), you can switch to another program, and then when you switch back to Notepad both the main frame and 'JDialog'-esque box is still showing.
I've been trying to get this to work for a couple of hours but can't seem to. The closest I have got is to add a WindowFocusListener to my JDialog and I hide it via setVisible(false) once windowLostFocus() is fired (then my plan was to implement a similar functionality in my JFrame class - albeit with windowGainedFocus - to show the JDialog again, i.e. once the user switches back to the program). Unfortunately this doesn't seem to work; I can't seem to get any window or window focus listeners to actually fire any methods, in fact?
I hope that kind of makes sense lol. In short I'm looking for Notepad CTRL+R esque functionality, albeit with a modal box. As for a 'short' code listing:
Main.java
// Not all of these required for the code excerpt of course.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
public class Main extends JFrame implements ActionListener, WindowFocusListener, WindowListener, FocusListener {
     static JFrame frame;
     private static int programWidth;
     private static int programHeight;
     private static int minimumProgramWidth = 700;
     private static int minimumProgramHeight = 550;
     public static SetupProject setupProjectDialog;
     public Main() {
          // Setup the overall GUI of the program
     private static void createSetupProjectDialog() {
          // Now open the 'Setup Your Project' dialog box
          // !!! Naturally this wouldn't auto-open on load if the user has already created a project
          setupProjectDialog = new SetupProject( frame, "Create Your Website Project", true );
          // Okay, for this we want it to be (say) 70% of the progamWidth/height, OR *slightly* (-25px) smaller than the minimum size of 700/550
          // Change (base on programWidth/Height) then setLocation
          int currProgramWidth = getProgramWidth();
          int currProgramHeight = getProgramHeight();
          int possibleWidth = (int) (currProgramWidth * 0.7);
          int possibleHeight = (int) (currProgramHeight * 0.7);
          // Set the size and location of the JDialog as needed
          if( (possibleWidth > (minimumProgramWidth-25)) && (possibleHeight > (minimumProgramHeight-25)) ) {
               setupProjectDialog.setPreferredSize( new Dimension(possibleWidth,possibleHeight) );
               setupProjectDialog.setLocation( ((currProgramWidth/2)-(possibleWidth/2)), ((currProgramHeight/2)-(possibleHeight/2)) );
           else {
               setupProjectDialog.setPreferredSize( new Dimension( (minimumProgramWidth-25), (minimumProgramHeight-25)) );
               setupProjectDialog.setLocation( ((currProgramWidth/2)-((minimumProgramWidth-25)/2)), ((currProgramHeight/2)-((minimumProgramHeight-25)/2)) );
          setupProjectDialog.setResizable(false);
          setupProjectDialog.toFront();
          setupProjectDialog.pack();
          setupProjectDialog.setVisible(true);
     public static void main ( String[] args ) {
          Main frame = new Main();
          frame.pack();
          frame.setVisible(true);
          createSetupProjectDialog();
        // None of these get fired when the Jframe is switched to. I also tried a ComponentListener, but had no joy there either.
     public void windowGainedFocus(WindowEvent e) {
          System.out.println("Gained");
          setupProjectDialog.setVisible(true);
     public void windowLostFocus(WindowEvent e) {
          System.out.println("GainedLost");
     public void windowOpened(WindowEvent e) {
          System.out.println("YAY1!");
     public void windowClosing(WindowEvent e) {
          System.out.println("YAY2!");
     public void windowClosed(WindowEvent e) {
          System.out.println("YAY3!");
     public void windowIconified(WindowEvent e) {
          System.out.println("YAY4!");
     public void windowDeiconified(WindowEvent e) {
          System.out.println("YAY5!");
     public void windowActivated(WindowEvent e) {
          System.out.println("YAY6!");
     public void windowDeactivated(WindowEvent e) {
          System.out.println("YAY7!");
     public void focusGained(FocusEvent e) {
          System.out.println("YAY8!");
     public void focusLost(FocusEvent e) {
          System.out.println("YAY9!");
SetupProject.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class SetupProject extends JDialog implements ActionListener {
     public SetupProject( final JFrame frame, String title, boolean modal ) {
          // Setup the JDialog
          super( frame, title, modal );
          setDefaultCloseOperation( JDialog.DISPOSE_ON_CLOSE );
          // Bad code. Is only temporary
          add( new JLabel("This is a test.") );
          // !!! TESTING
          addWindowFocusListener( new WindowFocusListener() {
               public void windowGainedFocus(WindowEvent e) {
                    // Naturally this now doesn't get called after the setVisible(false) call below
               public void windowLostFocus(WindowEvent e) {
                    System.out.println("Lost");
                    setVisible(false); // Doing this sort of thing since frame.someMethod() always fires a null pointer exception?!
}Any help would be very much greatly appreciated.
Thanks!
Tristan

Hi,
Many thanks for the reply. Isn't that what I'm doing with the super() call though?
As in, in Main.java I'm doing:
setupProjectDialog = new SetupProject( frame, "Create Your Website Project", true );Then the constructor in SetupProject is:
public SetupProject( final JFrame frame, String title, boolean modal ) {
          // Setup the JDialog
          super( frame, title, modal );
          And isn't the super call (since the class extends JDialog) essentially like doing new JDialog(frame,title,modal)?
If not, that would make sense due to the null pointer exception errors I've been getting. Although I did think I'd done it right hence am confused as to the right way to handle this,if so.
Thanks,
Tristan
Edited by: 802573 on 20-Oct-2010 08:27

Similar Messages

Maybe you are looking for