Frigyes Vass
Frigyes Vass

Reputation: 311

JAVA Swing: Can't append a text to a TextArea

I simply try to append a text to a text area if I press a button. I separated it, so the textArea has made in a different class named TextPanel. The interesting part is that I also print out the text to the console if I press that button and it works on the console, just can't append it to my textArea... Here is my code:

Toolbar.java

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Toolbar extends JPanel implements ActionListener{

    private JButton helloButton;
    private JButton goodbyeButton;
    private TextPanel textPanel;

    public Toolbar() {

        helloButton = new JButton("Hello!");
        goodbyeButton = new JButton("Goodbye!");
        textPanel = new TextPanel();

        helloButton.addActionListener(this);
        goodbyeButton.addActionListener(this);

        setLayout(new FlowLayout(FlowLayout.LEFT));

        add(helloButton);
        add(goodbyeButton);
    }

    public void actionPerformed(ActionEvent e) {

        JButton clicked = (JButton)e.getSource();

        if (clicked == helloButton) {
            textPanel.appendText("Hello!\n");
            System.out.println("Hello!\n");
        }
        else if (clicked == goodbyeButton) {
            textPanel.appendText("Goodbye!\n");
            System.out.println("Goodbye!\n");
        }
    }
    /*
    public void setTextPanel(TextPanel textPanel) {
        this.textPanel = textPanel;
    }
    */
}


TextPanel.java

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TextPanel extends JPanel{

    private JTextArea textArea;

    public TextPanel() {

        textArea = new JTextArea();
        setLayout(new BorderLayout());
        add(new JScrollPane(textArea), BorderLayout.CENTER);
    }

    public void appendText(String text) {
        textArea.append(text);
    }
}

MainFrame.java

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JToolBar;

public class MainFrame extends JFrame{

    private JButton btn;
    private TextPanel textPanel;
    private Toolbar toolbar; 

    public MainFrame() {

        super("My First JAVA Swing Window");
        setLayout(new BorderLayout());

        textPanel = new TextPanel();
        toolbar = new Toolbar();

        add(textPanel, BorderLayout.CENTER);
        add(toolbar, BorderLayout.NORTH);

        setSize(600,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

}

App.java

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class App {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MainFrame();
            }
        });

    }

}

Upvotes: 1

Views: 811

Answers (2)

Butiri Dan
Butiri Dan

Reputation: 1772

Have you tried to set an initial size to the JTextArea?

You can try with setRows and setColumns

public TextPanel() {

    textArea = new JTextArea();

    textArea.setRows(10);
    textArea.setColumns(10);

    setLayout(new BorderLayout());
    add(new JScrollPane(textArea), BorderLayout.CENTER);
}

Upvotes: 0

tomdaly
tomdaly

Reputation: 485

You need to add the textPanel object with add(textPanel);


Extended answer due to the addition of the Main class code:.
Remove textPanel = new TextPanel() from your Toolbar class, uncomment the setTextPanel method and call toolbar.setTextPanel(textPanel) just after you create the toolbar with toolbar = new Toolbar().
The issue is that you're creating two instances of textPanel, and only modifying the one that you haven't added to the frame.

Upvotes: 2

Related Questions