I'm new to Java and while trying to add a text area to the applet, found the textarea uneditable, and while trying to add the scrollbar to the textarea, found the applet will not be started. Code as below:

import java.awt.*;
import javax.swing.*;
import java.applet.Applet;

public class AppletEditor extends Applet{
    private JTextArea editor;
    private JScrollPane pane;
	
    public void init(){
		
        resize(500,200);
        setLayout(new BorderLayout());
		
        editor = new JTextArea(300,200);
        pane = new JScrollPane(editor, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED, 		JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //pane = new JScrollPane(editor);
        editor.setEditable(true);
        editor.setBackground(Color.cyan);

        Panel p1 = new Panel();
        p1.add(pane);
        add("North", p1);

    }

}

Dani AI

Generated

Short answer: there were two separate classes of problems in the thread. As discovered, using an AWT Panel or a JPanel without a proper layout can leave the scroll pane with no usable size; that alone explains why the textarea "disappeared" when wrapped. Two additional pitfalls often trip people here and are worth calling out.

  • Swing vs AWT and the EDT: build Swing UIs on the Event Dispatch Thread (use SwingUtilities.invokeAndWait/ invokeLater) and prefer subclassing javax.swing.JApplet (or better, use a JFrame for standalone testing). Mixing heavy AWT components (java.awt.Panel) with Swing components can cause painting/layout oddities—use Swing containers (JPanel) consistently.
  • Component sizing and constructors: new JTextArea(int,int) takes rows and columns, not pixels — new JTextArea(300,200) creates a huge preferred size. Pick realistic rows/columns (e.g. 10,40) or call setPreferredSize(...) on the scroll pane.
  • JScrollPane constructor order and editable state: the constructor is (component, VERTICAL_POLICY, HORIZONTAL_POLICY). Use setEditable(true) to allow typing; setEnabled(false) will also gray out and disable interactions.

Minimal pattern to follow (build UI on the EDT, use Swing containers, give the scroller a sensible preferred size):

/* build on EDT, use JApplet or a JFrame when debugging */
SwingUtilities.invokeLater(() -> {
    JTextArea ta = new JTextArea();
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    ta.setEditable(true);

    JScrollPane sp = new JScrollPane(ta,
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    sp.setPreferredSize(new Dimension(460,160));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(sp, BorderLayout.CENTER);
    getContentPane().add(panel, BorderLayout.CENTER);
});

Troubleshooting checklist: run under appletviewer or a simple JFrame to see stack traces, open the Java console for exceptions, avoid add("North", ...) string overloads (use BorderLayout constants), and if the UI still behaves oddly check for mixed AWT/Swing use or oversized preferred sizes.

Recommended Answers

All 9 Replies

The problem is in the last few lines I think. When I look at your code, you are trying to make a panel. Use a JPanel instead, if you want to use a panel.
I think you don't need any panel, just add the JTextArea to your applet straight away, using add(pane, BorderLayout.Center); (or wherever you want the text area).

Try this, and please reply if you run into any problems.

Yes, it's working if I don't put the textarea inside a panel.

But I have to use panel since I need to arrange other buttons and textfields, and I tried to use JPanel, still not working.

Any suggestions?

Great that part of it is working.

What exactly is not working? Try stuff, and let Eclipse (I hope you are using Eclipse) generate errors. Errors help, and describe the problem. If that does not work and you cannot exactly describe it yourself , post some code. I wont do your homework, but I'll help.

Please give a +1 to any post you like, or that helped you!

Yes, I'm using Eclipse, but it showed no errors.

Could you post all your code you have now? I will check for errors, because I can't do that now since you changed things since your first code sample.

I am getting two errors on the code you posted in the beginning:
- The Horizontal en Vertical properties of the scrollbars were swapped. Vertical first, then horizontal.
- I changed the Panel to JPanel
- I deleted all the enabled and editable lines.

My code that works:

import java.awt.*;
import javax.swing.*;
import java.applet.Applet;

public class Test extends Applet {
	private JTextArea editor;
	private JScrollPane pane;

	public void init() {

		resize (500, 200);
		setLayout (new BorderLayout ());

		editor = new JTextArea (300, 200);
		pane = new JScrollPane (editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		//pane.setEnabled (true);
		//pane.setBackground (Color.cyan);
		editor.setEnabled (true);

		JPanel p1 = new JPanel ();
		p1.add (pane);
		add (editor, BorderLayout.NORTH);

	}

}

I am getting two errors on the code you posted in the beginning:
- The Horizontal en Vertical properties of the scrollbars were swapped. Vertical first, then horizontal.
- I changed the Panel to JPanel
- I deleted all the enabled and editable lines.

My code that works:

import java.awt.*;
import javax.swing.*;
import java.applet.Applet;

public class Test extends Applet {
	private JTextArea editor;
	private JScrollPane pane;

	public void init() {

		resize (500, 200);
		setLayout (new BorderLayout ());

		editor = new JTextArea (300, 200);
		pane = new JScrollPane (editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		//pane.setEnabled (true);
		//pane.setBackground (Color.cyan);
		editor.setEnabled (true);

		JPanel p1 = new JPanel ();
		p1.add (pane);
		add (editor, BorderLayout.NORTH);

	}

}

In line 22, if you change to add(p1, BorderLayout.NORTH); then it's not working. I want to add the pane into the applet, not to add the textarea directly into the applet.

Could you post all your code you have now? I will check for errors, because I can't do that now since you changed things since your first code sample.

Here is all the code:

import java.awt.*;
import javax.swing.*;
import java.applet.Applet;

public class TestApplet extends Applet{
    private JTextArea editor;
    private JScrollPane pane;
	
    public void init(){
		
        resize(500,200);
        setLayout(new BorderLayout());
		
        editor = new JTextArea(300,200);
        pane = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
        		JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        //pane = new JScrollPane(editor);
        editor.setEnabled(true);
        editor.setBackground(Color.cyan);

        JPanel p1 = new JPanel();
        p1.add(pane);
        add(p1, BorderLayout.NORTH);

    }

}

Thanks for the code: I found it!

The problem was here:

JPanel p1 = new JPanel();
	        p1.add(pane);
	        add(p1, BorderLayout.NORTH);

Add a LayoutManager to the Panel, otherwise it will not know where to put the JTextArea.
Try like this:

JPanel p1 = new JPanel(new BorderLayout());
        p1.add(pane, BorderLayout.CENTER);
        add(p1, BorderLayout.NORTH);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.