PDA

View Full Version : Switching windows/Activating a client



Town
11-24-2007, 02:28 AM
I'm working on my first java application and I think I've got it all down except this.

So, what would you recommend using to switch between windows or activate a client?

Yakman
11-24-2007, 01:31 PM
you use a method called requestFocus in the java.awt.Component class

a bit like this..

//JFrame is a subclass of Component, so it has the request focus method
JFrame frame = new JFrame("my frame title");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setVisible(true);

frame.requestFocus();


btw, requestFocus doesnt guarantee you will get focus, it just sends a request to the operating system, loads of components could request focus, and only one might get it.
you can check if you have focus with the hasFocus method
or look for java.awt.event.FocusListener if you want the operating system to notify you when your component gets focus

Town
11-24-2007, 04:12 PM
Alright, thanks alot for that.