my dog learned polymorphism*
The moose likes Applets and the fly likes requestFocus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "requestFocus" Watch "requestFocus" New topic
Author

requestFocus

Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Hello,
I'm working with a JApplet and swing if it makes any difference. I have a JTextPane with 4 JButtons across the top that have text written on them to be a custom toolbar. In my init() method I use the following code:
jpane.requestFocus();
jpane.setCaretPosition(0);
which works fine. The JtextPane requests the focus and sets the caret (cursor) to the 0th position. The problem is when I press one of my JButtons. For Example, one of them is "CUT". When I press cut (the cut action works just fine), I want to return focus to the JTextPane and remove it from the JButton. Nothing I seem to do is able to do this. In the actionPerformed() function I tried using
jpane.requestFocus();
jpane.setCaretPosition(0);
to no avail. I've also tried to register a FocusListener with the JButtons and then in the GainedFocus() or lostFocus() methods I again used my requestFocus() and setCaretPosition() methods and they do not work. Looking at my applet it's plain to see that the JButton still has focus. Any ideas here. This is just driving me nuts.
Joe Crew
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Joe,
Not sure why it wouldn't work. I am running JDK1.2.2 with the appletviewer and the following seems to work.

Regards,
Manfred.
Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Manfred,
Thanks for your response and saving my insanity. I knew the code I was using should of worked, just like yours did. I kept stripping my code out until I got it to look just like what you posted and mine still would not work. Then I compared them line for line, word for word, and I found the problem. Here it is for future refernce.
You created a couple vars,
JTextPane textPane;
JButton button1, button2, button3, button4;
and in your init(), you make the statement
textPane = new JTextPane();
cont.add( textPane, "Center" );
This is where we differed. I made the same var declaration as you, but in my init() I accidentally put
JTextPane textPane = new JTextPane();
cont.add( textPane, "Center" );
Notice the JTextPane in front of textPane in mine. This caused every bit of the problem. Do the same to your code and see what happens by adding the JTextPane in front of textPane. Obviously I'm not supposed to do that I just never noticed it. I using jdk1.1.8 and swing 1.1.1 to complete my project. And yes I am forced to use jdk1.1.8 and swing 1.1.1 so I can't change if I want to. I would think, however, that the javac would complain about trying to redeclare a variable or something. If you can, Manfred, try this with 1.2.2 because I would like to know if the jdk1.2.2 you are using triggers any errors on this.
And thanks for taking the time posting a code snippet, I would of never been able to figure this out without a line-by-line compare because my eyes simply kept missing my mistake.
joe crew
Joe Crew
Ranch Hand

Joined: Apr 02, 2001
Posts: 49
Manfred,
Never mind, please don't waste your time recompiling your code. After looking at it in my short-circuited mind, I know what the problem is and I know why it does not give me an error and would not give you an error under 1.2.2. I was creating a local variable named jpane and setting my jtextpane up with it. And then in my actionListener, it simply wasn't seeing the local copy of jpane I set up locally. So thanks for helping me see my scope mistake. I knew my code should of worked all along and you helped me see the problem. Simple mistake that was crushing me.
Thanks again,
Joe Crew
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: requestFocus
 
Similar Threads
JButton in Applet triggers HTML
Visual Age for Java: requestFocus() doesn't work
JTextArea Scroll Focus
Giving focus to JPanel when Down arrow is pressed from JButton
Get Focus on JEditorPane