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
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();
}
}