I'm new to Java and I'm working on my first GUI using the WindowBuilder plugin for Eclipse Mars.
In "private void initialize" it creates my objects: a TextArea and a JButton.
I want to be able to manipulate my TextArea in a method, but I can't simply type something like: txtOutput.setText("test");
(I want my JButton to call a method and let the method do all the work because I also have a menu item that's going to do the same thing, so it makes sense to put my code into a method and have the menu item and the button call the method. My actionPerformed event handlers are working and they call the method, but the method can't "see" the txtOutput.) I'm guessing some keyword needs to be in front of txtOutput.
What do I need to put in front of txtOutput?
Sorry for being such a newb. I hope I've explained this well. Thank you for any help you can offer.
Here is a copy of my code:
package Junkpad;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.TextArea;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GUITest {
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUITest window = new GUITest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GUITest() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("My First GUI");
TextArea txtOutput = new TextArea();
txtOutput.setBounds(0, 20, 442, 197);
frame.getContentPane().add(txtOutput);
JButton btnChangeText = new JButton("Change Text");
btnChangeText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actEvent) {
Object srcSource = actEvent.getSource();
if (srcSource == btnChangeText) {
ChangeText();
}
}
});
btnChangeText.setBounds(247, 223, 180, 29);
frame.getContentPane().add(btnChangeText);
JMenuBar menuBar = new JMenuBar();
menuBar.setBounds(0, 0, 442, 27);
frame.getContentPane().add(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmChangeText = new JMenuItem("Change Text");
mntmChangeText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actEvent) {
Object srcSource = actEvent.getSource();
if (srcSource == mntmChangeText) {
ChangeText();
}
}
});
mnFile.add(mntmChangeText);
// center frame on screen
Dimension dimDimension = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dimDimension.width-frame.getSize().width)/2, (dimDimension.height-frame.getSize().height)/2);
}
private static void ChangeText(){
// here is where I want to change the text of txtOutput
// none of these attempts work:
//txtOutput.setText("testing");
//GUITest.txtOutput.setText("testing");
//initialize.txtOutput.setText("testing");
//this.txtOutput.setText("testing");
// the calls from the actionPerformed event handlers are working
// because this code runs when I click the button or the menu item
System.out.println("ChangeText was called");
}
}