Search code examples
javaswingjtreelook-and-feelnimbus

Nimbus JTree presentation error under java version 1.8


right now I am establishing the Nimbus Look and Feel to an Application which is already in use for some time. The Application contains some JTrees which I want to have to display vertical and horizontal lines.

Under java version 1.7 which I used earlier this was easy to set up with those specific entries in the UIDefaults:

  • UIManager.put("Tree.drawVerticalLines", true);

and

  • UIManager.put("Tree.drawHorizontalLines", true);

As implied above this works totally fine as long as I am using a jre with the verion 1.7, as soon as I use 1.8 the vertical lines in the JTree wont get displayed.

I just wanted to ask if anybody knows if this a known issue of Nimbus under java 1.8 and if so, does anyone know a solution or workaround to this problem?

EDIT: Here some example code to clarify my Problem:

public class test 
{

public static void main(String args[]) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
            | UnsupportedLookAndFeelException e) {

        e.printStackTrace();
    }

    UIManager.put("Tree.drawVerticalLines", true);
    UIManager.put("Tree.drawHorizontalLines", true);
    UIManager.put("Tree.linesStyle", "dashed");

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Root");
    top.add(new DefaultMutableTreeNode("Branch1"));
    top.add(new DefaultMutableTreeNode("Branch2"));
    top.add(new DefaultMutableTreeNode("Branch3"));
    ((DefaultMutableTreeNode)top.getFirstChild()).add(new DefaultMutableTreeNode("Leaf1"));
    ((DefaultMutableTreeNode)top.getFirstChild()).add(new DefaultMutableTreeNode("Leaf2"));


    JFrame frame = new JFrame();
    JTree tree = new JTree(top);
    frame.setSize(new Dimension(450,300));
    JScrollPane scroll = new JScrollPane(tree);
    frame.add(scroll);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

}
}

This is just an example code, not the actual software I am currently working at, so I think the issue is up to a mistake I did in both codes or its some problem with the java-version 1.8.

The usage of jdk1.7 and jdk1.8 leads to two different results:

jdk1.7

jdk1.7

jdk1.8

jdk1.8

As you can see the horizontal lines in the 1.8-version are missing.

Sorry for bad grammar, I´m no native speaker.


Solution

  • The reason is unknown (bug?), but it seems to work fine for me using UIManager.getLookAndFeelDefaults().put("Tree.drawVerticalLines", true); instead of UIManager.put("Tree.drawVerticalLines", true); (jdk1. 8.0_131 on Windows 10):

    import java.awt.*;
    import javax.swing.*;
    
    public class NimbusDrawVerticalLinesTest {
      public static void main(String... args) {
        EventQueue.invokeLater(() -> {
          try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch (Exception e) {
            e.printStackTrace();
          }
          // UIManager.put("Tree.drawVerticalLines", true);
          UIManager.getLookAndFeelDefaults().put("Tree.drawVerticalLines", true);
          UIManager.put("Tree.drawHorizontalLines", true);
          UIManager.put("Tree.linesStyle", "dashed");
    
          JTree tree = new JTree();
    //       UIDefaults d = new UIDefaults();
    //       d.put("Tree.drawVerticalLines", Boolean.TRUE);
    //       tree.putClientProperty("Nimbus.Overrides", d);
    
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.getContentPane().add(new JScrollPane(tree));
          f.setSize(320, 240);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        });
      }
    }