Java Program to Create a Vertical Scrollbar with Scroll Button Length 30px

This is a Java program to create a vertical scrollbar with a scroll button length 30px.

Problem Description

We have to write a program in Java such that it creates a vertical scrollbar whose starting and ending positions are 0px and 400px with scroll button length 30px.

Expected Input and Output

For creating a vertical scrollbar, we can have the following set of input and output.

To Create a Vertical Scrollbar :
At the execution of the program, it is expected that the frame consists of a vertical scrollbar with the given parameters (0px, 400px) with scroll button length 30px.

Problem Solution

1. Create a frame.
2. Create a scrollbar with vertical orientation and scroll size 30px.
3. Set the minimum and maximum value of scroll bar to 0 and 400 respectively.
4. Add the scrollbar to frame, and display the frame.

Program/Source Code

Here is source code of the Java Program to create a vertical scrollbar. The program is successfully compiled and tested using javac compiler on Fedora 30.

advertisement
advertisement
  1. /*Java Program to create a vertical scrollbar*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. class Vertical_ScrollBar
  5. {
  6. 	//Driver function
  7. 	public static void main(String args[])
  8. 	{
  9. 		//Create a frame
  10. 		JFrame frame=new JFrame("Vertical Scrollbar");
  11. 		frame.setSize(500,500);
  12. 		frame.setLayout(null);
  13. 		frame.getContentPane().setBackground(Color.white);
  14. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 		//Create a Scrollbar
  16. 		Scrollbar scroll=new Scrollbar();
  17. 		scroll.setOrientation(Scrollbar.VERTICAL);
  18. 		scroll.setBounds(225,0,50,400);
  19. 		scroll.setMaximum(400);
  20. 		scroll.setMinimum(0);
  21. 		//Set size of scroll
  22. 		scroll.setVisibleAmount(30);
  23. 		//Add scrollbar to frame
  24. 		frame.add(scroll);
  25. 		//Display the frame
  26. 		frame.setVisible(true);
  27. 	}
  28. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(500,500); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create a scrollbar and add it to the frame by using frame.add();.
Scrollbar scroll=new Scrollbar(); –> Used to create the scrollbar.
4. scroll.setOrientation(Scrollbar.VERTICAL); –> setOrientation is used to set the orientation of the scrollbar to vertical or horizontal. The orientation of the scrollbar used in this program is vertical.
5. scroll.setBounds(225,0,50,400); –> Sets the position of scrollbar.
6. scroll.setMaximum(400); –> setMaximum is used to set the maximum value of the scrollbar. The maximum value the scrollbar in this program is 400px.
7. scroll.setMinimum(0); –> setMinimum is used to set the minimum value of the scrollbar. The minimum value the scrollbar in this program is 0px.
8. scroll.setVisibleAmount(30); –> setVisibleAmount is used to set the size of the scroll. The size of the scroll in this program is 30px.
9. frame.add(scroll); –> It is used to add the scrollbar to the frame.
10. frame.setVisible(true); is used to display the frame.

Runtime Test Cases

Here’s the run time test case for creating a vertical scrollbar.

Test case – Here’s the runtime output to view the scrollbar. Once the program is executed, it displays the frame which consists of a vertical scrollbar with the given parameters (0px, 400px) with scroll button length 30px.

java-program-vertical-scrollbar

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.