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 ?