Search code examples
javaswingdesign-patternsframejcomponent

how to call compoment from another frame


hi i have two frames created in the same way:

public class DateFilter extends JFrame {

private final JDateChooser dateChooser = new JDateChooser();
private final JDateChooser dateChooser_1 = new JDateChooser();
private final JComboBox comboBox = new JComboBox();
private final JButton filtruotiButton = new JButton();

public DateFilter() {
    super();
    setBounds(100, 100, 277, 167);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try {
        jbInit();
    } catch (Throwable e) {
        e.printStackTrace();
    }

}
private void jbInit() throws Exception {
    getContentPane().setLayout(null);
    setTitle("Priemimo datos filtras");
    setResizable(false);

    getContentPane().add(dateChooser);
    dateChooser.setBounds(70, 40, 117, 20);

    getContentPane().add(dateChooser_1);
    dateChooser_1.setBounds(70, 65, 117, 20);
    dateChooser_1.setEnabled(false);
...

tell me plz someone how to get data from one to another frame for example if i want to place date from this frame's dateChooser to another frame's textField using button


Solution

  • You will need to pass the instance of the first frame to the second and than call a method on this instance.

    Little sample code:

    public class FrameA extends JFrame {
    
      public void setSomeDate() {
      }
    }
    
    public class FrameB extends JFrame {
    
      public void doSomething() {
        FrameA frameA = new FrameA();
        frameA.setSomeDate();
      }
    }
    

    Since I guessed that you are a beginner, I answered with this pretty simple and basic example. A more sophisticated way would be using the MVC pattern, of course!