Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

want to check whether JDialog is in background or not

Status
Not open for further replies.

frag

Programmer
Dec 7, 2000
321
GB
Hi!

Is there a away to figure out whether a non-modular JDialog is in the background (behind the JFram) or not?

The idea behind is that I want to have a non-modluar JDialog poping up at a certain state of the program but I want to make sure that user has to read and click something on the dialog even if the user wants to close the main window (JFrame) but it should be possible to work in the main window while the popup is open.

All I have found in the API until now is the isVisible() methode but that thing just checks whether the dialog is visible at all not caring if the dialog is behind a window or not.

cheers

frag

patrick.metz@epost.de
 
if you use the isFocused() that JDialog inherits from Window you'll be able to see whether it's the active window (which'll mean it's in front). If you need to be able to tell whether it's in view of the user use the getX and getY calls and check to see if it's directly behind the JFrame.

Here's a quick example I threw together - should be easy to rip out what you need!
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class FocusTest extends JFrame{

	private MyDialog dialog;	
	private JButton test;

	public FocusTest(){
		setTitle("frame");		
		test = new JButton("test");
		test.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					System.out.println("frame is "+
					(isFocused() ? "active" : "inactive")+
					" at "+getX()+","+getY());
					System.out.println("dialog is"+
					(dialog.isFocused() ? "active" : "inactive")+
					" at "+dialog.getX()+","+dialog.getY());
				}
			}
		);
		getContentPane().add(test);
		getContentPane().setLayout(new FlowLayout());
		setSize(200,200);
		show();
		addWindowListener(
			new WindowAdapter(){
				public void windowClosing(WindowEvent e){
					dispose();
					System.exit(0);
				}
			}
		);
		dialog = new MyDialog(this);
	}

	public static void main(String[] args){new FocusTest();}

}

class MyDialog extends JDialog{

	private JButton test;
	private Window parent;

	public MyDialog(Window parent){
		setTitle("dialog");
		this.parent = parent;
		test = new JButton("test");
		test.addActionListener(
			new ActionListener(){
				public void actionPerformed(ActionEvent e){
					System.out.println("dialog is "+
					(isFocused() ? "active" : "inactive")+
					" at "+getX()+","+getY());
					System.out.println("frame is "+
					(isParentFocused() ? "active" : "inactive")+
					" at "+getParentX()+","+getParentY());
				}
			}
		);
		getContentPane().add(test);
		getContentPane().setLayout(new FlowLayout());
		setSize(150,150);
		show();		
	}

	public boolean isParentFocused(){return parent.isFocused();}

	public int getParentX(){return parent.getX();}

	public int getParentY(){return parent.getY();}

}
 
isFocused()... wonderful :)

thank you!

patrick.metz@epost.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top