Reputation: 185
I need some help adding a scroll bar to my JTextArea.
The code I'm working on now seems to add the scrollbar to the frame instead of the textArea. The bar is visible, but nothing happens when text go outside the bounds of the frame.
Code bellow, does not contain any of the event handling:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class Simplenotepad extends JFrame implements ActionListener {
// private TextArea textArea = new TextArea("", 0, 0,
// TextArea.SCROLLBARS_VERTICAL_ONLY);
private JTextArea textArea = new JTextArea(10, 15);
private JScrollPane scrollPane = new JScrollPane(textArea);
private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
private Font f = new Font("Verdana", Font.PLAIN, 10);
private Menu file = new Menu();
private MenuItem open = new MenuItem();
private MenuItem save = new MenuItem();
private MenuItem exit = new MenuItem();
private Menu format = new Menu();
private MenuItem wrap = new MenuItem();
private MenuItem noWrap = new MenuItem();
private Menu font = new Menu();
private MenuItem s10 = new MenuItem();
private MenuItem s12 = new MenuItem();
private MenuItem s14 = new MenuItem();
private MenuItem s16 = new MenuItem();
private MenuItem s18 = new MenuItem();
private MenuItem s20 = new MenuItem();
private Menu edit = new Menu();
private MenuItem clear = new MenuItem();
private Menu help = new Menu();
private MenuItem about = new MenuItem();
private String saveTest = "";
public Simplenotepad() {
this.setSize(250, 250);
this.textArea.setFont(f);
this.setTitle("A Simple Notepad");
this.textArea.setLineWrap(true);
this.scrollPane.add(textArea);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(textArea);
this.getContentPane().add(scrollPane, BorderLayout.EAST);
this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
}
I have removed most of the unecessary code, this should be the bare minimum needed to sort out my problem. If by chance you need the entire code: http://pastebin.com/v6D8PrsB
Thanks for any help.
Upvotes: 1
Views: 744
Reputation: 96
You need to add scrollable component to ViewportView:
public Simplenotepad() {
this.setSize(250, 250);
this.textArea.setFont(f);
this.setTitle("A Simple Notepad");
this.textArea.setLineWrap(true);
// this.scrollPane.add(textArea); //This not correct, add Component to Viewport View
this.scrollPane.setViewportView(textArea);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
//this.getContentPane().add(textArea);
this.getContentPane().add(scrollPane, BorderLayout.CENTER);
this.scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setVisible(true);}
The JViewport provides a window, or "viewport" onto a data source -- for example, a text file. That data source is the "scrollable client" (aka data model) displayed by the JViewport view. A JScrollPane basically consists of JScrollBars, a JViewport, and the wiring between them, as shown in the diagram at right. JavaDoc-JScrollPane
Upvotes: 2
Reputation: 49
You are adding the text area to the to the JFrame. Just add the scrollPane.
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(scrollPane, BorderLayout.EAST);
Also, try
textArea.setPreferredSize(new Dimension(x,y)); OR
this.setPreferredSize(new Dimension(x,y));
//whichever you are trying to control the size of //x and y should be the width and height about the size you think it should be
for examples check: https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
Upvotes: -1