How to display the vertical and horizontal scrollbars always even if it is not required

Java 8Object Oriented ProgrammingProgramming

<p>Use the following constants for the JScrollBar to display the vertical and horizontal scrollbars always even if it is not required &minus;</p><pre class="result notranslate">scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);</pre><p>The following is an example to display the vertical and horizontal scrollbars always even if it is not required &minus;</p><h2>Example</h2><pre class="prettyprint notranslate">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 { &nbsp; &nbsp;public static void main(String args[]) { &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame(&quot;Demo&quot;); &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); &nbsp; &nbsp; &nbsp; JButton button1 = new JButton(&quot;Questions and Answers&quot;); &nbsp; &nbsp; &nbsp; JButton button2 = new JButton(&quot;Videos&quot;); &nbsp; &nbsp; &nbsp; JButton button3 = new JButton(&quot;Tools&quot;); &nbsp; &nbsp; &nbsp; JButton button4 = new JButton(&quot;Tutorials&quot;); &nbsp; &nbsp; &nbsp; JButton button5 = new JButton(&quot;Online Compiler&quot;); &nbsp; &nbsp; &nbsp; JButton button6 = new JButton(&quot;Quiz&quot;); &nbsp; &nbsp; &nbsp; Box box = Box.createVerticalBox(); &nbsp; &nbsp; &nbsp; box.setPreferredSize(new Dimension(900,900)); &nbsp; &nbsp; &nbsp; box.add(button1); &nbsp; &nbsp; &nbsp; box.add(button2); &nbsp; &nbsp; &nbsp; box.add(button3); &nbsp; &nbsp; &nbsp; box.add(button4); &nbsp; &nbsp; &nbsp; box.add(button5); &nbsp; &nbsp; &nbsp; box.add(button6); &nbsp; &nbsp; &nbsp; JScrollPane scrollPane = new JScrollPane(); &nbsp; &nbsp; &nbsp; scrollPane.setViewportView(box); &nbsp; &nbsp; &nbsp; scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); &nbsp; &nbsp; &nbsp; scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); &nbsp; &nbsp; &nbsp; frame.add(scrollPane, BorderLayout.CENTER); &nbsp; &nbsp; &nbsp; frame.setSize(550, 250); &nbsp; &nbsp; &nbsp; frame.setVisible(true); &nbsp; &nbsp;} }</pre><h2>Output</h2><p><img src="https://www.tutorialspoint.com/assets/questions/media/23982/demo_tut.jpg" class="fr-fic fr-dib" style="width: 504px; height: 227.221px;" width="504" height="227.221"></p>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements