Focus issue - JApplet (Swing)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Focus issue - JApplet (Swing)

 
0
  #1
Aug 11th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Focus issue - JApplet (Swing)

 
1
  #2
Aug 11th, 2008
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.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Focus issue - JApplet (Swing)

 
0
  #3
Aug 11th, 2008
Originally Posted by sciwizeh View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 413
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Focus issue - JApplet (Swing)

 
0
  #4
Aug 11th, 2008
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
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,505
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Focus issue - JApplet (Swing)

 
1
  #5
Aug 11th, 2008
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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Focus issue - JApplet (Swing)

 
0
  #6
Aug 11th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC