I have two JFrames jFrame1 and jFrame2,in jFrame1 there’s a textfield and a button,while clicking on button jFrame2 will appear. In jFrame2 there’s also a textfield and a button.I will type a name in textfield of jFrame2 and by clicking the button in it that textfield value should appear on textfield of jFrame1. But I am not getting the focus transferred to jFrame1,i tried the code,
in jFrame1
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jFrame2 abc=new jFrame2();
abc.setVisible(true);
}
public void inserting(String name){
jTextField1.requestFocusInWindow();
jTextField1.setText(name);
}
in jFrame2,
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jFrame1 abc1=new jFrame1();
// abc1.transferFocus(); //not working
abc1.inserting(jTextField1.getText());
this.dispose();
}
I am getting value to the method inserting(),but it’s not getting set into the textfield. If I again give setVisible(true) for jFrame1 it works,but I dont want to do i in that way. Is there any other way to resolve this?
To bring focus to the field, you should use
requestFocusInWindow, however I don’t think that will bring the window in question back into focus.You could use a
WindowListenerto monitor changes that you could respond.For example, in
jFrame1‘sactionPerformedhandler you couldjFrame1is requesting the text fromjFrame2causejFrame2doesn’t know aboutjFrame1, there’s no reference to it.In
jFrame2you would need to add aWindowListenerto handle the request for focus of the text field