我想构建一个标准的 JTable,但所有行都跨越一个特定的列。因此,该列必须只包含一个以 JTextPane 作为其渲染器的单元格。你知道有什么简单的方法吗?
注意:不需要第三方软件。
谢谢。
由@MvG 的某种覆盖的想法触发,这里是使用 JLayer 的概念验证(添加到 jdk7 的核心,对于早期版本,在 SwingLabs中使用非常相似的 JXLayer)
基本成分:
至少让它工作起来非常简单。但是,有一些粗糙的边缘:
一些代码:
public static class RowSpanUI extends LayerUI<JTable> {
public static String COLUMN_TO_SPAN_KEY = "Table.columnToSpan";
private JLayer layer;
private JTextArea area;
@Override
public void installUI(JComponent c) {
super.installUI(c);
this.layer = (JLayer) c;
installTextArea();
installListeners();
}
@Override
public void doLayout(JLayer<? extends JTable> l) {
super.doLayout(l);
l.getGlassPane().doLayout();
}
private void installTextArea() {
area = new JTextArea(10, 20);
layer.getGlassPane().setBorder(BorderFactory.createLineBorder(Color.RED));
layer.getGlassPane().setLayout(new ColumnLayoutManager(this));
layer.getGlassPane().add(area);
layer.getGlassPane().setVisible(true);
}
public JTable getView() {
return (JTable) layer.getView();
}
public int getViewColumnToSpan() {
Object clientProperty = getView().getClientProperty(COLUMN_TO_SPAN_KEY);
if (clientProperty instanceof Integer) {
return getView().convertColumnIndexToView((int) clientProperty);
}
return -1;
}
/**
* Install listeners to manually trigger a layout of the glassPane.
* This is incomplete, just the minimum for demonstration!
*/
protected void installListeners() {
ComponentListener compL = new ComponentListener() {
@Override
public void componentShown(ComponentEvent e) {
doLayout(layer);
}
@Override
public void componentResized(ComponentEvent e) {
doLayout(layer);
}
@Override
public void componentMoved(ComponentEvent e) {
doLayout(layer);
}
@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
}
};
layer.addComponentListener(compL);
TableColumnModelListener columnL = new TableColumnModelListener() {
@Override
public void columnRemoved(TableColumnModelEvent e) {
doLayout(layer);
}
@Override
public void columnMoved(TableColumnModelEvent e) {
doLayout(layer);
}
@Override
public void columnMarginChanged(ChangeEvent e) {
doLayout(layer);
}
@Override
public void columnAdded(TableColumnModelEvent e) {
doLayout(layer);
}
@Override
public void columnSelectionChanged(ListSelectionEvent e) {
}
};
getView().getColumnModel().addColumnModelListener(columnL);
}
}
public static class ColumnLayoutManager implements LayoutManager {
private RowSpanUI ui;
public ColumnLayoutManager(RowSpanUI ui) {
this.ui = ui;
}
@Override
public void layoutContainer(Container parent) {
Component child = parent.getComponent(0);
child.setBounds(getColumnBounds());
}
@Override
public Dimension preferredLayoutSize(Container parent) {
return ui.getView().getSize();
}
protected Rectangle getColumnBounds() {
int viewColumn = ui.getViewColumnToSpan();
if (viewColumn < 0) {
return new Rectangle();
}
Rectangle r = ui.getView().getCellRect(0, viewColumn, false);
r.height = ui.getView().getHeight();
return r;
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void removeLayoutComponent(Component comp) {
}
}
// usage
JTable table = new JTable(myModel);
table.putClientProperty(RowSpanUI.COLUMN_TO_SPAN_KEY, 2);
JLayer layer = new JLayer(table, new RowSpanUI());
a 的呈现JTable
,包括对单元格矩形的剪裁,深埋在所讨论的可插拔外观的用户界面实现中。改变那里的东西将会是一团糟,并且高度依赖于实际的 JFC 实施。
因此,我建议以您喜欢的任何方式绘制这些 roes 的单元格,并在表格顶部放置另一个透明窗格,以使用您描述的跨区内容覆盖这些区域。仍然很乱,可能需要做很多工作,但更可能是便携的。