I have a margin for a couple of hours LOL, and not fun, but here's why .. I have an input application that should set the button's background to a different color so as not to say yellow (for example, highlighted) when pressing or pressing a button, each button will be highlighted and it will return the background back to white when released. Well, my KeyListener, I think it has no focus, but I seem to be losing something or not seeing it there is what happens ..
I had to comment on adding a listener because to throw a null pointer exception ..
I really hope someone can understand what I cannot or what I missed. This is a run-through code that will make it easier for you guys to understand what is happening, and check it yourself and see what I mean ..
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class typeTutor extends JFrame implements KeyListener
{
String firstRow[] = {"~", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "+", "fill", "BackSpace"};
String secondRow[] = {"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\"};
String thirdRow[] = {"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ":", "\"", "fill", "fill", "Enter"};
String fourthRow[] = {"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "?", "blank", "^"};
String fifthRow[] = {"blank", "blank", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "fill", "", "<", "v", ">"};
private JTextField textField;
String strText = "";
String noShift="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./";
String specialChars ="~-+[]\\;',.?";
int keycode;
JTextArea text = new JTextArea();
JButton first[];
JButton second[];
JButton third[];
JButton fourth[];
JButton fifth[];
Color cc = new JButton().getBackground();
public static void main(String[] args) {
typeTutor a = new typeTutor();
a.setVisible(true);
}
public typeTutor()
{
super("Type Tutor - My JAC444");
JPanel contentPane = new JPanel() {
@Override
protected void paintComponent(Graphics grphcs) {
Graphics2D g2d = (Graphics2D) grphcs;
Dimension d = this.getSize();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0,
getBackground().brighter().brighter(), 0, d.height,
getBackground().darker().darker());
g2d.setPaint(gp);
g2d.fillRect(0, 0, d.width, d.height);
super.paintComponent(grphcs);
}
};
contentPane.setOpaque(false);
setContentPane(contentPane);
TextFieldHandler handler = new TextFieldHandler();
JLabel info = new JLabel("<html> Type some text using your keyboard.The keys you press will be highlighted and text will be displayed.<br> Note : Clicking the buttons with your mouse will not perform any action. <br><br> </html>" );
info.setFont(new Font("Verdana",Font.BOLD,12));
setLayout(new BorderLayout());
JPanel jpNorth = new JPanel();
JPanel jpCenter = new JPanel();
jpCenter.setPreferredSize(new Dimension(10,10));
JPanel jpKeyboard = new JPanel(new GridBagLayout());
JPanel jpNote = new JPanel();
add(jpNorth, BorderLayout.NORTH);
add(jpNote);
add(jpCenter, BorderLayout.CENTER);
add(jpKeyboard, BorderLayout.SOUTH);
jpNorth.setLayout(new BorderLayout());
jpNorth.add(info, BorderLayout.WEST);
jpNorth.add(info, BorderLayout.SOUTH);
jpCenter.setLayout( new BorderLayout());
jpCenter.add(text, BorderLayout.WEST);
jpCenter.add(text, BorderLayout.CENTER);
jpCenter.setPreferredSize(new Dimension(10,10));
first = new JButton[firstRow.length];
second = new JButton[secondRow.length];
third = new JButton[thirdRow.length];
fourth = new JButton[fourthRow.length];
fifth = new JButton[fifthRow.length];
addKeys(jpKeyboard, 0, firstRow, first);
addKeys(jpKeyboard, 1, secondRow, second);
addKeys(jpKeyboard, 2, thirdRow, third);
addKeys(jpKeyboard, 3, fourthRow, fourth);
addKeys(jpKeyboard, 4, fifthRow, fifth);
jpKeyboard.setPreferredSize(new Dimension(800,160));
info.setOpaque(false);
jpNote.setOpaque(false);
jpNorth.setOpaque(false);
jpCenter.setOpaque(false);
jpKeyboard.setOpaque(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.getContentPane().setPreferredSize(new Dimension(1100,350));
initWidgets();
pack();
this.toFront();
this.setLocationRelativeTo(null);
}
private void initWidgets()
{
}
private class TextAreaHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String string = "";
if ( event.getSource() == textField )
string = String.format( "%s",
event.getActionCommand() );
}
}
public void keyPressed(KeyEvent evt)
{
keycode = evt.getKeyCode();
strText = String.format( "%s",KeyEvent.getKeyText( evt.getKeyCode() ) );
setBackground(Color.BLUE);
}
public void keyReleased(KeyEvent evt)
{
keycode = evt.getKeyCode();
strText = String.format( "%s",KeyEvent.getKeyText( evt.getKeyCode() ) );
getBackground();
}
public void keyTyped(KeyEvent evt)
{
strText = String.format( "%s", evt.getKeyChar() );
}
private class TextFieldHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String string = "";
if ( event.getSource() == text )
strText = String.format( "%s", event.getActionCommand() );
}
}
protected void addKeys(JPanel parent, int row, String[] keys, JButton[] buttons) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = row;
gbc.gridx = 0;
gbc.fill = GridBagConstraints.BOTH;
int gap = 0;
for (int index = 0; index < keys.length; index++) {
String key = keys[index];
if ("blank".equalsIgnoreCase(key)) {
gbc.gridx++;
} else if ("fill".equalsIgnoreCase(key)) {
gbc.gridwidth++;
gap++;
} else {
JButton btn = new JButton(key);
buttons[index] = btn;
parent.add(btn, gbc);
gbc.gridx += gap + 1;
gbc.gridwidth = 1;
gap = 0;
}
}
}
}
If you guys need more clarification or have any questions about this, please let me know and I will provide as much information as possible. I will be very grateful to you for your help, you, how upset it is when you are in the warehouse, and can not find the answer to the problem. Not fun! :(