This is a Java program to create a vertical scrollbar with a scroll button length 30px.
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.
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.
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.
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.
/*Java Program to create a vertical scrollbar*/
import javax.swing.*;
import java.awt.*;
class Vertical_ScrollBar
{
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame=new JFrame("Vertical Scrollbar");
frame.setSize(500,500);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.white);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create a Scrollbar
Scrollbar scroll=new Scrollbar();
scroll.setOrientation(Scrollbar.VERTICAL);
scroll.setBounds(225,0,50,400);
scroll.setMaximum(400);
scroll.setMinimum(0);
//Set size of scroll
scroll.setVisibleAmount(30);
//Add scrollbar to frame
frame.add(scroll);
//Display the frame
frame.setVisible(true);
}
}
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.
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.
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice BCA MCQs
- Practice Programming MCQs
- Apply for Java Internship
- Practice Information Technology MCQs