bulenkov / Darcula

Darcula Look and Feel
Apache License 2.0
723 stars 114 forks source link

JTree rendering is off #39

Open DalgEllal opened 7 years ago

DalgEllal commented 7 years ago

See image - the tree is cropped and too little indentation darculajtree

Obtained by this code

package Tools.DarkculaTest;

import com.bulenkov.darcula.DarculaLaf;

import javax.swing.; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

public class DarkculaTest extends JFrame implements ActionListener { public DarkculaTest() throws HeadlessException { JTree jTree = new JTree(); Container contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(jTree, BorderLayout.CENTER); this.setSize(200, 300); CustomTreeNode root = new CustomTreeNode(0); jTree.setModel(new DefaultTreeModel(root));

    JButton lookAndFeelSwitchButton = new JButton("Switch lookAndFeel");
    lookAndFeelSwitchButton.addActionListener(this);
    contentPane.add(lookAndFeelSwitchButton, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent actionEvent)
{
    switchLookAndFeel();
    SwingUtilities.updateComponentTreeUI(this);
}

private class CustomTreeNode extends DefaultMutableTreeNode
{
    boolean areChildrenDefined = false;
    int level;

    private final int [] counts = new int []{5,7,4,7};

    public CustomTreeNode(int level)
    {
        this.level = level;
    }

    public int getChildCount()
    {
        if(level == 4)
            return 0;

        if (!areChildrenDefined)
            defineChildNodes();

        return (super.getChildCount());
    }

    private void defineChildNodes()
    {
        areChildrenDefined = true;

        for(int index = 0; index < counts[level]; index++)
            add(new CustomTreeNode(level+1));
    }

}

static boolean darculaOn = false;
public static void main(String[] args)
{
    switchLookAndFeel();
    new DarkculaTest().setVisible(true);
}

private static void switchLookAndFeel()
{
    try
    {
        if(darculaOn)
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        else
            UIManager.setLookAndFeel(new DarculaLaf());

        darculaOn = !darculaOn;
    }
    catch (Exception e)
    {
        System.out.println("Unable to set look and feel");
    }
}

}

DalgEllal commented 7 years ago

The problem seems to be related to the code below in DarculaTreeUI - returning false instead in isSkinny fixes the problem.

@Override public int getRightChildIndent() { return isSkinny() ? 8 : super.getRightChildIndent(); }

private static boolean isSkinny() { return true; }