I will try and illustrate my question with examples, I am attempting to create a Java program that will (eventually) incorporate a complex Swing GUI.
I have Main.java
public class Main extends JFrame implements ActionListener {
JTextArea example;
public Main()
{
//... Missing, basic swing code
example = new JTextArea();
//... example added to jpanel, jpanel added to jframe, jframe set visible etc.
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equalsIgnoreCase("Do Something!"))
{
new DoSomething();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Main();
}
});
}
}
So now, I want my DoSomething() class to update my example JTextArea, what is the best way to do this?
I can pass a reference to example to DoSomething(), so DoSomething(example), but that doesn't seem nice. I could also pass "this" to DoSomething() and implement a updateExample(String newString) method in Main but that doesn't seem great either.
Basically, what is the best way to achieve what I want to do? The program I am writing will ultimately get much more complicated than this and I can't see a way that will allow me to do this without it getting too messy.