I have an application creating a JPanel within a JFrame as follows:
public class Frame1{
public Frame1(){
Frame2 f2 = new Frame2();
f2.pack();
f2.setVisible(true);
}
class Frame2 extends JFrame{
public Frame2(){
JPanel p1 = new JPanel();
JTextField txt1 = new JTextField("Test",12);
p1.add(txt1);
JButton btn1 = new JButton("Click Me!");
p1.add(btn1);
add(p1);
btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
JOptionPane.showDialog(null,text of the textbox);
}
}
}
Is there a way to reference the text field even though it is not a field variable and it is just declared inside the constructor (this.getRootPane().getParent()
will give me the frame I believe, but where do I go from there)?