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
-
I have Windows 7 and just recently experienced a problem with itunes freezing up when I clicked on the "apps" tab which shows the apps that are loaded on my phone. Everything else in the itunes software seems to be working okay. The problem is in the part where you sync the newly purchased apps. The apps will all be blank that are downloaded and that part of the software crashes itunes or freezes up. I can get out of itunes okay after the freeze up, but it just stops me from syncing anything that I have newly added. If anyone has had this problem before or knows someone that has, please let me know how I can fix it. I have uninstalled and reloaded itunes 4 or 5 times, but the problem is still there. I might add that I have been using itunes and purchasing apps for 2 to 3 years without any problems. HELP.
You would get better response from the iTunes community forum.
Have a nice day! -
Ive used compressor and then tried to put it in dvdsp put its not working. I am having problems with the audio or when i burn it , i cant play the dvd? any help would be appreciated
I suspect you are attempting to burn an HD DVD, which will not play on a DVD player...
But if you provide more details such as the settings you are applying in Compressor, then we would be able to provide you more accurate information.
How are you adding the files to Compressor?
In FCP, what are your footage properties (command-9) and sequence settings (command-0) -
When I turn my computer on (Windows 7) I receive a message that Apple wants to make a change to the hardware. I click on yes but nothing happens. The change has something to do with Apple "push". I recently had a problem with"push" on iTunes when I tried to sync my iPod Touch. I have latest software for iTunes and iPod. Can anyone tell me what to do to get this change to install?
ptford wrote:
When I turn my computer on (Windows 7) I receive a message that Apple wants to make a change to the hardware
Since hardware cannot be changed with software, perhaps you would be so kind as to post the exact wording of the message? -
i have some problem with my computer because when i turn it up, the screen turns in a blue colour and the sound doesn't work and in this way i can't hear anything
Will u help me or not?
-
I have a problem with two PDF's when trying to open them through a link on a web page. The two PDF's open fine with Adobe on my own PC and on the server I have copied it to but when they are opened through a link on a web page (pointing to the server where the PDFs open fine) I get an error 'There was an error processing a page. Invalid function resource' The other one just doesn't open at all. Can anyone help with this please?
Hello,
Are the pdf linked correctly in the website? Is this a public website? If yes, please post the link here.
~Deepak -
Anyone having problems with Elements organiser 13 when try to import photos from Mac photos which was an upgrade from Iphotos on 9th April 2015. Before when I was using Iphotos all was ok...
hi I have been trying to do this, but PE13 comes up with a fail message saying that file types are not supported, am only trying to import jpeg files so can't understand this. So far have been unable to use PE13 at all, any ideas anyone please!
-
Im have a problem with my adobe id when i try to covert pdf files.
Im have a problem with my adobe id when i try to covert pdf files.
I think your question should go in the ExportPDF forum,
http://forums.adobe.com/community/exportpdf -
Hello I have encounted a problem with Photoshop. Normally when you start a new documant, you are given the options to have A4, A3, ect page set ups. These inbuilt page set ups seem to have disapeared, does anyone please know how to fix this? Many thanks Ben [email protected]
Hello Trevor
Thanks for your message. You can custom set pages but there are no preset of page sizes like A4, A3, ect. I cannot click the size preference, nothing comes up
Ben -
hi, i have problem with my apple id when i want purchase app even free one, asked me for complete my card informations... but i don't want to use debit card because already i have gift card and charge it in my id... please help me.
when i unchek the card information it was show me an error!Though you are being asked for card details it doesn't necessarily mean that the card will be charged for the purchase (unless you want to gift content). Have you tried entering your card's details to confirm the account ? You should then be able to remove it.
From http://support.apple.com/kb/HT5582 :
When making purchases, content credits are used first, followed by Gift Certificate, iTunes Card, or Allowance Account credits; your credit card or PayPal account is then charged for any remaining balance
Is it a new account ? If it is then unless a new account is created by following the instructions on this page (the instructions won't work with existing accounts) : http://support.apple.com/kb/HT2534
then you will need to enter card details (debit cards are no longer accepted in all countries) before the account can be used to download any item from the store. -
I have a problem with elements 8. When ever I start a new project, the program searches for the old, latest used filmmaterial, I have used before. That means, if I use a different DVD, it take so much time, until the program starts. So how can I stopp the automatically uploading of old material?
ThanksYou have 2 unreachable statements in this method.
public static int eval(String s2, String op, String s3) {
return lookup(s2);
return lookup(op);
return lookup(s3);
} You're missing a } at the end of this method:
public static int lookup(String s) {
for(int k = 0; k < symbols.length; k++){
String symbol = symbols[k];
if(s.equals(symbol))
return k;
}You have some loose } and ; at the end of the file:
public static void main(String args[])
commandline();
} -
Problem with background formatting in pivot table
Hello,
I have a problem with background formatting in pivot table. I can't change background (light blue rectangular in upper left corner). Do you have any idea how to do it?
Here is the link to the picture of the pivot table:
http://img163.imageshack.us/img163/110/unledfyq.png
I would be grateful for any help.
Regards,Hey,
Check this link once, you will need java script to acheive this:
http://everythingoracle.com/obieehdom.htm
scroll down to this topic: Parsing using HTML DOM
Please award points if helpful.
Thanks,
-Amith. -
IS ANY ONE HAVING PROBLEM WITH LIGHTROOM CC BRUSH WHEN THE GPU IS TURN ON
F. McLion wrote:
Update your graphic card driver to the latest version.
btw.: Rule number 2 - Do not use all caps - Netiquette Wiki
And for a very practical reason from your point of view: using all caps looks like SHOUTING, and people will concentrate more on the SHOUTING than on answering your question. Some will just ignore you for this reason.
If updated drivers don't fix the problem, perhaps you can tell us what is the "PROBLEM WITH LIGHTROOM CC BRUSH WHEN THE GPU IS TURN ON" that you are having. -
My daughter's older Ipod Touch is having a problem with images becoming pixilated when taken from an e-mail and placed into her camera roll. ( She wants to put on Instagram ) Any help?
I think that may be a consequence of the email provider she is using. Some services (AOL is one example) inspect and modify email attachments, particularly photos, resulting in reduced resolution compared to the email that was sent.
The iPod does not do this. The resolution of the picture in the email she receives will be identical to what is imported. -
İ have a problem with my itunes accounts.when i want to buy in-app features i got an error that;
"Please comtact with itunnes support for completing this process"
İ checked my device settings and tried my account with another ios device but result not changed.i still get same error.my credit card address info and my account address info same with each other.i tried my other credit cards but i got same error.
Please check my account details for solving this issue.
My account details :
email: [email protected]These are user-to-user forums, you are not talking to Apple here.
You can contact iTunes support via this page : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, the try selecting Purchases, Billing & Redemption
Maybe you are looking for
-
ITunes has stopped working in Windows Vista
Hi, I need help! Everytime I try to open iTunes, it always says "iTunes has stopped working", and close program. I have installed iTunes 7.5, 7.6 and 7.7 versions, all of them encounter such issue when I try to open iTunes. My system information list
-
How to set the resolution of images when saving Book as HTML
How can I set a different resolution for images converted to *.GIF when I save a book as HTML? Why doesn't the conversion to JPG work?
-
Time Machine quit backing up files. Bought a new WD external drive. Worked for awhile, then quit backing up. Tried restarting computer, remounting drive. Message: Time Machine Error. Unable to complete backup. An error occurred when copying files. Su
-
I hide my ipod by mistake. How do I unhide it on ITunes?
I hide my ipod application on ITunes. How do I unhide it?
-
Updated itunes, radio listing displays stream name and not station name
since updating itunes my radio listing displays stream name (stream.mp3) and not station name as previous versions did. I am using icecast and i rolled back to a previous version of itunes and it works fine. Cannot resubmit details until i can show s