Results 1 to 15 of 15
Thread: Home key
- 06-27-2010, 12:30 PM #1
- 06-27-2010, 12:39 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-27-2010, 12:44 PM #3Java Code:
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class MyFrame extends JFrame { KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); public MyFrame() { add(new JTextField()); add(new JButton()); manager.addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_TYPED) { System.out.println(e.getSource()); System.out.println(e.getKeyChar()); } return false; } }); } public static void main(String[] args) { MyFrame f = new MyFrame(); f.setBounds(100, 100, 500, 500); f.setVisible(true); } }
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: -1
at javax.swing.text.JTextComponent.setCaretPosition(J TextComponent.java:1650)
at javax.swing.text.DefaultEditorKit$BeginLineAction. actionPerformed(DefaultEditorKit.java:1935)
at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponen t.java:2844)
at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2879)
at javax.swing.JComponent.processKeyEvent(JComponent. java:2807)
at java.awt.Component.processEvent(Component.java:581 5)
at java.awt.Container.processEvent(Container.java:205 8)
at java.awt.Component.dispatchEventImpl(Component.jav a:4410)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:693)
at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:958)
at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:830)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:657)
at java.awt.Component.dispatchEventImpl(Component.jav a:4282)
at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
at java.awt.Window.dispatchEventImpl(Window.java:2429 )
at java.awt.Component.dispatchEvent(Component.java:42 40)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)The Quieter you become the more you are able to hear !
- 06-27-2010, 01:11 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Yes, now what? I ran your code (after setting those components in the north and center positions respectively) and I didn't get those exceptions; what do you have in mind? You at least have to give some information, don't just dump your code here and wait for us to consult our crystal balls and fix your (unknown) problem.
kind regards,
Jos
- 06-27-2010, 01:43 PM #5
While this code is under execution... I pressed the "Home" and "End" key the specified error was generated.:(
I am working on NetBeans.The Quieter you become the more you are able to hear !
- 06-27-2010, 01:49 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
I don't have that behaviour when I run your code, either a key code of 36 (the home key) or 35 (the end key) is returned for both the buton and the text field. Even when I try to press them both at the same time one is always first and after a roll over the other key shows up. No exceptions thrown here. What Java version are you running?
kind regards,
Jos
- 06-27-2010, 02:15 PM #7
I am actually willing to move from one frame to another when the user presses the "page up" or similar keys... while the frame contains multiple components.. irrespective of the focus being on any component.
Java Code:import java.awt.*; import java.awt.event.*; public class test_backspace extends javax.swing.JFrame { KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); public test_backspace() { initComponents(); manager.addKeyEventDispatcher(new KeyEventDispatcher() { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent.KEY_TYPED) { System.out.println((int) e.getKeyChar()); } return false; } }); } private void initComponents() { jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextField1.setText("jTextField1"); jButton1.setText("jButton1"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(65, 65, 65) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(197, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(46, 46, 46) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29) .addComponent(jButton1) .addContainerGap(182, Short.MAX_VALUE)) ); pack(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new test_backspace().setVisible(true); } }); } private javax.swing.JButton jButton1; private javax.swing.JTextField jTextField1; }
I am using jdk1.6.0.The Quieter you become the more you are able to hear !
- 06-27-2010, 02:36 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-27-2010, 02:50 PM #9
Yup it worked.. But how can I distinguish between these keys?????????
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if((int)e.getKeyChar()==36)
System.out.println("mo");
}
return false;
}
from the above code I hoped that the response will be generated when the "Home" key is pressed. But didn't received any response.. please help.The Quieter you become the more you are able to hear !
- 06-27-2010, 02:55 PM #10
i would prefer........ "page up"................... so
if((int)e.getKeyChar()==33)
the focus is on the textfield in my code.. but i want the program to respond irrespective of the focus being set at any component.The Quieter you become the more you are able to hear !
- 06-27-2010, 03:08 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-27-2010, 03:42 PM #12
thanks buddy it worked.
The Quieter you become the more you are able to hear !
- 06-27-2010, 04:00 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 06-27-2010, 08:04 PM #14what if I move the windows/frames to other locations and the PgUp key moves the focus to the window visually below the current window/frame?The Quieter you become the more you are able to hear !
- 06-27-2010, 08:48 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
I associate the PgUp key with moving something upward and the PgDown key with moving something downwards; you want to move the focus from window to window with those keys; what if the window that loses the focus is positioned above the window that receives the focus when I press, say, the PgUp key? It looks a bit unnatural to me ...
kind regards,
Jos
Similar Threads
-
Home work help due midnight
By alucard in forum New To JavaReplies: 2Last Post: 03-29-2010, 05:09 AM -
ow to get Home directory using Common Net ftp api?
By cprash.aggarwal in forum Advanced JavaReplies: 1Last Post: 03-01-2009, 06:57 PM -
How to get Home directory using Common Net ftp api?
By cprash.aggarwal in forum NetworkingReplies: 1Last Post: 03-01-2009, 06:37 PM -
Make money from home, Home Typing Data Entry Partnerships
By arturmoniswork in forum Reviews / AdvertisingReplies: 0Last Post: 12-30-2008, 06:55 AM -
What do I need to write programs at home (help)
By Zebra in forum New To JavaReplies: 22Last Post: 05-29-2008, 04:50 AM
Bookmarks