I'm writing a Chat program. I designed a mock-up gui with smileys where when the user clicks on a smiley(jbutton) it prints it onto a textpane. I managed to add an advanced feature where when a user types in ":)" and sends it, it inserts the smiley instead of the string - using the insertIcon() method. The problem I have is that it only prints the smiley once rather than multiple times. So if I type "Hi :) My name is Jack :)" it only inserts the icon ONCE. Any suggestions?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile1.png")));
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile2.png")));
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile3.png")));
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile4.png")));
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile5.png")));
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile6.png")));
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile7.png")));
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile8.png")));
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource(
"smileys/Smile9.png")));
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
// tPane.setText(a);
// tPane.getText();
if (a.equals(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.equals(":)")) {
tPane.insertIcon(smile);
} else if (a.equals(":(")) {
tPane.insertIcon(sad);
} else if (a.equalsIgnoreCase(":P")) {
tPane.insertIcon(tongue);
} else if (a.equals(";)")) {
tPane.insertIcon(wink);
}
text.setText(null);
text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
insertIcon() method of JTextPane uses selection (caret position in simplest case). So in your case you always replace the icon just once.
Your sendMessage() doesn't check multiple occurences of ":)" in the message. Use while loop obtaining indexes of the ":)" and for each index make it selected and then use insertIcon()
Related
I could use two ActionLister to perform the same action but I use the implement to make the code condense but it just doesnt work.
When I choose b1, there is no text on the textfield.
public Radio_Button() {
setSize(600, 400);
panel = new JPanel();
tf = new JTextField(" ");
group = new ButtonGroup();
b1 = new JRadioButton("1");
b2 = new JRadioButton("2");
b1.setActionCommand("you choose one");
b2.setActionCommand("you choose two");
group.add(b1);
group.add(b2);
panel.add(b1);
panel.add(b2);
panel.add(tf);
add(panel);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tf.setText(e.getActionCommand());
}
}
You forget to register a listener for the radio buttons.
Add this after setActionCommand().
b1.addActionListener(this);
b2.addActionListener(this);
In case you have different buttons
JButton button1 = new JButton("Some Text");
JButton button2 = new JButton("Some Other Text");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "I was clicked !");
} });
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Second button was clicked!");
} });
I'm writing a chat program in Java. I made a section for smileys. I managed to print the smileys on a text pane every time its clicked but when I click on the same smiley more than once it only prints it once. Any help?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SmileyTesterGUI extends JFrame {
JPanel main = new JPanel();
JPanel south = new JPanel();
JPanel messageCenter = new JPanel();
JPanel smileysNorth = new JPanel();
JTextField text;
JTextPane tPane;
Icon happy;
Icon smile;
Icon tongue;
Icon veryHappy;
Icon wink;
Icon laugh;
Icon sad;
Icon verySad;
Icon cry;
int number = 0;
boolean check = true;
public SmileyTesterGUI() {
super("Smileys");
add(main);
main.setLayout(new BorderLayout());
main.add(south, BorderLayout.SOUTH);
south.setLayout(new BorderLayout());
south.add(messageCenter, BorderLayout.CENTER);
south.add(smileysNorth, BorderLayout.NORTH);
// textpane panel
tPane = new JTextPane();
JScrollPane sPane = new JScrollPane(tPane);
main.add(sPane);
tPane.setEditable(false);
// smileysPanel
smileysNorth.setLayout(new GridLayout(1, 0));
JButton smiley1 = new JButton();
JButton smiley2 = new JButton();
JButton smiley3 = new JButton();
JButton smiley4 = new JButton();
JButton smiley5 = new JButton();
JButton smiley6 = new JButton();
JButton smiley7 = new JButton();
JButton smiley8 = new JButton();
JButton smiley9 = new JButton();
smileysNorth.add(smiley1);
smileysNorth.add(smiley2);
smileysNorth.add(smiley3);
smileysNorth.add(smiley4);
smileysNorth.add(smiley5);
smileysNorth.add(smiley6);
smileysNorth.add(smiley7);
smileysNorth.add(smiley8);
smileysNorth.add(smiley9);
// set smileys(icon) to each button - pathed from personal directory
happy = new ImageIcon(getClass().getResource("smileys/Smile1.png"));
smiley1.setIcon(happy);
smile = new ImageIcon(getClass().getResource("smileys/Smile2.png"));
smiley2.setIcon(smile);
tongue = new ImageIcon(getClass().getResource("smileys/Smile3.png"));
smiley3.setIcon(tongue);
veryHappy = new ImageIcon(getClass().getResource("smileys/Smile4.png"));
smiley4.setIcon(veryHappy);
wink = new ImageIcon(getClass().getResource("smileys/Smile5.png"));
smiley5.setIcon(wink);
laugh = new ImageIcon(getClass().getResource("smileys/Smile6.png"));
smiley6.setIcon(laugh);
sad = new ImageIcon(getClass().getResource("smileys/Smile7.png"));
smiley7.setIcon(sad);
verySad = new ImageIcon(getClass().getResource("smileys/Smile8.png"));
smiley8.setIcon(verySad);
cry = new ImageIcon(getClass().getResource("smileys/Smile9.png"));
smiley9.setIcon(cry);
// smileys print on the textpane
smiley1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(happy);
}
});
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(smile);
}
});
smiley3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(tongue);
}
});
smiley4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(veryHappy);
}
});
smiley5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(wink);
}
});
smiley6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(laugh);
}
});
smiley7.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(sad);
}
});
smiley8.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(verySad);
}
});
smiley9.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
tPane.insertIcon(cry);
}
});
// messagePanel
messageCenter.setLayout(new BorderLayout());
text = new JTextField();
JButton send = new JButton("Send");
messageCenter.add(text);
messageCenter.add(send, BorderLayout.EAST);
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
send.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
setLocation(500, 0);
setSize(600, 250);
}
public void sendMessage() {
String a = text.getText();
//
// if(a){
// tPane.setText(a);
// tPane.getText();
// }
if (a.contains(":D")) {
tPane.insertIcon(veryHappy);
} else if (a.contains(":)")) {
tPane.insertIcon(happy);
} else if (a.contains(":(")) {
tPane.insertIcon(sad);
}
// text.setText(null);
// text.requestFocus();
}
public static void main(String[] args) {
new SmileyTesterGUI().setVisible(true);
}
}
That is because you cannot add the same ImageIcon to the JTextPane more then once.
You can create the ImageIcons dynamically in the ActionListener, every time a smiley is clicked create a new ImageIcon
smiley2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tPane.insertIcon(new ImageIcon(getClass().getResource("smileys/Smile2.png")));
}
});
I have this class. This class paints 3 buttons, and I have an event associated with each button. I need to create an event for the keyboard, pressing a key will be the same as pressing one of the buttons. This is my class:
public class ShapedDialog extends JDialog implements KeyListener, ActionListener {
private final MainWindow parent;
public ShapedDialog(MainWindow parent, String title) {
super(parent, title, true);
this.parent = parent;
if (parent != null) {
Dimension parentSize = parent.getSize();
Point p = parent.getLocation();
setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
}
JPanel messagePane = new JPanel();
messagePane.add(new JLabel());
getContentPane().add(messagePane);
JPanel buttonPane = new JPanel();
//JButton1
JButton jButton1 = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("logo.png"));
jButton1.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
jButton1.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
//JButton2
JButton jButton2 = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("logo.png"));
jButton2.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
jButton2.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
//JButton3
JButton jButton3 = new JButton();
try {
Image img = ImageIO.read(getClass().getResource("logo.png"));
jButton3.setIcon(new ImageIcon(img));
} catch (IOException ex) {
}
jButton3.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
//Add buttons in the panel
buttonPane.setLayout(new GridLayout(3, 3));
buttonPane.add(jButton1);
buttonPane.add(jButton2);
buttonPane.add(jButton3);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
setVisible(false);
parent.setMyPackage("Type 1");
dispose();
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
setVisible(false);
parent.setMyPackage("Type 2");
dispose();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here
setVisible(false);
parent.setMyPackage("Type 3");
dispose();
}
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.setMyPackage("Type Package");
dispose();
}
public static void main(String[] a) {
JTextField component = new JTextField();
component.addKeyListener(new MyKeyListener());
ShapedDialog dlg = new ShapedDialog((MainWindow) new JFrame(), "title");
}
#Override
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public void keyPressed(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
#Override
public void keyReleased(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt) {
if (evt.getKeyChar() == 'a') {
System.out.println("Check for key characters: " + evt.getKeyChar());
}
if (evt.getKeyCode() == KeyEvent.VK_HOME) {
System.out.println("Check for key codes: " + evt.getKeyCode());
}
}
}
Someone can help me??
I need to create an event for the keyboard, pressing a key will be the same as pressing one of the buttons.
You need to:
Create an Action to be used by each button. See How to Use Actions
Create the JButton using the Action
Create Key Bindings for the Action and KeyStroke. See How to Use Key Bindings
Simple example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CalculatorPanel extends JPanel
{
private JTextField display;
public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
#Override
public void actionPerformed(ActionEvent e)
{
display.setCaretPosition( display.getDocument().getLength() );
display.replaceSelection(e.getActionCommand());
}
};
setLayout( new BorderLayout() );
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(50, 50) );
buttonPanel.add( button );
KeyStroke pressed = KeyStroke.getKeyStroke(text);
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(pressed, text);
button.getActionMap().put(text, numberAction);
}
}
private static void createAndShowUI()
{
// UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );
JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
I have a String called s
I have four JButtons b1 b2 b3 b4
By default String s is Hello World
b1 Makes String s toUpperCase
b2 Makes String s toLowerCase
b3 Allows user to change String
b4 Resets String to Hello World
When I run the program I have s set to Hello World but when I click my buttons the String is not changing to its new attribute.
import java.awt.EventQueue;
import java.awt.event.*;
import javax.swing.*;
public class Driver {
String s = "Hell World";
String up;
String low;
private JFrame f;
private JPanel p;
JFrame frame = new JFrame("Add Some Text");
JButton b1 = new JButton("Uppercase");
JButton b2 = new JButton("Lowercase");
JButton b3 = new JButton("New");
JButton b4 = new JButton("Reset");
JLabel jl = new JLabel(s);
public Driver () {
gui();
}
public void gui() {
f = new JFrame("Hello World");
p = new JPanel();
f.add(p);
p.setLayout(null);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(jl);
b1.setLocation(27, 80);
b2.setLocation(300, 80);
b3.setLocation(27, 180);
b4.setLocation(300, 180);
jl.setLocation(240, 20);
b1.setSize(230, 80);
b2.setSize(230, 80);
b3.setSize(230, 80);
b4.setSize(230, 80);
jl.setSize(230, 20);
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
f.setSize(560,300); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
up = s.toUpperCase();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
low = s.toLowerCase();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = "Hello World";
}
});
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method
} // End class Driver
You have to set the text to the label!
Try this:
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toUpperCase());
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText(s.toLowerCase());
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl.setText("Hello World");
}
});
Actually the String is changing, what happens here that you haven't update the JLabel with the new value for the String:
So your code should look like this:
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
s = JOptionPane.showInputDialog(frame, "");
jl.setText(s);
}
});
then add the following line in each Action ActionListener.
jl.setText(newValue);// newValue depends on what the ActionListener do.
In my form, when i press ENTER button in my keyboard, the okAction() method should be invoke (and invoke perfectly).
My problem is in focus state, When i fill the text fields and then press the ENTER button, the okAction() didn't invoked, because the focus is on the second text field (not on the panel).
How fix this problem?
public class T3 extends JFrame implements ActionListener {
JButton cancelBtn, okBtn;
JLabel fNameLbl, lNameLbl, tempBtn;
JTextField fNameTf, lNameTf;
public T3() {
add(createForm(), BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new T3();
}
});
}
public JPanel createForm() {
JPanel panel = new JPanel();
panel.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "Button");
panel.getActionMap().put("Button", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
okAction();
}
});
okBtn = new JButton("Ok");
okBtn.addActionListener(this);
cancelBtn = new JButton("Cancel");
tempBtn = new JLabel();
fNameLbl = new JLabel("First Name");
lNameLbl = new JLabel("Last Name");
fNameTf = new JTextField(10);
fNameTf.setName("FnTF");
lNameTf = new JTextField(10);
lNameTf.setName("LnTF");
panel.add(fNameLbl);
panel.add(fNameTf);
panel.add(lNameLbl);
panel.add(lNameTf);
panel.add(okBtn);
panel.add(cancelBtn);
panel.add(tempBtn);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 3, 2, 50, 10, 80, 60);
return panel;
}
private void okAction() {
if (fNameTf.getText().trim().length() != 0 && lNameTf.getText().trim().length() != 0) {
System.out.println("Data saved");
} else System.out.println("invalid data");
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == okBtn) {
okAction();
}
}
}
Declare a default button for your GUI's JRootPane:
public T3() {
//!! ..... etc...
setVisible(true);
getRootPane().setDefaultButton(okBtn);
}
In fact with a default button set, I don't see that you need to use key bindings.