Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to a add a vertical scroll bar to a JTextArea in Swing?

I have this Gui class:

public class Gui extends JFrame implements ActionListener {

/**
 * 
 */
private static final long serialVersionUID = -384241835772507459L;
JLabel playerInfo;
JTextField textField;
private final static String newline = "\n";
JTextArea feed;
JScrollPane scrollPane;
Player player;

public Gui() {
    super("Erik's RPG");        
    setLayout(new FlowLayout());        
    textField = new JTextField(30);     
    textField.addActionListener(this);      
    feed = new JTextArea(15, 30);
    feed.setEditable(false);    
}

public void setCurrentPlayer(Player currentPlayer) {
    player = currentPlayer;
    playerInfo = new JLabel("Health = " + currentPlayer.getHealth() + " | Mana = " + player.getMana());
    playerInfo.setBorder(BorderFactory.createTitledBorder(currentPlayer.getName()));
    add(playerInfo);
    add(feed);
    add(textField);
}

And it is annoying when a bunch of text is in the text field because it keeps making the window larger. I need to know how to make a scrollbar so the user doesn't have to keep resizing his JFrame. Also, how can I lock the dimensions of a JFrame so users can adjest the size?

like image 460
Erik Balen Avatar asked Sep 15 '11 05:09

Erik Balen


People also ask

How do I add a vertical Scrollbar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do you add a vertical Scrollbar in flutter?

By default, scrollable widgets in Flutter don't show a scrollbar. To add a scrollbar to a ScrollView, wrap the scroll view widget in a Scrollbar widget. Using this widget we can scroll a widget. The scrollbar allows the user to jump into the particular points and also shows how far the user reaches.

How do I add a Scrollbar in Visual Studio?

Open the Scroll Bars options page by choosing Tools > Options > Text Editor > All Languages > Scroll Bars.


1 Answers

Set the JTextArea instance as the viewport view of a JScrollPane instance, as shown in How to Use Scroll Panes. Add this scroll pane to the frame's content pane.

like image 122
Ashwini Raman Avatar answered Nov 05 '22 10:11

Ashwini Raman