I'm trying to make a program that will toggle a boolean value and paint a red rectangle on the right side of a swing panel when you press the 'a' key. Unfortunately, I'm not having much luck, because when I hit 'a' nothing happens. My mistake is probably something obvious that I'll know how to fix once someone points it out for me, but for the life of me, I can't find it. If someone could look over my code and figure out what's wrong, I'd very much appreciate it. I've attached my source code below.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ClickyLatinHelp extends JFrame implements ActionListener, KeyListener {
	final int xpad = 10;
	final int ypad = 15;
	final int ydown = 20;
	final int xeng = 380;
	int x = 0;
	int y = 0;
	int listnumber = 0;
	int arraylistindex = 0;
	int arrayelement = 0;
	boolean showeng = true;
	char key = 'a';
	String[] greeting = {"Why, Hello there!","Welcome.","blah blah blah","more useless crap"};
	JButton List1Button = new JButton("List 1");
	JButton List2Button = new JButton("List 2");
	JButton List3Button = new JButton("List 3");
	JButton List4Button = new JButton("List 4");
	JButton List5Button = new JButton("List 5");
	JButton List6Button = new JButton("List 6");
	JButton List7Button = new JButton("List 7");
	JButton List8Button = new JButton("List 8");
	JButton List9Button = new JButton("List 9");
	JButton List10Button = new JButton("List 10");

	public ClickyLatinHelp() {
		super("Welcome to Latin Help");                 
		setBounds(50, 20, 750, 700);                  
			DrawingArea canvas = new DrawingArea();
	JPanel ButtonBanner = new JPanel();

    setLayout(new BorderLayout());		
	public class DrawingArea extends JPanel {
		public void paintComponent(Graphics comp) {
    	Graphics2D comp2D = (Graphics2D) comp;
    	comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    	Font font = new Font("Arial", Font.BOLD, 12);
    	Color background = new Color(240, 0, 0);
    	Lists listhi = new Lists();
    	x = xpad;
    	y = ydown;
    	arrayelement = 0;
    	while (listhi.ListArrays[listnumber][arrayelement] != null) {
    		comp2D.drawString(listhi.ListArrays[listnumber][arrayelement], x, y);
    		y = y + ypad;
    	x = xeng;
    	y = ydown;
    	arrayelement = 0;
    	if (showeng == true){
    		while (listhi.EnglishArrays[listnumber][arrayelement] != null) {
    			comp2D.drawString(listhi.EnglishArrays[listnumber][arrayelement], x, y);
    			y = y + ypad;
    	else if (showeng == false){comp2D.setColor(background);
    	comp2D.fillRect(375, 3, 370, 650);

	public void actionPerformed(ActionEvent evt) {
		Object source = evt.getSource();
		if (source == List1Button){ listnumber = 1;}
		else if (source == List2Button){ listnumber = 2;}
		else if (source == List3Button){ listnumber = 3;}
		else if (source == List4Button){ listnumber = 4;}
		else if (source == List5Button){ listnumber = 5;}
		else if (source == List6Button){ listnumber = 6;}
		else if (source == List7Button){ listnumber = 7;}
		else if (source == List8Button){ listnumber = 8;}
		else if (source == List9Button){ listnumber = 9;}
		else if (source == List10Button){ listnumber = 10;}
	public void keyPressed(KeyEvent event) {
		if (event.getKeyChar() == 'a'){
			showeng = !showeng;
	public void keyReleased(KeyEvent event){}
	public void keyTyped(KeyEvent event){}
	public static void main(String[] arguments) {
        ClickyLatinHelp TLH = new ClickyLatinHelp();

First thing is try to debug the code by adding println()s to see where the code is executing.
The code does not compile:

ClickyLatinHelp.java:78: cannot find symbol
symbol  : class Lists
location: class ClickyLatinHelp.DrawingArea
    	Lists listhi = new Lists();
ClickyLatinHelp.java:78: cannot find symbol
symbol  : class Lists
location: class ClickyLatinHelp.DrawingArea
    	Lists listhi = new Lists();
ClickyLatinHelp.java:68: warning: [serial] serializable class ClickyLatinHelp.DrawingArea has no definition of serialVersionUID

	public class DrawingArea extends JPanel {
ClickyLatinHelp.java:5: warning: [serial] serializable class ClickyLatinHelp has no definition of serialVersionUID
public class ClickyLatinHelp extends JFrame implements ActionListener, KeyListener {
2 errors
2 warnings

4 error(s)

Could it be that it doesn't compile becasue you don't have the .class file for the Lists class I made?

public class Lists {
    String[][] ListArrays = {{"Why, Hello there!","Welcome to Latin Help.","Use the buttons above to select a list.","Eventually, right clicking will toggle the English.", null},
    {"This is where List 1 will go.", null}, {"This is where List 2 will go.", null}, {"This is where List 3 will go.", null}, 
    {"This is where List 4 will go.", null}, {"This is where List 5 will go.", null}, {"This is where List 6 will go.", null},
    {"This is where List 7 will go.", null}, {"This is where List 8 will go.", null}, {"This is where List 9 will go.", null},
    {"Ago, agere, egi, actus", 
    "Cogo, cogere, coegi, coactus",
    "Exigo, exigere, exegi, exactus",
    "Alo, alere, alui, alitus",
    "Bibo, bibere, bibi, ----",
    "Caedo, caedere, cecidi, caesus", 
    "Cedo, cedere, cessi, cessus",
    "Cognosco, cognoscere, cognovi, cognitus",
    "Comprehendo, comprehendere, comprehensi, comprehensus",
    "Consumo, consumere, consumpsi, consumptus",
    "Condo, condere, condidi, conditus",
    "Contundo, contundere, contudi, contusus",
    "Diligo, diligere, dilexi, dilectus",
    "Emo, emere, emi, emptus",
    "Fero, ferre, tuli, latus",
    "Fluo, fluere, fluxi, fluctus",
    "Linquo, linquere, liqui, ----",
    "Relinquo, relinquere, reliqui, relictus",
    "Metuo, metuere, metui, metuus",
    "Occido, occidere, occidi, occisus",
    "Occido, occidere, occidi, occasum",
    "Pendo, pendere, pependi, pensus",
    "Suspendo, suspendere, suspendi, suspensus",
    "surgo, surgere, surrexi, surrectum",
    "tango, tangere, tetigi, tactus",
    "tego, tegere, texi, tectus",
    "Tollo, tollere, sustuli, sublatus",
    "Trado, tradere, tradidi, traditus",
    "Vendo, vendere, vendidi, venditus",
    "Verto, vertere, verti, versus",
    String[][] EnglishArrays = {{null},{null},{null},{null},{null},{null},{null},{null},{null},{null},
    {"lead, drive, do, act",
    "force, compel",
    "drive out",
    "found, establish",
    "beat, batter, bruise",
    "love, cherish",
    "carry, bring, bear",
    "leave behind",
    "kill, cut down",
    "fall down, die",
    "hang beneath",
    "rise up",
    "lift, remove, destroy ",
    "hand over",

Have you tried debugging your code using println()s?

the obvious questions:
Do you have key listeners?
are they connected to any components?
Does the component have focus?

Edited 6 Years Ago by NormR1: n/a

Yeah, it's showing that for some reason, my program isn't picking up on key events, even though I implemented the key Listener interface and overrode it's methods.

What do you mean by: "are they connected to any components?" The key listener is implemented in the driver class and the extended JPanel is focusable.

You must add the KeyListener to a component with the addKeyListener() method. It's similar to what you did with the ActionListener for the buttons.

Implementing the interface just defines what the listener does. Then you attach it to the component you want to listen to.

OH! Duh! Sorry, the book I'm learning java from didn't mention a thing about having to use an addKeyListener() method. Now everything makes sense. Thanks so much.

This question has already been answered. Start a new discussion instead.