I worked on migrating a Java application from WindowsLookAndFeel to Nimbus, pretty much successfully, despite the flaws of Nimbus. My users in general are like Nimbus LaF, but did not like some of the details, some of which I changed by referring to the previous questions on this site. Example: I copied the LeafIcon, ClosedIcon, and OpenIcon files from Windows LaF (which they liked) and used them in the Nimbus version for a nice combination of LaFs.
Stuck on one last (?) Problem.
I have a JTree with a subclass of DefaultCellRenderer to create the corresponding node displays. This works fine under WindowsLookAndFeel.
Problem: In WindowsLaF, when 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 selection is made using a strip of (rather dark) color that controls the width of the tree window (and not just the width of the text), and the effect is confusing.
So: I just want WindowsLaF to handle the JTree node selection in Nimbus LaF (that is, the color background is only the width of the text and, preferably, the best color I can choose). I suspect this means that I need to assign the correct Painter collation "Tree: TreeCell [Focused + Selected] .backgroundPainter", but I donβt know how to write it.
Suggestions are welcome.
EDIT
Check out weird highlighted node allocation 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); } } }
java swing renderer jtree nimbus
user1359010
source share