Why isn't my modified KeyTyped method working properly?

Reply

Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Why isn't my modified KeyTyped method working properly?

 
0
  #1
Feb 21st, 2009
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

  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5. public class Exercise_12_18 extends JFrame
  6. {
  7. private JLabel choicesLabel;
  8. private JComboBox choicesComboBox;
  9. private String actions[] = {"Mouse Click", "Mouse Move", "Key Pressed", "Mouse Entered", "Mouse Exited"};
  10. private JPanel southPanel;
  11. private JPanel southCenterPanel;
  12. private boolean act[] = {true, false, false, false, false};
  13.  
  14. Exercise_12_18()
  15. {
  16. choicesComboBox = new JComboBox(actions);
  17. choicesComboBox.setMaximumRowCount(3);
  18. choicesLabel = new JLabel("Actions");
  19. southCenterPanel = new JPanel();
  20. southCenterPanel.setLayout(new GridLayout(1, 2));
  21. southCenterPanel.add(choicesLabel);
  22. southCenterPanel.add(choicesComboBox);
  23. southPanel = new JPanel();
  24. southPanel.setLayout(new BorderLayout());
  25. southPanel.add(southCenterPanel, BorderLayout.CENTER);
  26.  
  27.  
  28. choicesComboBox.addItemListener(
  29. new ItemListener()
  30. {
  31. public void itemStateChanged(ItemEvent e)
  32. {
  33. resetPrintIt();
  34. act[choicesComboBox.getSelectedIndex()] = true;
  35. System.out.println(actions[choicesComboBox.getSelectedIndex()]+ ": " + act[choicesComboBox.getSelectedIndex()]);
  36. }
  37. }
  38. );
  39.  
  40. addWindowListener(
  41. new WindowAdapter()
  42. {
  43. public void windowClosing(WindowEvent e)
  44. {
  45. dispose();
  46. System.exit(0);
  47. }
  48. }
  49. );
  50.  
  51. setSize(600, 600);
  52. setVisible(true);
  53.  
  54. addMouseListener(
  55. new MouseAdapter()
  56. {
  57. public void mouseClicked(MouseEvent e)
  58. {
  59. if (act[0])
  60. System.out.println("Mouse Clicked: X: " + e.getX() + " Y: " + e.getY());
  61. }
  62.  
  63. public void mouseEntered(MouseEvent e)
  64. {
  65. if(act[3])
  66. System.out.println("Mouse Entered");
  67. }
  68.  
  69. public void mouseExited(MouseEvent e)
  70. {
  71. if (act[4])
  72. System.out.println("Mouse Exited");
  73. }
  74.  
  75. }
  76. );
  77.  
  78. addMouseMotionListener(
  79. new MouseMotionAdapter()
  80. {
  81. public void mouseMoved(MouseEvent e)
  82. {
  83. if (act[1])
  84. System.out.println("Mouse Moved: X: " + e.getX() + " Y: " + e.getY());
  85. }
  86. }
  87. );
  88.  
  89. addKeyListener(
  90. new KeyAdapter()
  91. {
  92. public void keyTyped(KeyEvent e)
  93. {
  94.  
  95. if (act[2])
  96. System.out.println("Key Pressed: " + e.getKeyChar());
  97.  
  98. }
  99. }
  100. );
  101. Container c = getContentPane();
  102. c.add(southPanel, BorderLayout.SOUTH);
  103.  
  104. }
  105.  
  106. private void resetPrintIt()
  107. {
  108. for (int i = 0; i < act.length; i++)
  109. act[i] = false;
  110. }
  111.  
  112. public void paint(Graphics g)
  113. {
  114. super.paint(g);
  115. }
  116.  
  117. public static void main(String args[])
  118. {
  119. new Exercise_12_18();
  120. }
  121.  
  122. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 403
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 20
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Why isn't my modified KeyTyped method working properly?

 
0
  #2
Feb 21st, 2009
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:
  1. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 25
Reputation: curtissumpter is an unknown quantity at this point 
Solved Threads: 0
curtissumpter curtissumpter is offline Offline
Light Poster

Re: Why isn't my modified KeyTyped method working properly?

 
0
  #3
Feb 21st, 2009
Thanks for the setVisible tip. It works. It's appreciated. I'm currently researching the setFocusable() that you suggested. But why doesn't the solution I proposed work? It seems logical.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 403
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 20
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Why isn't my modified KeyTyped method working properly?

 
0
  #4
Feb 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 403
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 20
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

Re: Why isn't my modified KeyTyped method working properly?

 
0
  #5
Feb 22nd, 2009
I have a new suggestion for you.

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC