Reputation: 163
I'm trying to build screen keyboard and i need a little help.
The idea: text copied(Copy and Paste )from an external source will be used for learning touch typing.
When you type the correct character, the character is highlighted in the JTextArea to green color, and the button color will be changed to green,
if you type the incorrect character, so the button will change it color to red
When I press in my pc keyboard the buttons on the screen keyboard not changes its background to green .
public class Box extends JFrame {
JFrame frame = new JFrame("Box Value & Area");
GridLayout Form_Grig = new GridLayout(3,0);
GridLayout Panel_Grid = new GridLayout(1,18);
JPanel ans = new JPanel(new GridLayout(1,2));
final JLabel label =new JLabel("Points : ");
final JLabel label2 = new JLabel("answer ");
final JLabel label3 = new JLabel("length : ");
final JLabel label4 = new JLabel("Volume");
public int index=0;
public int counter=0;
public int press_count=-1;
JPanel panel = new JPanel();
private JPanel parent = new JPanel(new GridLayout(0, 1));
final JTextArea text1 = new JTextArea();
private JButton[][] button;
private JPanel[] Panel = new JPanel[6];
public JButton start = new JButton("Start game");
private static final String[][] key = {
{"Esc", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "Num", "Insert", "Delete", "Pause"},
{"~\n`", "1", "2", "3", "4", "5","6", "7", "8", "9", "0", "-", "=", "Backspace "},
{" Tab ", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "{", "}", "|"},
{"Caps Lock","A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", " Enter "},
{" Shift ", "Z", "X", "C", "V", "B","N", "M", "<", ">", "?", "Shift"},
{"Ctrl", "Fn", "Alt", " Space ", "Alt","Ctrl"}};
public Box(){
frame.setLayout(Form_Grig);
frame.add(text1 );
ans.add(label);
ans.add(label2);
button = new JButton[20][20];
for (int row = 0; row < key.length; row++) {
Panel[row] = new JPanel();
for (int column = 0; column < key[row].length; column++) {
button[row][column] = new JButton(key[row][column]);
button[row][column].putClientProperty("column", column);
button[row][column].putClientProperty("row", row);
button[row][column].putClientProperty("key", key[row][column]);
button[row][column].addActionListener(new MyActionListener());
Panel[row].add(button[row][column]);
}
parent.add(Panel[row]);
}
frame.add(ans);
frame.add(parent);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
Font font = new Font("Verdana", Font.BOLD, 56);
label2.setFont(font);
label2.setForeground(Color.gray);
label.setFont(font);
label.setForeground(Color.gray);
}
public class MyActionListener implements ActionListener {
String te = text1.getText();
@Override
public void actionPerformed(ActionEvent e) {
final JButton btn =(JButton) e.getSource();
text1.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if (!text1.getText().equals("")){
text1.setEditable(false);
}
btn.setBackground(Color.green);
System.out.println("this :"+ btn.getName());
}
@Override
public void keyReleased(KeyEvent e) { //dsd
}
@Override
public void keyTyped(KeyEvent e) {//sdsd
// TODO Auto-generated method stub
}
});
String te = text1.getText();
String Size = null;
if(press_count >= te.length() ){
JOptionPane.showMessageDialog(null, "from "+te.length() +" characters you guess "+index );
}
press_count++;
Size = Integer.toString(te.length());//adad
if (index==0)
counter=te.length();
label2.setText(Size); //setText
String bbb = (String) btn.getClientProperty("key") ;
if(bbb.equals(String.valueOf(te.charAt(index)))){
btn.setBackground(Color.green);
label2.setText(Integer.toString(counter));
}
else{
btn.setBackground(Color.red);//setting background
counter--;
label2.setText(Integer.toString(counter));
}
index++;//
}
}
Why when i press the pc buttons, the buttons on the screen keyboard doesn't change its background color ?
Upvotes: 0
Views: 531
Reputation: 208984
"Why when I press the pc buttons, the buttons on the screen keyboard doesn't change its background color"
They do change, but look at your logic,:
It only should change color when a key is pressed on the text area. Meaning you need to focus back to the text area and type something.
You are also adding multiple KeyListener
to the text area, on every button press. I don't think that is what you are trying to do.
Actually. you shouldn't be using a KeyListener
at all for text components. Changes in the underlying document of the text components should be listened for via a DocumentListener
. The DocumentListener
should only be added once to the text area, outside of the ActionListener
. The reason is that a component may have more then one listener registered to it, even if it is the same listener (as you are doing). What happens is that each listener call back gets put into a queue. So if you've pressed 10 buttons, then the text area will have 10 listener callbacks, and every time you press a key, the call back will get called 10 times.
You should read more on How To Use DocumentListner
Also for the presses of the changing of the color to the button, you may want to take a look at this question.
Upvotes: 1