gpt4 book ai didi

java - 跨越 JTable 特定列的所有行(以呈现 JTextPane)

转载 作者:行者123 更新时间:2023-11-30 07:23:56 31 4
gpt4 key购买 nike

我想构建一个标准的 JTable,但所有行都分布在一个特定的列上。因此该列必须仅包含一个以 JTextPane 作为其渲染器的单元格。您知道任何简单的方法吗?

注意:不需要第三方软件。

谢谢。

最佳答案

由某种覆盖的@MvG 想法触发,这里是使用 JLayer 的概念验证(添加到 jdk7 的核心,对于早期版本,使用非常相似的 JXLayer in SwingLabs)

基本成分:

  • 在其 glassPane 中管理文本区域的 LayerUI
  • 自定义 layoutManager,它在列上调整/定位 textArea
  • 强制重新布局 glassPane 的一些听众

至少让它发挥作用出人意料地简单。不过,也有一些粗糙的边缘:

  • 与往常一样,在表格中导航:当表格具有焦点时,其单元格选择将移动到 textArea 下方。可能需要自定义 selectionModel
  • 移动列时,该列下的表格行可见
  • ??可能又是一群魔鬼,毕竟我们混淆了用户界面:-)

部分代码:

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());

关于java - 跨越 JTable 特定列的所有行(以呈现 JTextPane),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11720617/

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