java - 将表头添加到自定义滚动窗格
问题描述
我目前正在开发一个项目,该项目需要我拥有一个看起来像浏览器滚动窗格的自定义ModernScrollPane 。
所以我想向ModernScrollPane添加一个表格,但标题不会像普通的 scrollPanes 一样添加到ModernScrollPane 。
对于我的问题,我将不胜感激。
该指令 sp.setColumnHeaderView(jt.getTableHeader())
不起作用。
这是我的代码:
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;
/**
* This is an implementation of a JScrollPane with a modern UI
*
* @author Philipp Danner
*
*/
public class ModernScrollPane extends JScrollPane {
private static final long serialVersionUID = 8607734981506765935L;
private static final int SCROLL_BAR_ALPHA_ROLLOVER = 100;
private static final int SCROLL_BAR_ALPHA = 70;
private static final int THUMB_SIZE = 15;
private static final int SB_SIZE = 15;
public ModernScrollPane(Component view) {
this(view, VERTICAL_SCROLLBAR_AS_NEEDED, HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
public ModernScrollPane(int vsbPolicy, int hsbPolicy) {
this(null, vsbPolicy, hsbPolicy);
}
public ModernScrollPane(Component view, int vsbPolicy, int hsbPolicy) {
setBorder(null);
// Set ScrollBar UI
JScrollBar verticalScrollBar = getVerticalScrollBar();
verticalScrollBar.setOpaque(false);
verticalScrollBar.setUI(new ModernScrollBarUI(this));
JScrollBar horizontalScrollBar = getHorizontalScrollBar();
horizontalScrollBar.setOpaque(false);
horizontalScrollBar.setUI(new ModernScrollBarUI(this));
setLayout(new ScrollPaneLayout() {
private static final long serialVersionUID = 5740408979909014146L;
@Override
public void layoutContainer(Container parent) {
Rectangle availR = ((JScrollPane) parent).getBounds();
availR.x = availR.y = 0;
// viewport
Insets insets = parent.getInsets();
availR.x = insets.left;
availR.y = insets.top;
availR.width -= insets.left + insets.right;
availR.height -= insets.top + insets.bottom;
if (viewport != null) {
viewport.setBounds(availR);
}
boolean vsbNeeded = isVerticalScrollBarfNecessary();
boolean hsbNeeded = isHorizontalScrollBarNecessary();
// vertical scroll bar
Rectangle vsbR = new Rectangle();
vsbR.width = SB_SIZE;
vsbR.height = availR.height - (hsbNeeded ? vsbR.width : 0);
vsbR.x = availR.x + availR.width - vsbR.width;
vsbR.y = availR.y;
if (vsb != null) {
vsb.setBounds(vsbR);
}
// horizontal scroll bar
Rectangle hsbR = new Rectangle();
hsbR.height = SB_SIZE;
hsbR.width = availR.width - (vsbNeeded ? hsbR.height : 0);
hsbR.x = availR.x;
hsbR.y = availR.y + availR.height - hsbR.height;
if (hsb != null) {
hsb.setBounds(hsbR);
}
}
});
// Layering
setComponentZOrder(getVerticalScrollBar(), 0);
setComponentZOrder(getHorizontalScrollBar(), 1);
setComponentZOrder(getViewport(), 2);
viewport.setView(view);
}
private boolean isVerticalScrollBarfNecessary() {
Rectangle viewRect = viewport.getViewRect();
Dimension viewSize = viewport.getViewSize();
return viewSize.getHeight() > viewRect.getHeight();
}
private boolean isHorizontalScrollBarNecessary() {
Rectangle viewRect = viewport.getViewRect();
Dimension viewSize = viewport.getViewSize();
return viewSize.getWidth() > viewRect.getWidth();
}
/**
* Class extending the BasicScrollBarUI and overrides all necessary methods
*/
private static class ModernScrollBarUI extends BasicScrollBarUI {
private JScrollPane sp;
public ModernScrollBarUI(ModernScrollPane sp) {
this.sp = sp;
}
@Override
protected JButton createDecreaseButton(int orientation) {
return new InvisibleScrollBarButton(scrollbar.getOrientation(), "⌃");
}
@Override
protected JButton createIncreaseButton(int orientation) {
return new InvisibleScrollBarButton(scrollbar.getOrientation(), "⌄");
}
@Override
protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
}
@Override
protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
int alpha = isThumbRollover() ? SCROLL_BAR_ALPHA_ROLLOVER : SCROLL_BAR_ALPHA;
int orientation = scrollbar.getOrientation();
int x = thumbBounds.x;
int y = thumbBounds.y;
int width = orientation == JScrollBar.VERTICAL ? THUMB_SIZE : thumbBounds.width;
width = Math.max(width, THUMB_SIZE);
int height = orientation == JScrollBar.VERTICAL ? thumbBounds.height : THUMB_SIZE;
height = Math.max(height, THUMB_SIZE);
Graphics2D graphics2D = (Graphics2D) g.create();
graphics2D.setColor(new Color(180, 180, 180, alpha));
graphics2D.fillRect(x, y, width, height);
graphics2D.dispose();
}
@Override
protected void setThumbBounds(int x, int y, int width, int height) {
super.setThumbBounds(x, y, width, height);
sp.repaint();
}
/**
* Invisible Buttons, to hide scroll bar buttons
*/
private static class InvisibleScrollBarButton extends JButton {
private static final long serialVersionUID = 1552427919226628689L;
private InvisibleScrollBarButton(int orientation, String text) {
super();
setBackground(new Color(18, 18, 18));
setForeground(new Color(158, 158, 158));
setOpaque(false);
setFocusable(false);
setBorder(BorderFactory.createEmptyBorder());
setBorderPainted(false);
setFocusPainted(false);
}
}
}
}`
//in the Main class
JScrollPane sp = new ModernScrollPane(jt);
sp.setColumnHeaderView(jt.getTableHeader());
解决方案
推荐阅读
- javascript - Vue.js 与 axios 的异步 api 调用
- linux - windows函数“CreateWaitableTimer”和“SetWaitableTimer”如何在Linux上实现?在 Linux 上是否有任何等效的替代品?
- reactjs - 测试使用上下文的反应组件 - 更改被测组件的状态
- r - 链接到 Github 存储库时 readRDS 出错
- python - Avoid opening an new browser for every instance in Selenium Python
- c++ - 在错误的 DLL 中查找函数
- python - 如何使用两个相同的键和不同的值从 Python 中的三个不同列表创建字典?
- azure-cosmosdb - 在 Azure Cosmos DB Graph 查询中定义变量
- java - 有没有办法在泛型函数中将 List Object 转换为精确的 List Class?
- github - 如何在 GitHub 上更改 GitHub 包注册表描述?