我正在使用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();
}
}