Hola:
He estado probando y el método que te he dicho a mí me da el botón (cosa lógica, al pulsarlo el JTextField pierde el foco y lo gana el botón).
Código:
package com.chuidiang.prueba;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.FocusManager;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Prueba {
/**
* @param args
*/
public static void main(String[] args) {
JFrame v = new JFrame();
JTextField tf = new JTextField(10);
JButton b = new JButton("pulsa");
v.getContentPane().setLayout(new FlowLayout());
v.getContentPane().add(tf);
v.getContentPane().add(b);
v.pack();
v.setLocationRelativeTo(null);
v.setVisible(true);
v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(FocusManager.getCurrentManager()
.getFocusOwner());
}
});
}
}
El siguiente trozo de código
Código:
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
@Override
public void eventDispatched(AWTEvent event) {
if (event instanceof FocusEvent) {
if (((FocusEvent) event).getID() == FocusEvent.FOCUS_LOST) {
System.out.println(((FocusEvent) event).getComponent());
}
}
}
}, AWTEvent.FOCUS_EVENT_MASK);
va recibiendo los componentes que pierden el foco. Con algo como eso puedes guardar en algún sitio cual es el último JTextField que pierde el foco y usarlo en el botón. No me gusta la solución (parece muy rebuscada).
Se bueno.