9 Replies - 421 Views - Last Post: 11 November 2019 - 10:58 AM Rate Topic: -----

#1 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 09 November 2019 - 05:46 PM

When I type a big text in a JTextArea the scrollbar does not appear. Why and how to fix this? I'm using a JPanel because I want to use a JBorder.

import java.awt.Dimension;
import java.awt.FlowLayout;

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

public class TestText extends JFrame {

	private JPanel panel;
	private JScrollPane scrollPane;
	private JTextArea textArea;

	public TestText() {
		createComponents();
		configComponents();
		placeComponents();
		configJFrame();
	}

	private void configComponents() {
		this.panel.setPreferredSize(new Dimension(280, 280));
		this.textArea.setPreferredSize(new Dimension(270, 270));
	}

	private void configJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());
		setLocation(200, 100);
		setSize(300, 300);
		setVisible(true);
	}

	private void createComponents() {
		this.panel = new JPanel();
		this.scrollPane = new JScrollPane();
		this.textArea = new JTextArea();
	}

	private void placeComponents() {
		this.scrollPane.add(this.textArea);
		this.panel.add(this.textArea);
		add(this.panel);
	}

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


Is This A Good Question/Topic? 0
  • +

Replies To: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 796
  • View blog
  • Posts: 6,040
  • Joined: 25-December 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 06:09 AM

One problem I see is that the text area should be passed to the scrollpane's constructor and not via the add method.
Was This Post Helpful? 1
  • +
  • -

#3 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 06:27 AM

View PostNormR, on 10 November 2019 - 06:09 AM, said:

One problem I see is that the text area should be passed to the scrollpane's constructor and not via the add method.


I changed that but didn't solve the problem.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3643
  • View blog
  • Posts: 16,753
  • Joined: 20-September 08

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 09:04 AM

If you don't setLineWrap(true) on the text area it might not scroll as it might not need to
Was This Post Helpful? 1
  • +
  • -

#5 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 796
  • View blog
  • Posts: 6,040
  • Joined: 25-December 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 09:34 AM

Another problem is the scrollPane is never added to any GUI.
Was This Post Helpful? 1
  • +
  • -

#6 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 01:19 PM

View Postg00se, on 10 November 2019 - 09:04 AM, said:

If you don't setLineWrap(true) on the text area it might not scroll as it might not need to

I definitely want that behavior. But still not scrolling

Quote

Another problem is the scrollPane is never added to any GUI.

Here what I tried.
	private void placeComponents() {
		this.scrollPane.add(this.textArea);
		this.panel.add(this.scrollPane);
		add(this.panel);
	}


But now I can't see the textarea.
Was This Post Helpful? 0
  • +
  • -

#7 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 796
  • View blog
  • Posts: 6,040
  • Joined: 25-December 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 01:29 PM

I thought you got rid of the call to the scroll pane's add method (line 2). The text area should be passed to the constructor.

For a test, get rid of panel. Add the scroll pane directly to the JFrame.

This post has been edited by NormR: 10 November 2019 - 01:31 PM

Was This Post Helpful? 1
  • +
  • -

#8 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 193
  • Joined: 15-January 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 04:47 PM

Here the entire code again. Still can't see scrollbar with a big text inside textarea even if I add scrollpane instead of textarea.

import java.awt.Dimension;
import java.awt.FlowLayout;

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

public class TestText extends JFrame {

	private JScrollPane scrollPane;
	private JTextArea textArea;

	public TestText() {
		createComponents();
		configComponents();
		placeComponents();
		configJFrame();
	}

	private void configComponents() {
		this.textArea.setLineWrap(true);
		this.textArea.setPreferredSize(new Dimension(270, 270));
	}

	private void configJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new FlowLayout());
		setLocation(200, 100);
		setSize(300, 300);
		setVisible(true);
	}

	private void createComponents() {
		this.textArea = new JTextArea();
		this.scrollPane = new JScrollPane(this.textArea);
	}

	private void placeComponents() {
		add(this.textArea);
	}

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

This post has been edited by robgeek: 10 November 2019 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#9 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 796
  • View blog
  • Posts: 6,040
  • Joined: 25-December 13

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 10 November 2019 - 05:07 PM

The code does not add the scroll pane to the GUI.
Was This Post Helpful? 1
  • +
  • -

#10 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3643
  • View blog
  • Posts: 16,753
  • Joined: 20-September 08

Re: JScrollPane: Scrollbar not pop up with a big text in a JTextArea.

Posted 11 November 2019 - 10:58 AM

Your fiddling about with the layout has stopped the program calculating the scrolling properly. See the below instead. If you want that border effect then use a ... Border ;)

import java.awt.Dimension;
import java.awt.FlowLayout;

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

public class TestText extends JFrame {

    private JScrollPane scrollPane;
    private JTextArea textArea;

    public TestText() {
        createComponents();
        configComponents();
        placeComponents();
        configJFrame();
    }

    private void configComponents() {
        this.textArea.setLineWrap(true);
        //this.textArea.setPreferredSize(new Dimension(270, 270));
    }

    private void configJFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setLayout(new FlowLayout());
        setLocation(200, 100);
        setSize(300, 300);
        setVisible(true);
    }

    private void createComponents() {
        this.textArea = new JTextArea();
        this.scrollPane = new JScrollPane(this.textArea);
    }

    private void placeComponents() {
        //add(this.textArea);
        add(scrollPane);
    }

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


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1