Results 1 to 15 of 15

Thread: Home key

  1. #1
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default Home key

    I want the frame to respond when the home key is pressed.. Whenever I try pressing "Home Key" while the program is under execution some error is generated.

    please suggest. thnx:)
    The Quieter you become the more you are able to hear !

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    I want the frame to respond when the home key is pressed.. Whenever I try pressing "Home Key" while the program is under execution some error is generated.

    please suggest. thnx:)
    Great, all the information you're giving us is "some error"; how are we supposed to know what you have in mind and what your program actually does?

    kind regards,

    Jos

  3. #3
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    Java 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 !

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  5. #5
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    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 !

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    While this code is under execution... I pressed the "Home" and "End" key the specified error was generated.:(
    I am working on NetBeans.
    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

  7. #7
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    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;
    }
    In the above code when I press keys such as "HOme", "End", "Page Up", "Page Down" I don't get any response.

    I am using jdk1.6.0.
    The Quieter you become the more you are able to hear !

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    In the above code when I press keys such as "HOme", "End", "Page Up", "Page Down" I don't get any response.
    I don't think the home, end, pgup etc. keys send "KEY_TYPED" events because they don't have an ASII code equivalent. Check the KEY_PRESSED, KEY_RELEASED parts instead.

    kind regards,

    Jos

  9. #9
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    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 !

  10. #10
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    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 !

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    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.
    Don't check for the getKeyChar(), check for the getKeyCode().

    kind regards,

    Jos

  12. #12
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    thanks buddy it worked.
    The Quieter you become the more you are able to hear !

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    thanks buddy it worked.
    I still find it a strange way to navigate between windows/frames, what 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? I would be a bit surprised ...

    kind regards,

    Jos

  14. #14
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    what 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?
    I didn't get it !! Please elaborate.
    The Quieter you become the more you are able to hear !

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Stephen Douglas View Post
    I didn't get it !! Please elaborate.
    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

  1. Home work help due midnight
    By alucard in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 04:09 AM
  2. ow to get Home directory using Common Net ftp api?
    By cprash.aggarwal in forum Advanced Java
    Replies: 1
    Last Post: 03-01-2009, 05:57 PM
  3. How to get Home directory using Common Net ftp api?
    By cprash.aggarwal in forum Networking
    Replies: 1
    Last Post: 03-01-2009, 05:37 PM
  4. Make money from home, Home Typing Data Entry Partnerships
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 05:55 AM
  5. What do I need to write programs at home (help)
    By Zebra in forum New To Java
    Replies: 22
    Last Post: 05-29-2008, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •