There are two JFrame. On one is TextField1, on the other - TextField2 and Button1. Question: how to make so that after clicking on the button text from TextField2 went to TextField1?

  • one
    take it from the first and put in the second? how did you do? where is the code? - Gorets
  • That's exactly what I can not imagine how this can be done. - Sergey1991

2 answers 2

Frame1.java

import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JTextField; public class Frame1 extends JFrame{ private JTextField textField1 = new JTextField(20); public Frame1() { setLayout(new FlowLayout()); add(textField1); } public JTextField getTextField() { return textField1; } } 

Frame2.java

 import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class Frame2 extends JFrame { private JTextField textField2 = new JTextField(20); private JButton button = new JButton("OK"); public Frame2(final JTextField textField) { setLayout(new FlowLayout()); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { textField.setText(textField2.getText()); textField2.setText(null); } }); add(textField2); add(button); } } 

Main.java

 public class Main { public static void main(String[] args) { Frame1 frame1 = new Frame1(); frame1.pack(); frame1.setLocationRelativeTo(null); frame1.setVisible(true); Frame2 frame2 = new Frame2(frame1.getTextField()); frame2.pack(); frame2.setLocationRelativeTo(null); frame2.setVisible(true); } } 

    Example of 2 windows based on one class:

     import java.awt.GridLayout ; import java.awt.event.ActionEvent ; import javax.swing.AbstractAction ; import javax.swing.Action ; import javax.swing.JButton ; import javax.swing.JDialog ; import javax.swing.JTextField ; //model interface interface IHaveText { public void setText ( String text ) ; public String getText () ; } // view & controller + model implementation public class CustomFrame extends JDialog implements IHaveText { private JTextField textField = new JTextField () ; // main window public CustomFrame () { super () ; // do common init init ( "Parent window" ) ; // do custom init // create actions Action showChild = new AbstractAction ( "Show child" ) { @ Override public void actionPerformed ( ActionEvent e ) { new CustomFrame ( CustomFrame.this ).setVisible ( true ) ; } } ; // add buttons add ( new JButton ( showChild ) ) ; } // child window public CustomFrame ( final CustomFrame parent ) { super ( parent, true ) ; // do common init init ( "Child window" ) ; // do custom init // create actions Action fromParent = new AbstractAction ( "Copy from parent" ) { @ Override public void actionPerformed ( ActionEvent e ) { setValueFrom ( CustomFrame.this, parent ) ; } } ; Action toParent = new AbstractAction ( "Copy to parent" ) { @ Override public void actionPerformed ( ActionEvent e ) { setValueFrom ( parent, CustomFrame.this ) ; } } ; // add buttons add ( new JButton ( fromParent ) ) ; add ( new JButton ( toParent ) ) ; } private void init ( String title ) { setSize ( 300, 300 ) ; setText ( title ) ; setTitle ( title ) ; setLayout ( new GridLayout ( 5, 1 ) ) ; setLocationByPlatform ( true ) ; setDefaultCloseOperation ( DISPOSE_ON_CLOSE ) ; add ( textField ) ; } public static void setValueFrom ( IHaveText dest, IHaveText source ) { if ( null != dest && null != source ) { dest.setText ( source.getText () ) ; } } public static void main ( String[] args ) { new CustomFrame ().setVisible ( true ) ; } @ Override public void setText ( String text ) { textField.setText ( text ) ; } @ Override public String getText () { return textField.getText () ; } } 

    ps what the interface is for and what tasks it performs - you have to figure it out yourself :)

    • we will understand) - Sergey1991