我正在使用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
更改以检测单元格的行是否展开?
答案 0 :(得分:1)
可以根据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();
}
}
答案 1 :(得分:0)
无论如何,
CellContext
使用的ComponentProvider
始终表示节点已展开。
此行为可追溯到DefaultTableRenderer
提供的getTableCellRenderer(...)的实现: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;
}
...
}