0

I have got a little problem. I have created 2 JFrames, and in main one i would like to show a String name. In the other one I can provide my name. And when i save my 'name' to variable name it doesn't update in main frame. How to update this frame?

public class Main extends JFrame {

    IFrame frame = new IFrame ("Main");
    JButton button_name = new JButton("Enter your name");    
    IFrame frame_name = new IFrame ("What is your name?");
    Point center = 
    GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); //Get center of screen
    static Image Icon = Toolkit.getDefaultToolkit().getImage("Icon.png");
    ButtonListener button_listen = new ButtonListener();

    /**************************************************************************/
    String name = new String();
    JButton button_get_name = new JButton("OK");
    JTextField field_name = new JTextField(15);
    JPanel panel_name = new JPanel();
    /**************************************************************************/

    public Main(){
        this.setIconImage(Icon);

        button_name.addActionListener(button_listen); 
        button_get_name.addActionListener(button_listen);

        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(4,1));

        frame.add(new JLabel (name, SwingConstants.CENTER));
        frame.add(button_name);
        frame.add(new JLabel ("Table 2", SwingConstants.CENTER));
        frame.add(panel_name);

        frame.setBounds(center.x - 400 / 2, center.y - 400 / 2, 400, 400);      //Set window in a center of screen 
        frame.setVisible(true);         
        /**********************************************************************/
        panel_name.setBackground(Color.CYAN);
        /**********************************************************************/
    }

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main();
    }

    //Class responsible for detectin which button was pushed
    public class ButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == button_name){
                CreateFrameName();
            }
            if (e.getSource() == button_get_name){
                GetName();
            }
        }
    }

    //Creates a frame for name provide
    public void CreateFrameName() {
        int frame_name_width = 280;
        int frame_name_height = 100;
        frame.dispose();
        frame_name.setBounds(center.x - frame_name_width / 2, center.y - 
                frame_name_height / 2, frame_name_width, frame_name_height);
        frame_name.setLayout(new GridLayout(2,1));
        frame_name.add(field_name);
        frame_name.add(button_get_name);
        frame_name.setAlwaysOnTop(true);
        frame_name.setVisible(true);
    }
    public void GetName() {
        name = field_name.getText();
        frame_name.dispose();
        frame.setVisible(true);
        System.out.println(name);
    }
}
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
Marek
  • 1,189
  • 3
  • 13
  • 33
  • 2
    You need some kind of model which can be shared between the two frames, the first frame would set the value of the model and the model would alert the second that the state has changed, allowing it to update it's view – MadProgrammer Oct 16 '14 at 21:03
  • 1
    Consider taking a look at [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) as well – MadProgrammer Oct 16 '14 at 21:03
  • @MadProgrammer, i have already changed it into one jframe. Could you give me some tipa how to make this model? Greetings – Marek Oct 21 '14 at 09:23

0 Answers0