Results 1 to 6 of 6
Thread: Question on focusing
- 02-13-2012, 09:28 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
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:
I think I'm doing the setVisable at line 46 wrong. Would appreciate any advice.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"); } } }
ThanksLast edited by elliotHenry; 02-13-2012 at 09:28 PM. Reason: Typo
- 02-13-2012, 09:33 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-13-2012, 10:07 PM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-13-2012, 10:33 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
- 02-13-2012, 10:35 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 34
- Rep Power
- 0
- 02-14-2012, 10:30 PM #6
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
focusing on object when clicking an image?
By handy web in forum New To JavaReplies: 1Last Post: 02-09-2012, 10:05 PM -
question posted by indissa: library question.
By Fubarable in forum New To JavaReplies: 2Last Post: 11-18-2011, 01:14 AM -
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Focusing the JTextField and so on while pressing Tab
By britto_bicsjohn in forum AWT / SwingReplies: 1Last Post: 08-28-2009, 08:24 AM -
Focusing on the next JTextField
By britto_bicsjohn in forum AWT / SwingReplies: 2Last Post: 08-26-2009, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks