My question is related about java swing frame. I have a two jframe. jframe1 and jframe2. there is a one jbutton in the jframe1 and when user click the jbutton I want to show jframe 2. jframe2 have a textbox and jbutton user can enter value to textbox,when user click the jbutton , I want to set focus to 1st jframe and pass user entered value to jlable in the jrame1. Please help me to do this.
Asked
Active
Viewed 2,671 times
0
-
2Do you have any code to support what you are saying? – Josh M Aug 19 '13 at 16:15
-
2-1: This is basic functionality that can be easily figured out via google – StormeHawke Aug 19 '13 at 16:15
-
1See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Think 'one frame, many (modal) dialogs'. – Andrew Thompson Aug 19 '13 at 18:16
1 Answers
2
Seems to me like the second frame is rather a dialog, used to enter a value, which would then be returned to the caller (first frame).
To achieve this, you create a modal JDialog
, add the controls there (text field, ok and maybe a cancel button) and add a method which opens the dialog (blocks the caller) and returns the entered text unless it was cancelled. That way, you can pass the entered text directly, whithout having to temporarily store it into a variable (which usually is bad practice).
Here's a simple implementation of such a dialog:
public class SwingTestDialog extends JDialog {
private JTextField text;
private boolean cancelled = true;
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run () {
SwingTestDialog dialog = new SwingTestDialog();
String text = dialog.selectValue();
System.out.println("Selected: " + text);
}
});
}
public SwingTestDialog () {
setModal(true);
setTitle("Please enter something");
JPanel content = new JPanel();
content.setLayout(new BorderLayout(10, 10));
getContentPane().add(content);
text = new JTextField();
JButton ok = new JButton("Accept");
JButton cancel = new JButton("Cancel");
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
buttons.add(ok);
buttons.add(cancel);
content.add(text, BorderLayout.NORTH);
content.add(buttons, BorderLayout.SOUTH);
content.setBorder(new EmptyBorder(15, 15, 15, 15));
pack();
ok.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
cancelled = false;
dispose();
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent e) {
cancelled = true;
dispose();
}
});
// default button, allows to trigger ok when pressing enter in the text field
getRootPane().setDefaultButton(ok);
}
/**
* Open the dialog (modal, blocks caller until dialog is disposed) and returns the entered value, or null if
* cancelled.
*/
public String selectValue () {
setVisible(true);
return cancelled ? null : text.getText();
}
}

Peter Walser
- 15,208
- 4
- 51
- 78
-
-Thank you very much for your quick reply. this situation is the only example. my real requirement is get value to 1st jrame from jtable(locate in the 2nd jframe). by using JDialog, Can I succeed this requirement ? – kasuntec Aug 19 '13 at 16:53