| | |
Why isn't my modified KeyTyped method working properly?
![]() |
•
•
Join Date: Nov 2008
Posts: 25
Reputation:
Solved Threads: 0
Hi,
Everything else in this code works properly but the keyTyped method is not working properly. I have been working on this for sometime. Any help would be appreciated. Also, any advice on getting my southPanel to appear when I start the application? It appears but only after I expand the Window manually. I'd like the combo box to show itself right away as opposed to relying on the user to expand it. Thanks.
-- Curtis
Everything else in this code works properly but the keyTyped method is not working properly. I have been working on this for sometime. Any help would be appreciated. Also, any advice on getting my southPanel to appear when I start the application? It appears but only after I expand the Window manually. I'd like the combo box to show itself right away as opposed to relying on the user to expand it. Thanks.
-- Curtis
java Syntax (Toggle Plain Text)
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Exercise_12_18 extends JFrame { private JLabel choicesLabel; private JComboBox choicesComboBox; private String actions[] = {"Mouse Click", "Mouse Move", "Key Pressed", "Mouse Entered", "Mouse Exited"}; private JPanel southPanel; private JPanel southCenterPanel; private boolean act[] = {true, false, false, false, false}; Exercise_12_18() { choicesComboBox = new JComboBox(actions); choicesComboBox.setMaximumRowCount(3); choicesLabel = new JLabel("Actions"); southCenterPanel = new JPanel(); southCenterPanel.setLayout(new GridLayout(1, 2)); southCenterPanel.add(choicesLabel); southCenterPanel.add(choicesComboBox); southPanel = new JPanel(); southPanel.setLayout(new BorderLayout()); southPanel.add(southCenterPanel, BorderLayout.CENTER); choicesComboBox.addItemListener( new ItemListener() { public void itemStateChanged(ItemEvent e) { resetPrintIt(); act[choicesComboBox.getSelectedIndex()] = true; System.out.println(actions[choicesComboBox.getSelectedIndex()]+ ": " + act[choicesComboBox.getSelectedIndex()]); } } ); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } } ); setSize(600, 600); setVisible(true); addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (act[0]) System.out.println("Mouse Clicked: X: " + e.getX() + " Y: " + e.getY()); } public void mouseEntered(MouseEvent e) { if(act[3]) System.out.println("Mouse Entered"); } public void mouseExited(MouseEvent e) { if (act[4]) System.out.println("Mouse Exited"); } } ); addMouseMotionListener( new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { if (act[1]) System.out.println("Mouse Moved: X: " + e.getX() + " Y: " + e.getY()); } } ); addKeyListener( new KeyAdapter() { public void keyTyped(KeyEvent e) { if (act[2]) System.out.println("Key Pressed: " + e.getKeyChar()); } } ); Container c = getContentPane(); c.add(southPanel, BorderLayout.SOUTH); } private void resetPrintIt() { for (int i = 0; i < act.length; i++) act[i] = false; } public void paint(Graphics g) { super.paint(g); } public static void main(String args[]) { new Exercise_12_18(); } }
Hello,
I think I may have found a reason, but I also have a few questions, the first of which is why are you using a window listener, the same action could be done with:
and two, why not just implement all the listeners in your class rather than using all those anonymous classes, not that there is anything wrong with that, it's just slightly harder to follow, in my opinion.
as for the the problem of not showing the pane, try calling setVisible(true); after you add it.
as for why the key listener isn't working, try setting setFocusable(true); on your JFrame, and use false for the JLabel and JComboBox. then call requestFocus(); on your JFrame.
I think I may have found a reason, but I also have a few questions, the first of which is why are you using a window listener, the same action could be done with:
java Syntax (Toggle Plain Text)
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
as for the the problem of not showing the pane, try calling setVisible(true); after you add it.
as for why the key listener isn't working, try setting setFocusable(true); on your JFrame, and use false for the JLabel and JComboBox. then call requestFocus(); on your JFrame.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
I'm not sure, usually the component in question has to have focus. The only way I have been able to get a key listener to work is to derive from JComponent rather than JFrame then add the derived components.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
I have a new suggestion for you.
Make a new JPanel, or JComponent instance in your constructor, then rather than adding your listeners to
if you do all that and it still doesn't work, post your new problems with revised code.
Make a new JPanel, or JComponent instance in your constructor, then rather than adding your listeners to
this add them to the JPanel/JComponent and add the JComponent or JLabel to the JFrame (this) with BorderLayout.CENTER . make sure to set the JLabel or JComponent to be focusable and request focus, also try making everything else in the program non focusable.if you do all that and it still doesn't work, post your new problems with revised code.
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
![]() |
Other Threads in the Java Forum
- Previous Thread: Animated GIF with Graphics, or Graphics2D?
- Next Thread: Execute SQL scripts with Java
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows





