I have made the JButtons just like Keyboard, which when the user type, then what he/she typed must be displayed on the textfield. And as the user press the keyboard, the colour of keys on the screen should be changed then go back to the original colour when the key is not pressed anymore.

I am sure that I should use keyListener but no matter how i try, i cant seems to make tat buttons change colour.

It will be greate full if anyone explain or show simple codes. Thank you.

You could try using JLabels instead of JButtons in the GUI, then in the keyListener change the background of the JLabel as needed. Do you have the keyListener defined in the same class as your JButtons? If not you may run into communication problems between the two.

hsuan,
let us assume that the default color of keys on your painted keybord is red, and that when you press a key on your keyboard, the color is changed to blue.

say now that you press key D and it turns blue on your screen. do you want it to stay blue until you press another letter, or do you just want it to stay blue for a few moments and then turn back to red regardless if you press another key?! let me know, and I'll give you an idea.

hsuan,
let us assume that the default color of keys on your painted keybord is red, and that when you press a key on your keyboard, the color is changed to blue.

say now that you press key D and it turns blue on your screen. do you want it to stay blue until you press another letter, or do you just want it to stay blue for a few moments and then turn back to red regardless if you press another key?! let me know, and I'll give you an idea.

Hey...

I too developed similar typing tutor few months ago and i too encountered the same..

but the thing is did you use an image to show what key is press??. If so, then you need to

have all images which shows every key pressed . when the keys are pressed you should

repaint the picture with appropriate one

Hi, my friend......

Have you got answer for your problem? Here is solution for your problem. Test and try it.

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;

public class abc extends JFrame implements KeyListener
{
	JButton b1,b2 ;
	
	public abc()
	{
		Container con = this.getContentPane();
		
		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(1,2));
		
		JPanel p2 = new JPanel();
		b1 = new JButton("  Press A  ");
		b1.setFont(new Font("System",Font.PLAIN,18));
		b1.setBackground(Color.blue);
		p2.add(b1);
		p1.add(p2);
		
		JPanel p3 = new JPanel();
		b2 = new JButton("  Press B  ");
		b2.setSelected(true);
		b2.setFont(new Font("System",Font.PLAIN,18));
		b2.setBackground(Color.blue);			
		p3.add(b2);
		p1.add(p3);
	
		con.add(p1);

		con.addKeyListener(this);  // important !!!
		b1.addKeyListener(this);   // important !!!
		b2.addKeyListener(this);   // important !!!
		p1.addKeyListener(this);   // important !!!
		p2.addKeyListener(this);   // important !!!
		p3.addKeyListener(this);   // important !!!
		
		this.setLocation(30, 30);
		this.pack();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);	
		
	}
	public static void main(String args[])
	{
		new abc();
	}
	public void keyPressed(KeyEvent evt) 
	{
		if(evt.getKeyChar() == 'a')
		{
			b1.setBackground(Color.red);
		}
		else if(evt.getKeyChar() == 'b')
		{
			b2.setBackground(Color.red);
		}		
	}
	public void keyReleased(KeyEvent evt)
	{		
			b1.setBackground(Color.blue);			
			b2.setBackground(Color.blue);	
		
	}
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

Explanation : To generate an event from a component, we need to meet 2 requirements. These are .....
1. When we start running program, the focus must exist on this component. (In my program, the default focus will exist on Button 'Press A' .)
2. We need to register this component. (I mean that for Eg. button.addKeyListener(this); etc....)

When we meet these 2 requirements, we can easily catch the events from this component. You said that the buttons color doesn't change although you catch the KeyListener event on these buttons. Yeah! it could be... this problem will arise sometimes when we don't care window is focusing on which component. Sometimes focus is existing on JPanel. When you don't catch KeyListener event on JPanel ( i mean : panel.addKeyListener(this); ), you will not get keyEvent from JPanel. So, your program will not do statements written in keyEvent method. In conclusion, your problem emerge because you miss to register KeyListener event upon a component and focus is existing on this component. That is why you will not see anything when you pressed Keys. So, The solution is that "look for window is focusing on which component and just register this component."

ps : Register means (eg. button.addKeyListener(this); ) ....... component means (eg. JButton, JPanel, etc... )

* if my program doesn't meet your problem, come back. I will await you.......

I hope to be more power to you and your bright futures :):):)

This article has been dead for over six months. Start a new discussion instead.