JScrollPane Component in Java

This is a tutorial in which we will going to create a program that will have a JScrollPaneComponent in Java. A JScrollPane implements scrollable view of a component that lets the user display a component that is large or one whose size can change using scrollbars. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jScrollPaneComponent.java. 2. Import the following packages:
  1. import javax.swing.*; // used to access the JFrame, JPanel, JLabel, and JScrollPane class
  2. import java.awt.*; // used to access the Font class
3. Initialize your variable in your Main, variable frame for JFrame, variable pane for JPanel, variable scroll for JScrollPane, label for JLabel, and variable txtFont for Font.
  1. JFrame frame = new JFrame("JScrollPane Component");
  2. JPanel panel = new JPanel();
  3. JScrollPane scroll = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  4. JLabel label = new JLabel("This is a JScrollPane Component");
  5. Font txtFont = new Font("Tahoma", Font.BOLD, 38);
As you have seen the code above, we have put the panel inside the scrollpane. The ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS code will trigger to always show the vertical scrollbar of the panel. The ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS code will trigger to always show the horizontal scrollbar of the panel. We have also created the Font class that has the Tahoma as font, Bold as font style, and 38 as font size. Now, we will add the label to the panel using the add method and the font of the label using the setFont method. Have this code below:
  1. panel.add(label);
  2. label.setFont(txtFont);
4. Now, add the JScrollPane variable to the frame using the setContentPane().add method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.setContentPane(scroll);
  2. frame.setSize(300, 100);
  3. frame.setVisible(true);
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access the JFrame, JPanel, JLabel, and JScrollPane class
  2. import java.awt.*; // used to access the Font class
  3.  
  4. public class jScrollPaneComponent {
  5.  
  6. public static void main(String[] args) {
  7. JFrame frame = new JFrame("JScrollPane Component");
  8. JPanel panel = new JPanel();
  9. JScrollPane scroll = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
  10. JLabel label = new JLabel("This is a JScrollPane Component");
  11. Font txtFont = new Font("Tahoma", Font.BOLD, 38);
  12.  
  13.  
  14. panel.add(label);
  15. label.setFont(txtFont);EXIT_ON_CLOSE
  16. frame.setContentPane(scroll);
  17. frame.setSize(300, 100);
  18. frame.setVisible(true);
  19. frame.setDefaultCloseOperation(JFrame.);
  20. }
  21. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment