CellContext.isExpanded()在JXTreeTable中始终返回true [英] CellContext.isExpanded() returns always true in JXTreeTable

查看:100
本文介绍了CellContext.isExpanded()在JXTreeTable中始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JXTreeTable. 我只想在折叠的行中显示文本.如果该行被展开,则详细值将显示在子级中,因此父级中的缩短文本将不再可见.

I am using a JXTreeTable. I want to show a text in a cell only if the row is collapsed. If the row is expanded the detailed values are shown in the childs so the shortened text in the parent should no longer be visible.

据我所知,这需要通过提供DefaultTableRenderer所使用的特殊ComponentProvider来实现.无论如何,ComponentProvider使用的CellContext始终表示该节点已扩展. context.isExpanded()始终返回true.

As far as I know this need to be implemented by providing a special ComponentProvider used by the DefaultTableRenderer. Anyhow the CellContext used by the ComponentProvider always indicates that the node is expanded. context.isExpanded() always returns 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中进行哪些更改才能检测单元格的行是否已展开?

What do I have to change in the ComponentProvider to detect if the row of the cell is expanded or not?

推荐答案

该解决方案可以基于dic19的答案来实现.

The solution can be implemented based on the answer of dic19.

解决方案是使用getTableCellRendererComponent方法实现特殊的渲染器,该方法检查JXTreeTable.在这种情况下,将评估行是否被扩展.也可以添加对leaf标志的检查.

The solution is to implement a special renderer with a getTableCellRendererComponent method that checks for a JXTreeTable. In such a case it is evaluated if the row is expanded. The check for the leaf flag may be added also.

很遗憾,无法修改DefaultTableRenderer,因为CellContext在覆盖的类中不可见.

Unfortunately it is not possible to modify the DefaultTableRenderer because the CellContext is not visible in overriding classes.

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.isExpanded()在JXTreeTable中始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆