Disable only the vertical scrollbar in Java


To disable only the vertical scrollbar, use the VERTICAL_SCROLLBAR_NEVER constant, which displays only the horizontal scrollbar.

The following is an example to disable only the vertical scrollbar in Java −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class SwingDemo {
   public static void main(String args[]) {
      JFrame frame = new JFrame("Demo");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JButton button1 = new JButton("Online Compiler");
      JButton button2 = new JButton("Quiz");
      JButton button3 = new JButton("Questions and Answers");
      JButton button4 = new JButton("Videos");
      JButton button5 = new JButton("Tools");
      Box box = Box.createVerticalBox();
      box.setPreferredSize(new Dimension(900,900));
      box.add(button1);
      box.add(button2);
      box.add(button3);
      box.add(button4);
      box.add(button5);
      JScrollPane scrollPane = new JScrollPane();
      scrollPane.setViewportView(box);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
      frame.add(scrollPane, BorderLayout.CENTER);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

Output


Updated on: 30-Jul-2019

848 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements