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!

cursor problem

Status
Not open for further replies.

petold

Programmer
Sep 18, 2002
9
SE
Hi all. Im creating a chat system and have a question about the cursor.
The Gui is set up as follows: I have a chatArea where all the dialogs shows (JTextArea), I have a message window where you type what you wanna say (JTextField).
Here is the question. When the program starts I would like the cursor to be shown directly in the message window (JTextField). That is, you shouldnt have to mouse-click the message window to get the cursor active in that window.

Do you guys know if this is possible and give me a tip how to solve this?
Thx, Peter
 
You have to create a separate thread to set the initial cursor on a component of the Swing frame, and then instruct Swing to run the thread after the window finishes building...

Create a window adapter:

class SymWindow extends java.awt.event.WindowAdapter
{
public void windowActivated( java.awt.event.WindowEvent event )
{
Object object = event.getSource();
if( object == myFrame.this )
{
myFrame_windowActivated( event );
}
}
}

Assign the adapter to your frame:

SymWindow aSymWindow = new SymWindow();
this.addWindowListener( aSymWindow );

Put this method in your frame:

void myFrame_windowActivated( java.awt.event.WindowEvent event )
{
if( focusObject != myJTextField )
{
focusObject = myJTextField;
Runnable doRequestFocus = new Runnable()
{
public void run()
{
focusObject.requestFocus();
}
};
SwingUtilities.invokeLater( doRequestFocus );
}
}
Component focusObject = null;


There's probably a more consise way to implement this. But it works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top