Make JScrollPane to always show scroll bar in Java

Description

The following code shows how to make JScrollPane to always show scroll bar.

Example


import java.awt.Dimension;
import java.awt.FlowLayout;
/*from ww  w.jav  a  2  s  . c o m*/
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("JTextArea Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
    JTextArea textAreal = new JTextArea("from java2s.com", 5, 10);
    textAreal.setPreferredSize(new Dimension(100, 100));
    
    JScrollPane scrollPane = new JScrollPane(textAreal, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);


    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
  }
}

The code above generates the following result.

Make JScrollPane to always show scroll bar in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer