gpt4 book ai didi

java - 自定义Jtable滚动条

转载 作者:行者123 更新时间:2023-11-30 04:01:52 25 4
gpt4 key购买 nike

我正在制作一个基于 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");

最佳答案

如果您在 UI 的特定滚动条上执行此操作,则只需直接在包裹表格的滚动 Pane 上设置 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();
};
});
}

}

关于java - 自定义Jtable滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21790288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com