Skip to main content

JXTreeTable and font highlighter (BoldHighlighter)

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
montechristos
Offline
Joined: 2005-09-29

There is a bug in JXTreeTable since very early versions of SwingX which is still there:

I have made a BoldHighligher which displays the highlighted item in bold.
In JXTree, it works as expected.
In JXTreeTable, as soon as one element is highlighted, all subsequent elements are also highlighted. I believe that the font of the renderer is not reset at each cell...?

Here is the code:

public class BoldHighlighter extends AbstractHighlighter {
private Font BOLD_FONT = null;

public BoldHighlighter(final HighlightPredicate predicate) {
super(predicate);
}

@Override
protected Component doHighlight(Component renderer, ComponentAdapter adapter) {
if (BOLD_FONT == null) {
BOLD_FONT = renderer.getFont().deriveFont(Font.BOLD);
}

renderer.setFont(BOLD_FONT);

return renderer;
}
}

Many thanks!
(Kleopatra, I had talked to you about a similar bug few years back :-)

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
montechristos
Offline
Joined: 2005-09-29

And the small runnable code:

package testhighlighter;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.decorator.ComponentAdapter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.renderer.DefaultTreeRenderer;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;

public class TreeTableTest {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JXTreeTable tree = new JXTreeTable();

tree.setTreeCellRenderer(new DefaultTreeRenderer());

DefaultMutableTreeTableNode rootNode = new DefaultMutableTreeTableNode("Hello");
for (int i = 0; i < 10; i++) {
rootNode.add(new DefaultMutableTreeTableNode("a node"));
rootNode.add(new DefaultMutableTreeTableNode("b node"));
}

tree.setTreeTableModel(new DefaultTreeTableModel(rootNode));
tree.setRootVisible(true);

tree.addHighlighter(new BoldHighlighter(new HighlightPredicate() {
@Override
public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
return adapter.getValue().toString().startsWith("a");
}
}));

JFrame frame = new JFrame("Test");
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new JScrollPane(tree));

frame.pack();
frame.setVisible(true);
}
});
}
}
kleopatra
Offline
Joined: 2003-06-11

yeah, we are aware of the issue: the base reason is that the JXTree which is used as a renderer in the hierarchical column is not a ComponentProvider (and consequently indeed not updated as needed)

No pending plans to fix it, sorry - thanks for the example, though!

Cheers
Jeanette