| | |
Focus issue - JApplet (Swing)
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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.
i don't know whether this will work, but try setting using
EDIT: it may ruin the way that you use the keyboard, but without seeing how you implement i cannot say
.setFocusable(false) on the buttons. i don't think it will work, but it's worth a tryEDIT: 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
"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
•
•
•
•
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
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
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
"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
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
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)
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JOptionPane; public class FocusTest extends JApplet { JButton button; public void init() { button = new JButton("Ok"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { requestFocusInWindow(); } }); setLayout(new BorderLayout()); add(button,BorderLayout.SOUTH); setFocusable(true); addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { JOptionPane.showMessageDialog(FocusTest.this, "Key pressed"); } }); } }
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
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
![]() |
Other Threads in the Java Forum
- Previous Thread: Java bluetooth implementation
- Next Thread: Accessing JApplet "Window Close" [x] box
| Thread Tools | Search this Thread |
Tag cloud for Java
affinetransform android api append apple applet application arguments array arrays automation bi binary bluetooth businessintelligence busy_handler(null) chat class classes client code component database draw eclipse encryption equation error event exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer intersect j2me java javaexcel javaprojects jmf jni jpanel julia linked linux list loop main map method methods mobile netbeans newbie number open-source oracle oriented panel print problem program programming project qt recursion reference replaysolutions repositories return robot scanner screen scrollbar se server set singleton size sms socket sort sql string swing test threads time tree utility windows xor






