943,735 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1906
  • Java RSS
Aug 11th, 2008
0

Focus issue - JApplet (Swing)

Expand Post »
I'm having a Focus issue when running a Swing application.

There are 4 buttons visible on the JApplet.

I have an implementation that allows the user to make keyboard and mouse events in the JApplet.

The problem is that I can use the keyboard and mouse events when the program first starts, but the moment I click a button the focus is lost and the JApplet can no longer receive keyboard events.

What I'd like is a solution on how to reobtain the focus or somehow create a managed thread that will constantly re-focus the JApplet (or the contentpane) such that it will receive keyboard events from the user.

Thanks,

-Alex.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 11th, 2008
1

Re: Focus issue - JApplet (Swing)

i don't know whether this will work, but try setting using .setFocusable(false) on the buttons. i don't think it will work, but it's worth a try

EDIT: it may ruin the way that you use the keyboard, but without seeing how you implement i cannot say
Last edited by sciwizeh; Aug 11th, 2008 at 10:35 am.
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 11th, 2008
0

Re: Focus issue - JApplet (Swing)

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
i don't know whether this will work, but try setting using .setFocusable(false) on the buttons. i don't think it will work, but it's worth a try

EDIT: it may ruin the way that you use the keyboard, but without seeing how you implement i cannot say
That worked!

Now, is there some kind of way to make the JApplet part of a focus-traversal between itself and the JButtons and additionally make it such that clicking a JButton will cause the JApplet to be focused after an action-performed?

That would be the best of both worlds, but it's not completely necessary. Thank you Sciwizeh, you genius! XD
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 11th, 2008
0

Re: Focus issue - JApplet (Swing)

i don't really know, i assume you have read the sun tutorial on focus, but i don't see how you include an applet, being the root, i would assume it's possible, but i cannot find anything else
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Aug 11th, 2008
1

Re: Focus issue - JApplet (Swing)

If you can use them, key bindings are easier to handle with regards to focus:
http://java.sun.com/docs/books/tutor...eybinding.html
but if you can't use those then you could call requestFocusInWindow() on the JApplet (or whatever other container you need to have focus) at the end of your button listener code
java Syntax (Toggle Plain Text)
  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.KeyAdapter;
  5. import java.awt.event.KeyEvent;
  6. import javax.swing.JApplet;
  7. import javax.swing.JButton;
  8. import javax.swing.JOptionPane;
  9.  
  10.  
  11. public class FocusTest extends JApplet {
  12.  
  13. JButton button;
  14.  
  15. public void init() {
  16. button = new JButton("Ok");
  17. button.addActionListener(new ActionListener() {
  18. public void actionPerformed(ActionEvent e) {
  19. requestFocusInWindow();
  20. }
  21. });
  22. setLayout(new BorderLayout());
  23. add(button,BorderLayout.SOUTH);
  24.  
  25. setFocusable(true);
  26. addKeyListener(new KeyAdapter() {
  27. public void keyPressed(KeyEvent e) {
  28. JOptionPane.showMessageDialog(FocusTest.this, "Key pressed");
  29. }
  30.  
  31. });
  32. }
  33. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 11th, 2008
0

Re: Focus issue - JApplet (Swing)

You both rock!

I didn't think I could directly treat a JApplet as a window - for some time I was trying to tie together a WindowListener into a JApplet but considering a JApplets methodology I guess that's impossible?

Even so, this method worked! I'll be making a new topic describing a different issue pertaining to JApplets and Windows--

-Alex
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java bluetooth implementation
Next Thread in Java Forum Timeline: Accessing JApplet "Window Close" [x] box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC