I have been working to transition a Java application from WindowsLookAndFeel to Nimbus, largely successfully, despite Nimbus foibles. My users overall like the Nimbus LaF but didn't like some details, some of which I changed by consulting previous questions on this site. Example: I copied the LeafIcon, ClosedIcon and OpenIcon from Windows LaF (which they liked) and use them in the Nimbus version, for a nice combination of LaFs.
Stuck on one last (?) problem.
I have a JTree with a subclassed DefaultCellRenderer to create the appropriate node displays. This works fine under WindowsLookAndFeel.
Problem: Under WindowsLaF when a node is selected the text of the node is highlighted, and the effect is visually easy to understand. Under Nimbus when a node is selected the highlighting is done with a bar of (fairly dark) color that runs the width of the tree window (not just the width of the text), and the effect is disconcerting.
So: I simply want WindowsLaF treatment of JTree node highlighting in the Nimbus LaF (ie colored background only the width of the text, and preferably in a better color that I can choose). I suspect this means I need to assign the right sort Painter to "Tree:TreeCell[Focused+Selected].backgroundPainter", but I don't know how to write it.
Suggestions most welcome.
EDIT
See the strange selected node highlight with Java 7!
public class TreeBorder {
public static void main( String[] args ) {
try{
for( UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels() ) {
if( "Nimbus".equals( info.getName() ) ) {
UIManager.setLookAndFeel( info.getClassName() );
break;
}
}
} catch( Exception e ) {
e.printStackTrace();
}
SwingUtilities.invokeLater( new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setLocationRelativeTo( null );
f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
f.getContentPane().add( getJTree() );
f.pack();
f.setVisible( true );
}
private JTree getJTree() {
JTree jTree = new JTree();
jTree.setCellRenderer( new LocalRenderer() );
return jTree;
}
} );
}
private static class LocalRenderer extends DefaultTreeCellRenderer {
@Override
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasfocus ) {
DefaultTreeCellRenderer result = (DefaultTreeCellRenderer)super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasfocus );
if( true ) {
result.setFont( new JLabel().getFont() );
Icon icon = UIManager.getIcon("FileView.floppyDriveIcon");
result.setIcon( icon );
}
return(result);
}
}
}