Skip to main content

processKeyEvent and processFocusEvent not called

2 replies [Last post]
Joined: 2007-09-13


I'm new to JXLayer, and it's all working fine, except for processKeyEvent and processFocusEvent which are never called ...
See this example :

<br />
import java.awt.Color;<br />
import java.awt.Dimension;<br />
import java.awt.event.FocusEvent;<br />
import java.awt.event.KeyEvent;<br />
import java.awt.event.MouseEvent;</p>
<p>import javax.swing.JFrame;<br />
import javax.swing.JLabel;<br />
import javax.swing.JPanel;</p>
<p>import org.jdesktop.jxlayer.JXLayer;<br />
import org.jdesktop.jxlayer.plaf.AbstractLayerUI;</p>
<p>public class Test extends JFrame {</p>
<p>	public Test() {<br />
		JPanel layeredPanel = new JPanel();<br />
		layeredPanel.setPreferredSize(new Dimension(50, 50));<br />
		layeredPanel.setBackground(Color.BLUE);<br />
		JXLayer layer = new JXLayer(layeredPanel);<br />
		layer.setUI(new AbstractLayerUI() {<br />
			protected void processKeyEvent(KeyEvent e, JXLayer l) {<br />
				super.processKeyEvent(e, l);<br />
				System.out.println("keybord : " + e.getKeyCode());<br />
			}<br />
			@Override<br />
			protected void processMouseEvent(MouseEvent e, JXLayer l) {<br />
				super.processMouseEvent(e, l);<br />
				System.out.println("mouse : " + e.getButton());<br />
			}<br />
			@Override<br />
			protected void processFocusEvent(FocusEvent e, JXLayer l) {<br />
				super.processFocusEvent(e, l);<br />
				System.out.println("focus");<br />
			}<br />
<p>		JPanel content = new JPanel();<br />
		content.add(layer);<br />
		content.add(new JLabel("a test"));<br />
		this.add(content);<br />
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);<br />
		this.setSize(new Dimension(200, 200));<br />
		this.setVisible(true);<br />
<p>	public static void main(String[] args) {<br />
		new Test();<br />
<p>}<br />

(the processMouseEvent method does work well !)
May be something is stealing the focus from my panel ?? or I have done something really stupid in the code above :)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2005-09-05

Hello Lebesnec

If you put to the layer something like JTextField, you'll see that it will get key and focus events,
when JPanel is not supposed to get focus by default, that's why you don't see these events,
the fix is simple and it has nothing to do with JXLayer

after that your panel will get the focus and start receiving key events


Joined: 2007-09-13

It work !