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

Hi all
I am trying to add a modal JDialog to my JFrame (to use for the data entry), even if I have problems with the JFrame "focus". Basically, at the moment my program starts the JFrame and JDialog (loading program) very well. But then - if I move to another program (say, my browser) and then I try to go 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 notebook: when you open the Find/Replace box (even though it is not modal), you can switch to another program, and then when you switch again Notepad the two main frame and "JDialog" - esque box is always on display.

I tried to make it work for a couple of hours, but can't seem to. The closest I got is to add a WindowFocusListener for my JDialog and I hide it via setVisible once triggered windowLostFocus() (then my plan was to implement a feature similar in my class JFrame--though with windowGainedFocus - to redisplay the JDialog, i.e. once the user switches to the program). Unfortunately, this doesn't seem to work; I can't seem to get any window or window focus listeners actually pull all methods, in fact?

I hope that this kind of sense lol. In short, I'm looking for the Notepad CTRL + R-esque functionality, but with a modal box. With regard to a 'short ': code list

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 greatly appreciated.

Thank you!
Tristan

Set the image as the owner of the dialog box when you create the dialog box:

JFrame frame = new JFrame();
...
frame.setVisible( true );

JDialog dialog = new JDialog(frame);
...
dialog.setVisible( true );

Tags: Java

Similar Questions

  • I have problems with my Apple ID when you download applications, that it is said that your ID Apple has not yet been used in the iTunes Store

    I have problems with my Apple ID when you download applications, that it is said that your ID Apple has not yet been used in the iTunes Store

    What exactly is the problem? You are also being invited to review the account and enter payment details? If you did you are your details accepted - if they are, you should be able to remove them then.

  • Problems with clicking and scrolling when you are using the mouse in IE

    Separated from this thread.

    Original title:

    Problems with clicking and scrolling when you are using the mouse

    I have the same problem.  My touchscreen responds but my touchpad and mouse are unable to save a click in IE.  I need to reboot to rectify.  It seems to be more common when the laptop comes out of fashion 'sleep'.   I tried all the steps above, everything is up-to-date.

    Hello Fred,.

    Thanks for the reply.

    I appreciate your efforts to resolve the issue.

    I would suggest trying the following methods and check if it helps.

    Method 1:
    Run the hardware and devices Troubleshooter and check. Please follow these steps:

    a. press Windows + W keys, type Troubleshooting in the search box and press on Enter.
    b. click on 'show all' and then click 'hardware and devices'.
    c. click 'Next' and then follow the on-screen instructions.

    If this does not help, then use method 2.

    Method 2:
    Start your computer in safe mode and check the number.
    Refer to this article:
    Start settings for Windows (including safe mode)
    http://Windows.Microsoft.com/en-us/Windows-8/Windows-startup-settings-including-safe-mode

    I hope this information helps.

    Please let us know if you need more help.

    Thank you

  • Anyone who has a problem with the system crashes when importing using the new interface to import

    Anyone who has a problem with the system crashes when importing using the new interface to import

    Can specify you what Adobe program you use so that we do that your post is in the right forum?

  • Hello. I have a problem with the payment process when trying to buy the plan of creative photography of Cloud (one year). the error is: there was a problem processing your order, please contact our Service team customer care for assistance. AfricaBa North

    Hello. I have a problem with the payment process when trying to buy the plan of creative photography of Cloud (one year).

    the error is: there was a problem processing your order, please contact our Service team customer care for assistance. Middle East & North AfricaBahrain: 80081097Egypt: 08000000447Jordan/Kuwait/Lebanon/Qatar/Yemen: English + 44 207 365 0735Jordan/Kuwait/Lebanon/Qatar/Yemen: Arabic/French + 44 203 564 4145Oman: 80077173Saudi Arabia: 8008446638Tunisia, the Morocco and the Algeria: + 33 United Arab Emirates 157324642United: 80004443085Commonthwealth of independent States (CIS): + 44 207 365 0735

    Help me please to solve this problem. I need my lightroom immediately)

    Hi mohamed,.

    Please see the link below to buy the plan of photography:

    Pricing plans and creative Cloud membership | Adobe Creative Cloud

    In case you still have question, please contact sales at Adobe team.

    Adobe Middle East and North Africa

    Kind regards

    Sheena

  • Hello all: I have a problem with my Illustrator? When I export final work, the text that is does not, I can config it? I have try several times, but still cannot fix it.

    Hello all: I have a problem with my Illustrator? When I export final work, the text that is does not, I can config it? I have try several times, but still cannot fix it. inactiveforumnotifier Newsgroup_User Zoheir Illustrator

    What is the format of your final work? How are you viewing? What version of Illustrator do you use?

    Here are some common reasons why your type might be missing - is your type completely on the artboard and bleed?

    Is your type on a layer that is set up to print not? Is your white type and position it to overprint?

  • Having a problem with the Muse crashing when opening a Web site I designed in Muse.

    Having a problem with the Muse crashing when opening a Web site I designed in Muse. You need to be able to access this site in order to update for my clients.

    [LASP/updateCaches] TypeError: Error #1009: cannot access a property or method of a null object reference.

    All adventures Rafting | Wenatchee Whitewater Rafting trips - two hours East of Seattle

    Could you please send us the .muse file to [email protected] as well as a link to this topic? If the file is larger than 20 MB, you can use a service like Adobe SendNow, Dropbox, WeTransfer, etc.. Thank you.

  • Menu Spry, problems with background images in Internet Explorer

    Hello

    Looking for help from someone more qualified than me with some menus spry.  I have problems with background images don't show is not for some of my items menu in Internet Explorer.  Safari and firefox seem to accept it's ok.

    Here is a link to the menu: http://www.scottbrownwebdesign.com/beta/HTML/index.html

    and here is a link to the spry .css: http://www.scottbrownwebdesign.com/beta/HTML/SpryAssets/SpryMenuBarHorizontal.CSS

    I know that these are the elements that have the drop down or the side of the arrows.  I can't have two background images that I can?

    Thanks in advance,

    -Scott

    My first thought...

                         display: inline;          f\loat: left;          background: #FFF;
    
    Try declaring the background-image here as well... where it says not to mess with unless you know what you are doing.
    
    I'm just guessing though. If I had more time I'd look into it further but I do not.
    
  • Problems with porting boost::regex when ranging from CS6 to CC2014 using Xcode

    I have a job in CS6 project which uses the framework boost::regex I have porting problems in CC2014.

    I wore this project using Dolly to create a new project, and then face the old files source overall, finally update the output of various dated calls to API InDesign that appeared when I compiled. For the use of regular expressions, I added the debug and release builds of the boost_regex.framework to the respective targets.

    Now, for the version new projects of CC2014 debug builds with happiness. But when I switch to the Release version I get some errors from the linker. They all have something like:

    Undefined symbols for x86_64 architecture:

    'boost::re_detail::get_mem_block()', referenced from:

    Boost::re_details:perl_matcher < __gnu_cxx::__normal_iterator < const char *, std::string >, std::allocator < boost::sub_match < __gnu_cxx::__normal_iterator < const char *, std::string > > >, < char, < char > boost::cpp_regex_traits > boost::regex:regex_traits >: find_imp() in SbRegex.o

    etc.

    I did a bit of reading through the impetus of header files. The strange thing is that the 'get_mem_block()' seems to be set for option pre processor BOOST_REGEX_NON_RECURSIVE, while in the project contains parameters for BOOST_PREPROCESSOR_DEFINITIONS BOOST_REGEX_RECURSIVE. These two parameters are mutually exclusive, and it is logical to preprocessor that blocks with both cases, defined both.

    This recursive option is used to control the use of memory when processing a regular expression, then see a call get_mem_block() (other errors of link imply a call to put_mem_block) somehow made sense being called that look like something to do with memory.

    I'm trying to understand why the debug builds, but Release version fails. This problem sound familiar to anyone? Anyone have any suggestions to try to fix it?

    Figured out how to build the project when you add the framework for regular expression of thrust.

    The key is the optimization level. When Dolly creates a project it defines the level of optimization for the Release version of "-O3". When you use boost::regex, you can only link correctly if you use the O0 and O1 optimization levels. Highest and link errors I saw appear.

    For the Release version, so I put in the Release_Cocoa64 build parameters 'Optimization level' to ' Fast [-O, O1] ' and it compiles.

  • Border problems with background image

    Hello

    I have a problem with my background image. I have fillings of browser size the original value and activated scale option in the middle. The image has a resolution of 1920x1080px. But when I now change the size of my browser I get a white border on the left or on the right size.


    Small to large size of Office Office:

    https://DL.dropboxusercontent.com/u/84237067/kleinzugross.PNG

    Large size of small office desktop:

    https://DL.dropboxusercontent.com/u/84237067/grosszuklein.PNG

    Hope you understand me, my English for this specific part is not perfect it.

    Greetings

    Marcel

    I checked out the site but don't see any white space on the page with large screens, I think that you have already solved the problem?

    Thank you

    Sanjit

  • Problem with sending a message when you're connected via Wi - Fi

    Hello. Still getting used to my Dext (owned it for about 4 days). I noticed this morning that I can't send the mail from the POP3 account, I set up on my Dext when I am connected via Wi - Fi - when I try I get an "authentication failure". When I disable Wi - Fi email sends end. There is no problem with webmail - send although I am connected to the Wi - Fi or 3 G network.

    Anyone know how to send the mail while POP3 on the Wi - Fi network? Thanks in advance...

    Understand the problem - port incorrect settin for mail server outgoing. Changed to 587 according to some messages that I saw during a search - that did the trick!

  • Why am I getting the message - sorry theres a problem with hotmail right now - when I try to open hotmail

    When I try to open my hotmail page, I get the following message - sorry theres a problem with hotmail now - why this phenomenon occur?

    Hotmail support:

    http://windowslivehelp.com/product.aspx?ProductID=1>

    UTC/GMT is 11:06 on Wednesday, February 15, 2012

  • I have a problem with the cursor jumps when entering text.

    Original title: cursor jumping when entering text

    I use Microsoft 7 and the cursor keeps jumping when I type. This causes the text to be misplaced. Some answers I've seen only direct you to the hardware options but never directly any specific solution. Grateful for ideas?

    Hey Joe,

    Thanks to join Microsoft Community where you will find all the required information on the Windows operating systems!

    The problem may occur due to the speed of the pointer mouse inappropriate or if the sensitivity of the touchpad is too high.

    To help you better, we need more information.

    What is brand and model computer?

    To work with the issue, I suggest you follow these steps:

    Method 1: Hardware store

     

    Open the hardware and devices Troubleshooter

    http://Windows.Microsoft.com/en-us/Windows7/open-the-hardware-and-devices-Troubleshooter

    Method 2: Update mouse drivers

     

    Updated a hardware driver that is not working properly

    http://Windows.Microsoft.com/en-us/Windows7/update-a-driver-for-hardware-that-isn ' t-work correctly

     

    Method 3:

     

    Change the settings of the mouse pointer.

     

     

    Refer to section to change the mouse pointer works on the site:

    Change the settings of the mouse

    http://Windows.Microsoft.com/en-in/Windows7/change-mouse-settings

    Method 4:

    If you use a laptop, change the sensitivity of the touchpad.

    See solving problems with your touchpad in more options in the site:

     

    Mouse, touchpad and keyboard with Windows problems

    http://Windows.Microsoft.com/en-in/Windows/help/Mouse-touchpad-and-keyboard-problems-in-Windows

     

    I hope this helps. If the problem persists, or if you have problems of Windows in the future, let us know and we would be happy to help you.

  • Windows 7 laptop. I have problems with my cursor. When I type, it jumps and anywhere else except where I type moves.

    Hello:

    I am pleased to say that I have really only one problem left to solve.  I have a Dell Inspiron N5010 with Windows 7.  I started having this problem immediately about two or three weeks after the purchase of this computer.  I have problems with my cursor.  A Laptop store owner who corrects laptops told me ' you type you're probably hit a key that throws the slider out where you type and be aware of this.  I really don't think that.

    Is that the problem is that whenever I type just arbitrarily the cursor jump to other sections of my screen where I previously typed something.  It's very frustrating.  Can you really fix that because a tech support person told me that "which cannot really be fixed with someone to take control of your computer, or there is a problem with the keyboard".  That's what he says.  "It could be a problem with the keyboard" and he concluded that it might have to be taken in a store or (sent to DELL (and I say no, no, no (Amy Whinehouse)).)  A lot of smiles.

    But in any case this problem can be corrected as any other problem?  I hate it when this cursor jumps around on the screen to the typed sections previously.  It does not have it on my desk.

    Please let me know.

    Lavenderjade

    Hello

    1. the problem occurs with any particular application or all applications?
    2. don't you make changes to the computer before the show?

    Method 1
    The issue may be due to some third-party program conflicts. I suggest you perform the clean boot and check if the problem persists.

    How to troubleshoot a problem by performing a clean boot in Windows Vista or in Windows 7
    http://support.Microsoft.com/kb/929135
    Note: Follow step 7 clean boot KB929135 article to reset the computer in normal mode.

    Method 2
    I suggest that you try to reduce the sensitivity of the touchpad and check if that helps.

    a. turn on your computer and log in to Windows as usual. Open the "Start" menu and click on the "Control Panel" option.

    b. double-click the icon named "Mouse"... The "Mouse properties" window opens. Go to the tab "device settings". Click on the button "settings". A window titled "Properties" appears.

    c. examine the left panel of the 'Properties' window and expand the option "sensitivity." Click on the option "Sensitivity to touch."

    d. reduce tactile sensitivity in moving the slider to the right, to the label of "heavy touch. Experiment with different levels of sensitivity until find you one suited to your needs.

    e. click the 'OK' button to close the "Properties" window Repeat the operation with the control panel "mouse". Your new touchpad sensitivity settings are now saved.

    Method 3
    I suggest you to update the drivers for the touchpad on the manufacturers Web site.
    http://www.Dell.com/support/drivers/us/en/555

    More information
    Mouse, touchpad and keyboard with Windows problems
    http://Windows.Microsoft.com/en-us/Windows/help/Mouse-touchpad-and-keyboard-problems-in-Windows?T1=tab04

  • Problems with the graphics card when you use AutoCad

    Hi, I have a dell xps 18.4 - i7 with 8 GB of RAM, when I'm working with Auto CAD I face problem with my graphic card, what can I do to fix this? has any experience with that? the graphics card is Intel(r) HD Graphics 4000

    Hello

    Thanks for the reply.

    I'm sorry for the late reply.

    I appreciate your patience.

    I suggest you to try installing the driver in compatibility mode and tick.
    Reference:
    Make the programs more compatible with this version of Windows
    http://Windows.Microsoft.com/en-us/Windows-8/older-programs-compatible-version-Windows

    Please keep us informed.

    Thank you

Maybe you are looking for