Results 1 to 6 of 6
  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Question on focusing

    Hi folks

    Ok, yet another issue for me I'm hoping you can help with. My GUI has keyListeners added to the buttons. When I start up the app, and start to type nothing happens until AFTER I've pressed one of the buttons (i.e. the buttons are now in focus). How to I allow for either a key press event or button pressed event to work on startup? Code below:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import javax.swing.*;
    public class MyGui extends MasterGUI
    {
       public MyGui()
        {
              
            KeyAdapter adt;
            for (int i = 0; i < KEYS.length; i++)
            {
                mem.put(KEYS[i], "");
            }
            mem.put(currentKey, "");
            int k = 0;
            for (int j = 0; j < 4; j++)
            {
                for (int i = 0; i < 5; i++)
                {
                    JButton b = (JButton) buts[k];
                    b.addActionListener(new ButtonSeeker(k));
                    k++;
                }
            }
            adt = new KeyWatcher();
            setFocusable(true);
            MenuSeeker menWat = new MenuSeeker();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            exit.addActionListener(menWat);
            for (int i=0; i<menu.length; i++)
            {
                if(menu[i] != null)
              {
                  menu[i].addActionListener(menWat);
              }
            }
          for (int i = 0; i<buts.length; i++)
          {
             buts[i].addKeyListener(adt);
          }     
            for (int i = 0; i<buts.length; i++)
         {
             buts[i].setVisible(true);
             buts[i].addKeyListener(adt);     
         }
        }
      private class KeyWatcher extends KeyAdapter
        {
            @Override
            public void keyPressed(KeyEvent k)
            {
                Character ch = k.getKeyChar();
                char aCha = ch.toUpperCase(cha);
                System.out.println(aCha);           
            }
        }
        private class MenuSeeker implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
               // TODO complete the method
                if (e.getSource() == exit)
                {
                    System.exit(0);
                }
                for (int i = 0; i < menu.length; i++)
                {
                   if(e.getSource() == menu[i] )
                    {
                        System.out.println(e.getSource().toString());
                    }
                }           
            }
        }
        private class ButtonSeeker implements ActionListener
        {
            private int command = 0;
            private ButtonSeeker(int k)
            {
                command = k;
            }
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println(e.getID() + "here");
            }
        }
    }
    I think I'm doing the setVisable at line 46 wrong. Would appreciate any advice.

    Thanks
    Last edited by elliotHenry; 02-13-2012 at 09:28 PM. Reason: Typo

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    11,380
    Blog Entries
    7
    Rep Power
    17

    Default Re: Question on focusing

    A Button class extends a JComponent class (indirectly) so it inherits methods from the JComponent class; a JComponent has a few methods to acquire the keyboard focus.

    kind regards,

    Jos
    When people rob a bank they get a penalty; when banks rob people they get a bonus.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Question on focusing

    Swing was designed to work with Key Bindings. Drop the KeyListener(s) and learn how to do it the Swing way: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Moving this thread to AWT/Swing.

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

  4. #4
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Question on focusing

    Quote Originally Posted by JosAH View Post
    A Button class extends a JComponent class (indirectly) so it inherits methods from the JComponent class; a JComponent has a few methods to acquire the keyboard focus.

    kind regards,

    Jos
    Thanks Josh

    The API seems to want me to add the requestFocusInWindow() to a particular button but not sure where I should apply this. Can you provide a hint?

  5. #5
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Question on focusing

    Quote Originally Posted by DarrylBurke View Post
    Swing was designed to work with Key Bindings. Drop the KeyListener(s) and learn how to do it the Swing way: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

    Moving this thread to AWT/Swing.

    db
    Hi DarrylBurke

    If this wasn't a homework question I'd be all over it. But as it stands I have to follow the criteria set out in the quesiton.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,918
    Rep Power
    16

    Default Re: Question on focusing

    Don't double post. Your other thread has been moved to AWT/Swing and closed.
    Please go through http://www.java-forums.org/content/113-forum-rules.html

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. focusing on object when clicking an image?
    By handy web in forum New To Java
    Replies: 1
    Last Post: 02-09-2012, 10:05 PM
  2. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  3. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  4. Focusing the JTextField and so on while pressing Tab
    By britto_bicsjohn in forum AWT / Swing
    Replies: 1
    Last Post: 08-28-2009, 08:24 AM
  5. Focusing on the next JTextField
    By britto_bicsjohn in forum AWT / Swing
    Replies: 2
    Last Post: 08-26-2009, 03:53 PM

Posting Permissions

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