自定义Jtable滚动条

时间:2014-02-14 22:00:49

标签: java swing jtable

我正在制作基于Swing的桌面应用程序。

我想在jtable中自定义滚动条。我已经得到以下代码来自定义滚动条但我不知道如何将它应用于jtable。

public class MyScrollBarUI extends BasicScrollBarUI {

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        g.setColor(Color.black);
        g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height);
        if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) {
            this.paintDecreaseHighlight(g);
        } else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) {
            this.paintIncreaseHighlight(g);
        }
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.setColor(this.thumbDarkShadowColor);
        g.drawOval(2, 0, 14, 14);
        g.setColor(this.thumbColor);
        g.fillOval(2, 0, 14, 14);
        g.setColor(this.thumbHighlightColor);
        g.setColor(this.thumbLightShadowColor);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    } 
}

我执行以下操作以在程序开头设置滚动条的外观和感觉

UIManager.put("ScrollBarUI", "mypackage.ui.customization.MyScrollBarUI");

1 个答案:

答案 0 :(得分:2)

如果您在UI的特定滚动条上执行此操作,则只需在包裹表格的滚动窗格上直接设置ScrollBarUI的新实例。

以下说明如何执行此操作:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicScrollBarUI;
import javax.swing.table.DefaultTableModel;

public class MyScrollBarUI extends BasicScrollBarUI {

    protected static void initUI() {
        JFrame frame = new JFrame("Test scrollbar UI");
        JTable table = new JTable(new DefaultTableModel(30, 5));
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.getVerticalScrollBar().setUI(new MyScrollBarUI());
        frame.add(scrollPane);
        frame.pack();
        frame.setSize(frame.getWidth(), frame.getHeight()-50); // Forces the vertical scroll bar to show up
        frame.setVisible(true);
    }

    @Override
    protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
        g.setColor(Color.black);
        g.fillRect(trackBounds.width / 2, trackBounds.y, 3, trackBounds.height);
        if (this.trackHighlight == BasicScrollBarUI.DECREASE_HIGHLIGHT) {
            this.paintDecreaseHighlight(g);
        } else if (this.trackHighlight == BasicScrollBarUI.INCREASE_HIGHLIGHT) {
            this.paintIncreaseHighlight(g);
        }
    }

    @Override
    protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
        if (thumbBounds.isEmpty() || !this.scrollbar.isEnabled()) {
            return;
        }
        g.translate(thumbBounds.x, thumbBounds.y);
        g.setColor(this.thumbDarkShadowColor);
        g.drawOval(2, 0, 14, 14);
        g.setColor(this.thumbColor);
        g.fillOval(2, 0, 14, 14);
        g.setColor(this.thumbHighlightColor);
        g.setColor(this.thumbLightShadowColor);
        g.translate(-thumbBounds.x, -thumbBounds.y);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                initUI();
            };
        });
    }

}