Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Focus problems

801176
Member Posts: 5
Hy everyone!
I have some focusing problems. (I hope it's focusing...)
I have 3 java files:
If I try hasFocus test after added the Canvas, I get false.
Someone can helm me with this?
I have some focusing problems. (I hope it's focusing...)
I have 3 java files:
package testproject; import java.applet.Applet; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; /** * * @author Laxika */ public class Main extends Applet implements MouseMotionListener, KeyListener { //Added these variables just to test the DoubleBuffering. public static int posX = 0; public static int posY = 0; @Override public void init() { setSize(300, 300); this.add(new DoubleBufferTest()); addMouseMotionListener(this); addKeyListener(this); } public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { posX = e.getX(); posY = e.getY(); this.getComponent(0).repaint(); } public void keyTyped(KeyEvent e) { System.out.println("Typed"); } public void keyPressed(KeyEvent e) { System.out.println("Pressed"); } public void keyReleased(KeyEvent e) { System.out.println("Released"); } }
package testproject; import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; /** * * @author Rendszergazda */ public class DoubleBufferTest extends DoubleBufferedCanvas { public DoubleBufferTest() { super.setBounds(new Rectangle(300, 300)); } @Override public void paint(Graphics g) { g.clearRect(0, 0, 300, 300); try { g.setColor(Color.red); g.fillRect(Main.posX, Main.posY, 40, 40); } finally { //Best to dispose graphics object after finish. g.dispose(); } } }
package testproject; import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Toolkit; import java.awt.image.BufferStrategy; /** * * @author Laxika * @version 1.0 */ public abstract class DoubleBufferedCanvas extends Canvas { private int width; private int height; /** * Set the width and the height of the canvas. * * @param width The width of the canvas. * @param height The height of the canvas. */ @Override public void setBounds(Rectangle r) { this.width = (int) r.getWidth(); this.height = (int) r.getHeight(); } @Override public void addNotify() { super.addNotify(); this.createBufferStrategy(2); //The main Applet will handle every event. this.setEnabled(false); } @Override public void repaint() { BufferStrategy bf = this.getBufferStrategy(); paint(bf.getDrawGraphics()); bf.show(); Toolkit.getDefaultToolkit().sync(); } @Override public abstract void paint(Graphics g); @Override public Dimension getMinimumSize() { return new Dimension(width, height); } @Override public Dimension getPreferredSize() { return new Dimension(width, height); } @Override public Dimension getMaximumSize() { return new Dimension(width, height); } }My problem is that I can't get the Key events on main.java after I add the DoubleBufferTest class to the applet. ( this.add(new DoubleBufferTest()); )
If I try hasFocus test after added the Canvas, I get false.
Someone can helm me with this?
Tagged:
Answers
-
Moderator action: Moved from Java Programming.
db -
You add the <tt>KeyListener</tt> to the <tt>Applet</tt>, so why do you expect to get <tt>KeyEvent</tt>s in the <tt>Canvas</tt>?
Do you have a reason for using an <tt>Applet</tt> (AWT) and not a <tt>JApplet</tt> (Swing)? With a Swing <tt>JApplet</tt> and a <tt>JPanel</tt>, you get double buffering for free and you can use Key Bindings.
db -
Uhh thanks!
I want they Key event s in the Applet. But I don't get them.:S
EDIT:
I found the solution, if someone have the same problem:KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() { public boolean dispatchKeyEvent(KeyEvent e) { // This example converts all typed keys to upper case if (e.getID() == KeyEvent.KEY_TYPED) { e.setKeyChar(Character.toUpperCase(e.getKeyChar())); } // If the key should not be dispatched to the // focused component, set discardEvent to true System.out.println(e.getKeyChar()); boolean discardEvent = false; return discardEvent; } });
Edited by: Teh Heavenly on 2010.10.05. 12:43 -
Took me 3 hours googling, but finally...
This discussion has been closed.