Dimitris Sfounis
Dimitris Sfounis

Reputation: 2500

JTextArea won't show up unless I call append() on it

The following problem boggled my mind, so I came here for some help.

After experimenting I ended up with this block of code:

JTextArea chatPanel = null;
JScrollPane scrollPanel = null;

if(chatPanel == null)
{
    chatPanel = new JTextArea("derp");
    chatPanel.setEditable(false);
    chatPanel.setForeground(new Color(10,191,26));
    chatPanel.setOpaque(false);
    scrollPanel = new JScrollPane(chatPanel);
    scrollPanel.setOpaque(false);
    scrollPanel.getViewport().setOpaque(false);
    scrollPanel.setBorder(BorderFactory.createEmptyBorder());
}

//## marked area ##         
scrollPanel.setBounds(9,596,435,138);
pane.add(scrollPanel);              

The result? Nothing shows up. The text area with "derp" in it I expected is not there, just an empty scroll panel. Now, if I go to the ## marked area ## and replace it with this:

chatPanel.append("Hello.");

the chatPanel shows up fine in the scrollPanel, with its text being "derpHello.". Any ideas as per what's going on?

For the record, pane is a simple container with a null layout that otherwise displays eveyrthing fine. Declaration, just for the record:

Container pane = getContentPane()
pane.setLayout(null);

Upvotes: 1

Views: 457

Answers (2)

Timmos
Timmos

Reputation: 3324

There is nothing in your JTextArea to display as you initialize it with the empty String. When you append something to it, there is effectively text inside.

Note that your JScrollPane will never try to resize your component inside (why else would it allow you to scroll?). Therefore, you will have to set a dimension on your JTextArea.

EDIT (after your code correction): I can see "derp" just fine with following code.

public class TextTest extends JFrame {
public TextTest () {
    JPanel pane = new JPanel();
    JTextArea chatPanel = null;
    JScrollPane scrollPanel = null;

    if(chatPanel == null)
    {
        chatPanel = new JTextArea("derp");
        chatPanel.setEditable(false);
        chatPanel.setForeground(new Color(10,191,26));
        chatPanel.setOpaque(false);
        scrollPanel = new JScrollPane(chatPanel);
        scrollPanel.setOpaque(false);
        scrollPanel.getViewport().setOpaque(false);
        scrollPanel.setBorder(BorderFactory.createEmptyBorder());
    }

    //## marked area ##         
    scrollPanel.setBounds(9,596,435,138);
    pane.add(scrollPanel);
    setContentPane(pane);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
    new TextTest();
}

}

Upvotes: 0

Guillaume Polet
Guillaume Polet

Reputation: 47627

I have no problem with the following code, I can see the "derp" in green just fine:

import java.awt.Color;
import java.awt.Container;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TestTextArea {

    private void initUI() {
        JFrame frame = new JFrame("test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextArea chatPanel = null;
        JScrollPane scrollPanel = null;
        Container pane = frame.getContentPane();
        pane.setLayout(null);
        if (chatPanel == null) {
            chatPanel = new JTextArea("derp");
            chatPanel.setEditable(false);
            chatPanel.setForeground(new Color(10, 191, 26));
            chatPanel.setOpaque(false);
            scrollPanel = new JScrollPane(chatPanel);
            scrollPanel.setOpaque(false);
            scrollPanel.getViewport().setOpaque(false);
            scrollPanel.setBorder(BorderFactory.createEmptyBorder());
        }

        // ## marked area ##
        scrollPanel.setBounds(9, 596, 435, 138);
        pane.add(scrollPanel);
        frame.validate();
        frame.setSize(600, 800);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestTextArea().initUI();
            }
        });
    }

}

Now, I would really advise you to use an appropriate LayoutManager instead of that null layout. That would allow you to use pack() and revalidate() and have a much simpler and more maintainable code.

There must be something else that your code does not illustrate for now. Try to put an SSCCE.

Upvotes: 2

Related Questions