我使用的是JXTreeTable
。 我想显示仅当行被折叠的单元格的文本。 如果该行被扩大了详细的值在孩子的出这样父缩短的文字应该不再可见。
据我所知,这需要通过提供一个特殊的实施ComponentProvider
由使用DefaultTableRenderer
。 总之在CellContext
由所使用的ComponentProvider
始终表示的节点展开。 context.isExpanded()
总是返回true
。
StringValue valueProvider = new StringValue()
{
@Override
public String getString(Object value)
{
return String.valueOf(value);
}
};
ComponentProvider<?> textProvider = new LabelProvider(valueProvider, JLabel.TRAILING)
{
@Override
protected String getValueAsString(CellContext context)
{
System.out.println("Context expanded " + context.isExpanded());
if (context.isExpanded())
{
return "";
}
return super.getValueAsString(context);
}
};
DefaultTableRenderer renderer = new DefaultTableRenderer(textProvider);
我有什么在改变ComponentProvider
如果单元格的行扩展或不检测?
该解决方案可以根据dic19的答案来实现。
的解决方案是实现具有一种特殊的渲染器getTableCellRendererComponent
用于检查的方法JXTreeTable
。 在这种情况下,如果该行扩大其被评估。 对于支票leaf
中也可以加入标志。
不幸的是这是不可能修改DefaultTableRenderer
因为CellContext不是压倒一切的类可见。
public class DefaultTreeTableRenderer
extends AbstractRenderer
implements TableCellRenderer
{
private TableCellContext cellContext;
/**
* Instantiates a default table renderer with the default component provider.
*
* @see #DefaultTableRenderer(ComponentProvider)
*/
public DefaultTreeTableRenderer()
{
this((ComponentProvider<?>)null);
}
/**
* Instantiates a default table renderer with the given component provider. If the controller is null, creates
* and uses a default. The default provider is of type <code>LabelProvider</code>.
*
* @param componentProvider the provider of the configured component to use for cell rendering
*/
public DefaultTreeTableRenderer(ComponentProvider<?> componentProvider)
{
super(componentProvider);
this.cellContext = new TableCellContext();
}
/**
* Instantiates a default table renderer with a default component provider using the given converter.
*
* @param converter the converter to use for mapping the content value to a String representation.
* @see #DefaultTableRenderer(ComponentProvider)
*/
public DefaultTreeTableRenderer(StringValue converter)
{
this(new LabelProvider(converter));
}
/**
* Instantiates a default table renderer with a default component provider using the given converter and
* horizontal alignment.
*
* @param converter the converter to use for mapping the content value to a String representation.
* @see #DefaultTableRenderer(ComponentProvider)
*/
public DefaultTreeTableRenderer(StringValue converter, int alignment)
{
this(new LabelProvider(converter, alignment));
}
/**
* Intantiates a default table renderer with default component provider using both converters.
*
* @param stringValue the converter to use for the string representation
* @param iconValue the converter to use for the icon representation
*/
public DefaultTreeTableRenderer(StringValue stringValue, IconValue iconValue)
{
this(new MappedValue(stringValue, iconValue));
}
/**
* Intantiates a default table renderer with default component provider using both converters and the given
* alignment.
*
* @param stringValue the converter to use for the string representation
* @param iconValue the converter to use for the icon representation
* @param alignment the rendering component's horizontal alignment
*/
public DefaultTreeTableRenderer(StringValue stringValue, IconValue iconValue, int alignment)
{
this(new MappedValue(stringValue, iconValue), alignment);
}
// -------------- implements javax.swing.table.TableCellRenderer
/**
* Returns a configured component, appropriate to render the given list cell.
* <p>
* Note: The component's name is set to "Table.cellRenderer" for the sake of Synth-based LAFs.
*
* @param table the <code>JTable</code>
* @param value the value to assign to the cell at <code>[row, column]</code>
* @param isSelected true if cell is selected
* @param hasFocus true if cell has focus
* @param row the row of the cell to render
* @param column the column of the cell to render
* @return the default table cell renderer
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
boolean expanded = true;
boolean leaf = true;
if (table instanceof JXTreeTable)
{
JXTreeTable treeTable = (JXTreeTable)table;
expanded = treeTable.isExpanded(row);
}
this.cellContext.installContext(table, value, row, column, isSelected, hasFocus, expanded, leaf);
Component comp = this.componentController.getRendererComponent(this.cellContext);
// fix issue #1040-swingx: memory leak if value not released
this.cellContext.replaceValue(null);
return comp;
}
/**
* {@inheritDoc}
*/
@Override
protected ComponentProvider<?> createDefaultComponentProvider()
{
return new LabelProvider();
}
}
总之在CellContext
由所使用的ComponentProvider
始终表示的节点展开。
这种行为可以追溯到实施getTableCellRenderer(...)提供DefaultTableRenderer
:既expanded
和leaf
属性设置为true
:
public class DefaultTableRenderer extends AbstractRenderer implements TableCellRenderer {
private TableCellContext cellContext;
...
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
// both 'expanded' and 'leaf' properties are set to 'true' here:
cellContext.installContext(table, value, row, column, isSelected, hasFocus, true, true);
// 'componentController' is an instance of 'ComponentProvider<?>'
Component comp = componentController.getRendererComponent(cellContext);
cellContext.replaceValue(null);
return comp;
}
...
}
它使给出的性质意义是在树上电池方面,而不是一个表格单元格背景显著。 如果设置了TreeCellRenderer
到JXTreeTable
使用同样的ComponentProvider
那么就应该按预期在第一列(树列):
ComponentProvider<?> textProvider = new LabelProvider(valueProvider, JLabel.TRAILING) {...};
...
DefaultTreeRenderer treeRenderer = new DefaultTreeRenderer(textProvider);
...
treeTable.setTreeCellRenderer(treeRenderer);
因为CellContext
将不会是一个TableCellContext
但TreeCellContext
,它将包含正确的expanded
和leaf
值:
public class DefaultTreeRenderer extends AbstractRenderer implements TreeCellRenderer {
private TreeCellContext cellContext;
...
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
// both 'expanded' and 'leaf' properties are correctly set here:
cellContext.installContext(tree, value, row, 0, selected, hasFocus, expanded, leaf);
// 'componentController' is an instance of 'ComponentProvider<?>'
Component comp = componentController.getRendererComponent(cellContext);
cellContext.replaceValue(null);
return comp;
}
...
}