Manage JFrame Instances

In my project I've one instance of jframe1 and two instance of jframe2. Then I want to update from jframe1 txt2 component of first instance of jframe2. But when I invoke perfomaction() method it was to update the second instance of jframe2.

public class Jframe1 extends Jframe {
    public jframe1() {
    Performedaction() {
       jframe2.txt2.setText("do it");
    }
}

public class jframe2 extends Jframe {
    public static JtextFiedl txt2;
    public jframe2() {
}

1 answer

  • answered 2018-11-08 12:46 prasad_

    Here is an example.. The example uses two JFrame window's and on clicking a button in one jframe, the second one's JLabel is updated. The example uses a JLabel instead of JTextField.

    The mechanism uses java.util.Observer interface and Observable class to update from one window to the other one.

    The example's code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class TwoFramesExample {
        public static void main(String [] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TwoFramesExample().start();
                }
            });
        }
        private void start() {
            Frame1 f1 = new Frame1();
            new Frame2(f1);
        }
    }
    
    class Frame1 implements Observer {
        private JLabel label;
        @Override // Observer interface's implemented method
        public void update(Observable o, Object data) { 
            label.setText((String) data); // displays new text in JLabel
        }
        Frame1() {  
            JFrame f1 = new JFrame("Frame-1");
            f1.getRootPane().setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));        
            label = new JLabel("Click button in frame-2...");
            label.setFont(new Font("Dialog", Font.PLAIN, 20));  
            f1.add(label);
            f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f1.setSize(350, 150);
            f1.setLocation(200, 200);
            f1.setVisible(true);
        }
    }
    
    class Frame2 {
        private int clicks;
        Frame2(Frame1 f1) {
            // Create Observable and add Observer       
            final MessageObservable observable = new MessageObservable();
            observable.addObserver(f1);
            // Display frame
            JFrame f2 = new JFrame("Frame-2");      
            JButton button = new JButton("Press me");
            button.setFont(new Font("Dialog", Font.PLAIN, 20));
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String data = "button clicks in frame-2: [" + ++clicks + "]";
                    observable.changeData(data);
                }
            });     
            f2.add(button);
            f2.setSize(250, 150);
            f2.setLocation(600, 200);
            f2.setVisible(true);
        }
    }
    
    class MessageObservable extends Observable {
        MessageObservable() {   
            super();
        }
        void changeData(Object data) {
            // the two methods of Observable class
            setChanged(); 
            notifyObservers(data);
        }
    }