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);

    }

}

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.