0

I'm designing a simple GUI ( Using WindowBuilder Pro in Eclipse) that just shows "Hello World" in the textArea after pressing the button (Testing).

https://i.stack.imgur.com/PbYo8.jpg

However, when I press the button, it doesn't show up in the text area! Can somebody adjust the code or at least tell me what to do?

public class TextA {

private JFrame frame;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TextA window = new TextA();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public TextA() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JTextArea textArea = new JTextArea();
    textArea.setBounds(113, 44, 226, 96);
    frame.getContentPane().add(textArea);

    JButton btnTesting = new JButton("Testing");
    btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            JTextArea textArea = new JTextArea();
            textArea.setText("Hello World!");


        }
    });
    btnTesting.setBounds(168, 167, 117, 29);
    frame.getContentPane().add(btnTesting);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mike
  • 380
  • 4
  • 19
  • 3
    You're creating a new `JTextArea` inside of your action listener, and setting the text on it. This isn't the same text area that you have added to your `JFrame`. – crush Jan 23 '14 at 18:59
  • 2
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jan 23 '14 at 19:04
  • Or, use JavaFX and you can make much richer, and more exact GUI's. – SnakeDoc Jan 23 '14 at 19:04

3 Answers3

3

Change your code to something like this.

 final JTextArea textArea = new JTextArea();
 frame.getContentPane().add(textArea);
 btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textArea.setText("Hello World!");
        }
    });

You are creating a new instance inside the actionListener you want to refer to the object you are adding into the frame. And as @AndrewThompson always advice not to use null layout cause :

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

nachokk
  • 14,363
  • 4
  • 24
  • 53
1

You are working on wrong JTextArea object. It should look like this:

final JTextArea textArea = new JTextArea(); // final added here
textArea.setBounds(113, 44, 226, 96);
frame.getContentPane().add(textArea);

JButton btnTesting = new JButton("Testing");
btnTesting.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            //JTextArea textArea = new JTextArea(); this should be removed.
            textArea.setText("Hello World!");


        }
    });
nervosol
  • 1,295
  • 3
  • 24
  • 46
0

You are creating a new JTextArea object in the actionPerformed(ActionEvent e). Just use the text area object you already defined and make it final because it's gonna be used in the action event method. You can delete the line JTextArea textArea = new JTextArea() in action event method and it should work.

Alex Goja
  • 548
  • 5
  • 18