This Java Swing tip illustrates a method of replacing the existing scrollbar UI of an application with your own. This tip may also be used as an example of modifying an existing L&F. This tip replaces the standard scrollbar UI with a custom one. Developers may use this in their applications and provide a customized look to their users according to their needs.

 import javax.swing.*;
import java.awt.*;

public class MetalModExample {
  public static void main(String[] args) {
      // Make sure we're using the Metal L&F, since the example needs it
      try {
      catch (Exception e) {
          System.err.println("Metal is not available on this platform?!");
    JComponent before = makeExamplePane();

    // Replace the MetalScrollBarUI with our own!
    UIManager.put("ScrollBarUI", "MyMetalScrollBarUI");

    JComponent after = makeExamplePane();

    JFrame f = new JFrame();

    Container c = f.getContentPane();
    c.setLayout(new GridLayout(2, 1, 0, 1));
    f.setSize(450, 400);

  // Create a scroll pane with a text area in it.
  public static JComponent makeExamplePane() {
    JTextArea text = new JTextArea();

    try { FileReader(""), null);
    catch (IOException ex) {}

    JScrollPane scroll = new JScrollPane(text);
    return scroll;