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");
}
}
}
}