1

I have a button on frame1, clicking on it opens frame2 which has a button and a label. When I click button on frame2, the JLabel text is not changed. I want to change the JLabel text on frame2 when the button present on frame2 is clicked.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Gui{
    JFrame frame1, frame2;
    JPanel panel1, panel2;
    JButton firstButton, secondButton;
    JLabel prompt;

    public static void main(String[] args){
        Gui myGui = new Gui();
        myGui.ui1();
    }

    public void ui1(){
        frame1 = new JFrame();
        frame1.setSize(500,500);
        frame1.setTitle("First Frame");

        panel1 = new JPanel();
        firstButton = new JButton("click me");

        frame1.add(panel1);
        panel1.add(firstButton);

        ListenForButtonOnUi1 buttonListener = new ListenForButtonOnUi1();
        firstButton.addActionListener(buttonListener);

        frame1.setVisible(true);

    }

    public void ui2(){
        frame2 = new JFrame();
        frame2.setSize(300,300);
        frame2.setTitle("hell yeah");

        panel2 = new JPanel();
        secondButton = new JButton("done");

        prompt = new JLabel("please fill in the details");
        frame2.add(panel2);
        panel2.add(prompt)
        panel2.add(secondButton);
        frame2.setVisible(true);

        ListenForButtonOnUi2 buttonListener = new ListenForButtonOnUi2();
        secondButton.addActionListener(buttonListener);


    }

    private class ListenForButtonOnUi1 implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == firstButton){
                Gui myGui = new Gui();
                myGui.ui2();
            }
        }
    }

    private class ListenForButtonOnUi2 implements ActionListener{
        public void actionPerforme(ActionEvent e){
            if(e.getSource() == secondButton){
                prompt.setText("text changed");

            }
        }
    }
}
99darshan
  • 562
  • 1
  • 8
  • 17
  • 2
    Try `public void actionPerforme(ActionEvent e)` → `public void actionPerformed(ActionEvent e)` – Spikatrix Nov 08 '15 at 07:09
  • ah, It was a typo, spent a whole hour trying to figure out what went wrong. Thank you, it works now. – 99darshan Nov 08 '15 at 07:19
  • 2
    @99everest Tip: Next time put an `@Override` annotation before you're trying to override an existing method, it will throw an error if the method doesn't even exist in the, in your case, `ActionListener` interface. – Lukas Rotter Nov 08 '15 at 07:26
  • 1
    Also try checking out the [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Using multiple JFrame's is frowned upon. Or check out [JOptionPane's](http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html) – Jonah Nov 08 '15 at 07:29
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Nov 08 '15 at 07:30
  • 1
    You should set your JFrames [defaultCloseOperation](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29) as well. Your frames aren't getting disposed of. – Jonah Nov 08 '15 at 07:36

0 Answers0