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

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:

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.

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.