Prueba esto espero y te sirva
Código PHP:
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import javax.swing.border.BevelBorder;
public class keyenter extends JFrame
{
JFrame t;
private JList list;
private JTextField caja=new JTextField();
private JTextField text=new JTextField();
private String returnValue;
keyenter()
{
super("OK");
String ACTION_KEY = "The Action";
final JButton boton= new JButton("<html><center>ENTER<br>Window/Pressed");
final JButton bot2=new JButton("<html><center>CONTROL C<br>Window/Pressed");
final JButton k=new JButton("test");
JPanel pan=new JPanel();
final JDialog ok;
final JTextField text=new JTextField(10);
//pan.setLayout(null);
pan.add(text);
Container pane = this.getContentPane();
pane.add(boton, BorderLayout.SOUTH);
pane.add(pan,BorderLayout.CENTER);
pane.add(bot2,BorderLayout.NORTH);
AbstractAction pasaFoco = new AbstractAction()
{
public void actionPerformed(ActionEvent actionEvent)
{
JButton source = (JButton) actionEvent.getSource();
System.out.println("Activated: " + source.getText());
if (source==bot2)
{
String retVal = new dialog(t,keyenter.this).getName();
String temp = "";
if(retVal == null)
temp = "Cancelled";
else if(retVal.equals(""))
temp = "nothing entered";
else
temp = retVal;
text.setText(temp);
}
}
};
bot2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
String retVal = new dialog(t,keyenter.this).getName();
String temp = "";
if(retVal == null)
temp = "Cancelled";
else if(retVal.equals(""))
temp = "nothing entered";
else
temp = retVal;
text.setText(temp);
}
});
boton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String source=boton.getText();
System.out.println("Activated:"+ source);
}
});
InputMap inputMap = new InputMap();
KeyStroke strok=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK,true);
inputMap=bot2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(strok,ACTION_KEY);
ActionMap actionBot=bot2.getActionMap();
actionBot.put(ACTION_KEY,pasaFoco);
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,true);
inputMap = boton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke,ACTION_KEY);
ActionMap actionMap = boton.getActionMap();
actionMap.put(ACTION_KEY, pasaFoco);
setSize(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public static void main(String [] args)
{
new keyenter();
}
class dialog {
public dialog(JFrame fr, keyenter t)
{
keyenter padre;
padre=t;
final JButton k=new JButton("test");
final JDialog ok=new JDialog(fr,true);
String val=ok.getName();
final JTextField caja=new JTextField();
k.setEnabled(false);
String ar[]={"uno","dos","tres","four","five","six","seven","salir"};
final JList list=new JList(ar);
JScrollPane js=new JScrollPane(list);
caja.setBounds(new Rectangle(10,10,120,20));
k.setBounds(new Rectangle(20,30,60,30));
k.setMnemonic(KeyEvent.VK_T);
JPanel p=new JPanel();
JPanel p2=new JPanel();
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setOpaque(true);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt)
{
if (evt.getValueIsAdjusting()==false)
{
//return;
// System.out.println("Selected from " + evt.getFirstIndex() + " to " + evt.getLastIndex());
//boolean anySelected = !list.isSelectionEmpty();
if (list.getSelectedIndex()==-1)
{
k.setEnabled(false);
}
else
{
k.setEnabled(true);
String name=list.getSelectedValue().toString();
caja.setText(name);
System.out.println(name);
}
}
}
});
list.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent ev )
{
if (ev.getKeyCode()==KeyEvent.VK_ENTER)
{
returnValue=caja.getText();
ok.dispose();
}
}
}
);
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent ev)
{
if (ev.getClickCount() == 2)
{
System.out.println(" double click" );
returnValue=caja.getText();
ok.dispose();
}
}
}
);
k.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
returnValue=caja.getText();
ok.dispose();
}
});
ok.setLayout(new GridLayout(2,1));
ok.getRootPane().setBorder(new BevelBorder(BevelBorder.LOWERED));
ok.add(p);
ok.add(p2);
p.setLayout(null);
p.add(caja);
p.add(k);
p2.setLayout(new GridLayout(1,1));
p2.setPreferredSize(new java.awt.Dimension(40,80));
p2.add(js);
ok.setSize(200,180);
ok.setVisible(true);
}
public String getName()
{
return returnValue;
}
}
}////fin de la clase keyenter
Saludos