Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:20:46+00:00 2026-06-11T15:20:46+00:00

So I use Java Swing to build the UI for my app and use

  • 0

So I use Java Swing to build the UI for my app and use custom images to replace the ugly Java ones, the custom images have a style and are very easy to integrate into Java Swing.

Right now my problem is I need to use a JScrollBar with a JScrollPane in an app and I really don’t want to use either the default Java Scroll Bar or even the native OS Scroll Bar.

I just want to be able to have a custom image be the Background of the scroll bar
and an image to be the Thumb of the scroll bar.

How to make a custom JScrollBar using an image?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:20:48+00:00Added an answer on June 11, 2026 at 3:20 pm

    I wrote an example that shows how to set a custom image for the thumb and background (called track) of a scrollbar. You need the two files thumb.png and track.png for the custom images in the same place where your class file is. I also do some scaling of the images to fit the scrollbar. Just experiment a little bit with this code. I changed the width of the scrollbar (setPreferredSize) to better see the images too.

    The essential points are that you have to create your own class MyUi that extends BasicScrollBarUI and overrides paintThumb and paintTrack, and personalize your scrollbar with setUI(new MyUI):

    import java.awt.*;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.*;
    import javax.swing.plaf.metal.MetalScrollBarUI;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.awt.geom.AffineTransform;
    
    
    public class CustomScrollbarUIExample {
      public static void main(String[] args) {
        JScrollPane before = makeExamplePane();
        JScrollPane after = makeExamplePane();  
    
        JScrollBar sb=after.getVerticalScrollBar();
        sb.setPreferredSize(new Dimension(50, Integer.MAX_VALUE));
        sb.setUI(new MyScrollbarUI());
    
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        Container c = f.getContentPane();
        c.setLayout(new GridLayout(2, 1, 0, 1));
        c.add(before);
        c.add(after);
        f.setSize(450, 400);
        f.setVisible(true);
      }
    
      private static JScrollPane makeExamplePane() {
        String exampleText= "Lorem ipsum dolor sit amet,\n consetetur sadipscing elitr,\n sed diam nonumy eirmod \ntempor invidunt ut labore et dolore \nmagna aliquyam erat,\n sed diam voluptua. At vero eos et accusam et \njusto duo dolores et ea rebum. Stet clita\n kasd gubergren, no sea\n takimata sanctus est Lorem ipsum dolor sit amet.\n Lorem ipsum dolor sit amet,\n consetetur sadipscing elitr, sed diam\n nonumy eirmod tempor invidunt \nut labore et dolore\n magna aliquyam erat, sed diam voluptua.\n At vero eos et accusam et justo \nduo\n dolores et ea rebum. Stet clita kasd gubergren, no sea\n takimata sanctus est Lorem\n ipsum dolor sit amet. Lorem ipsum dolor\n sit amet, consetetur sadipscing elitr,\n sed diam nonumy\n eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos\n et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est \nLorem ipsum dolor sit amet.Duis\n autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel\n illum dolore eu feugiat nulla facilisis at vero eros et \naccumsan et iusto odio \ndignissim qui blandit praesent luptatum zzril delenit augue\n duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer\n adipiscing elit, sed diam nonummy nibh euismod \ntincidunt ut laoreet\n dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam,\n quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea\n commodo consequat. Duis autem vel eum iriure \ndolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla \nfacilisis at vero eros et accumsan et iusto odio dignissim qui blandit\n praesent luptatum zzril delenit augue duis dolore \nte feugait nulla facilisi.";
        JTextArea text = new JTextArea(exampleText);
        JScrollPane scroll = new JScrollPane(text);
        return scroll;
      }
    
      static class MyScrollbarUI extends MetalScrollBarUI {
        private Image imageThumb, imageTrack;
        MyScrollbarUI() {
            try {
                imageThumb = ImageIO.read(new File("thumb.png"));
                imageTrack = ImageIO.read(new File("track.png"));
            } catch (IOException e){}
        }
    
        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {        
            g.translate(thumbBounds.x, thumbBounds.y);
            g.setColor( Color.red );
            g.drawRect( 0, 0, thumbBounds.width - 2, thumbBounds.height - 1 );
            AffineTransform transform = AffineTransform.getScaleInstance((double)thumbBounds.width/imageThumb.getWidth(null),(double)thumbBounds.height/imageThumb.getHeight(null));
            ((Graphics2D)g).drawImage(imageThumb, transform, null);
            g.translate( -thumbBounds.x, -thumbBounds.y );
        }
    
        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {        
            g.translate(trackBounds.x, trackBounds.y);
            ((Graphics2D)g).drawImage(imageTrack,AffineTransform.getScaleInstance(1,(double)trackBounds.height/imageTrack.getHeight(null)),null);
            g.translate( -trackBounds.x, -trackBounds.y );
        }
    
      }
    }
    

    For more information consult the sourcefile MetalScrollBarUI.java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating one GUI in swing Java. I have to use one button
When I use Java applets, they tend to be slow, don't integrate very well
I am developing a Java desktop application and I use Swing to build the
I have a Java Swing application client that I want to use to consume
I wish to embed a very light HTTP server in my Java Swing app
I have a college project based on java swing. I wanted to use windowbuilder
I am developing a desktop app using Java and Swing Application Framework. I have
I have a Java Swing program with many classes. I do use try-catchs where
I have a Java web application that has a 'disconnected' Java Swing desktop app.
I'm trying to create a Java swing application and I'm trying to use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.