JTree.lineStyle : alignment of lines

Hi folks,
I'm trying to align the angled node line to the top of the child node.
tree.putClientProperty("JTree.lineStyle", "Angled")  draws lines showing the relationship between nodes on the tree. However the line is joined to the child node aligned vertically to the child's centre. My child node can be multline, e.g. a multline description, and I'd like the join line to connect to the child node at the top where the node's icon is and where the text begins.
I'm sure this must be possible but I can't work it out.
Thanks,
Des

Okay, here's one, but you'll do much better if you ask on Swing: it probably has 20x the readership of Java 2D.
import java.awt.*;
import javax.swing.*;
public class X {
    public static void main(String[] args)  {
        UIManager.put("Tree.hash", Color.RED);
        JTree tree = new JTree();
        JFrame f = new JFrame("X");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(tree);
        f.setSize(400, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);

Similar Messages

  • PutClientProperty("JTree.lineStyle", "Angled") in JDK 1.4.2

    In 1.4.2 the look of the tree has been updated to remove the ---- line handles on the tree by default. I would like to continue displaying those as they appeared in earlier versions without using the older classic windows look and feel for the rest of the application (so I don't want to use the command line toggle that alters this).
    Has anyone gotten the above client property to work properly on the tree in 1.4.2? I've been having difficulties with this. I generally do something like the following:
    tree.putClientProperty("JTree.lineStyle", "Angled");
    tree.repaint();
    OR
    tree.updateUI();
    Would it also be possible to override this for all instances of JTree's in the application by setting something in the UIManager?

    There seems to be a great deal of confusion here.
    The property "JTree.lineStyle" is (unfortunately) only used by
    MetalTreeUI, and won't have any effect in the Windows L&F. It is a
    client property, which (unfortunately) is a different mechanism from
    the UIManager defaults.
    The bug is in WindowsTreeUI and will be fixed in 1.5. There is no
    easy workaround, except to replace the UI class with a fixed one. Here
    is an attempt which seems to work. I don't give any guarantees, but
    please let me know how it works for you.
    import com.sun.java.swing.plaf.windows.WindowsTreeUI;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.plaf.basic.*;
    import javax.swing.plaf.*;
    import javax.swing.tree.*;
    * A subclass of JTree that fixes bug 4887931: Tree lines
    * missing on Windows XP
    public class JTree14 extends JTree {
        public JTree14() {
        public JTree14(Object[] value) {
         super(value);
        public JTree14(Vector value) {
         super(value);
        public JTree14(Hashtable value) {
         super(value);
        public JTree14(TreeNode root) {
         super(root);
        public JTree14(TreeNode root, boolean asksAllowsChildren) {
         super(root, asksAllowsChildren);
        public JTree14(TreeModel newModel) {
         super(newModel);
        public void setUI(TreeUI ui) {
         if (ui instanceof WindowsTreeUI &&
             System.getProperty("java.version").equals("1.4.2")) {
             ui = new FixedWindowsTreeUI();
         super.setUI(ui);
        private static class FixedWindowsTreeUI extends BasicTreeUI {
         protected void ensureRowsAreVisible(int beginRow, int endRow) {
             if(tree != null && beginRow >= 0 && endRow < getRowCount(tree)) {
              Rectangle visRect = tree.getVisibleRect();
              if(beginRow == endRow) {
                  Rectangle     scrollBounds = getPathBounds(tree, getPathForRow
                                              (tree, beginRow));
                  if(scrollBounds != null) {
                   scrollBounds.x = visRect.x;
                   scrollBounds.width = visRect.width;
                   tree.scrollRectToVisible(scrollBounds);
              else {
                  Rectangle   beginRect = getPathBounds(tree, getPathForRow
                                         (tree, beginRow));
                  Rectangle   testRect = beginRect;
                  int         beginY = beginRect.y;
                  int         maxY = beginY + visRect.height;
                  for(int counter = beginRow + 1; counter <= endRow; counter++) {
                   testRect = getPathBounds(tree,
                                   getPathForRow(tree, counter));
                   if((testRect.y + testRect.height) > maxY)
                       counter = endRow;
                  tree.scrollRectToVisible(new Rectangle(visRect.x, beginY, 1,
                                        testRect.y + testRect.height-
                                        beginY));
         protected TreeCellRenderer createDefaultCellRenderer() {
             return new WindowsTreeUI().new WindowsTreeCellRenderer();
    }Cheers,
    Leif Samuelsson
    Java Swing Team
    Sun Microsystems, Inc.

  • JTree lineStyle in XP

    No lines appear between the nodes when testing in XP with windows LAF. I've tried setting
    tree.putClientProperty("JTree.lineStyle", "Angled") ;
    but it has not effect. Any ideas?
    -d

    This is a known bug, and you'll need to apply the fix
    posed at this link.
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=488
    7931Thanks for the info!

  • JTree.linestyle doesn't show with Windows Look&Feel

    Hi guys,
    I've a problem with the JTree.linestyle. In my application works fine with the GTK and the Metal L&F, but in the Windows L&F doesn't shows any lines or angles.
    I'm using j2sdk1.4.2.
    Anyone else have the same problem? Any solution?
    Thank you very much in advance.
    Best Regards,
    Carlos.

    Solved in http://forum.java.sun.com/thread.jsp?forum=57&thread=433899&start=0&range=15#1951313

  • New to CSS, text-align and line-height ok in 'Design', but not in browsers (IE 10 & Chrome) on Win8

    [DREAMWEAVER CC]
    I'm now learning CSS, and find that where I specify in an external style sheet
    h1 {
              font-family:Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", serif;
              text-align:center;
              color: black;
              font-size: 1em;
              font-weight: normal;
              font-style: normal;
              line-height: 3.0;
              letter-spacing: 0px;
              word-spacing: 10px;
    text-shadow: 3px 3px 5px #333;
    text-decoration: none ;
    text-transform: none;
    That, in DW Design, they look fine, but in Windows 8, IE 11 and ChromeVersion 31.0.1650.63 m   that the text-align: center; and the line-height: 3.0;  are ignored.
    I work around by specifying <tr align="center" height = "77">   shouldn't the text-align: center; and the line-height: 3.0;  be specifiable in my CSS??
    Thanks for any help!
    <tr height="77" align="center">
            <h1>
                <td bgcolor="CornSilk">1/28/2014</td>
                <td>the Sky</td>
                <td>Fate is the Hunter</td>
              <td >Ernest Gann</td>
                <td >John</td>
                <td >Jessica</td>
              <td><a href="http://www.amazon.com/FATE-HUNTER-Ernest-K-Gann/dp/0671636030/ref=sr_1_1?s=books&ie=UTF8&q id=1387462831&sr=1-1&keywords=ernest+gann">Learn More</a></td>
            </h1>
        </tr>

    hemmi1 wrote:
    [DREAMWEAVER CC]
    I'm now learning CSS, and find that where I specify in an external style sheet
    Not sure what it is you are trying to do but what you have at the moment is invalid code which is most likely why it doesn't work cross browser. (of cousre this css does not help  - line-height: 3.0; - 3.0 what? - px, ems?)
    Here's an example below of how you could do it. Give your table a class (below it is called .myTable) then you can start styling the <td> cell, and anything inside it by appending the element like an <h2> tag to the class name - .myTable h2
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Untitled Document</title>
    <style>
    .myTable {
        width: 900px;
        margin: 0 auto;
    .myTable td {
    font-family:Baskerville, "Palatino Linotype", Palatino, "Century Schoolbook L", "Times New Roman", serif;
    width: 150px;
    .myTable h2 {
        margin: 0;
        padding: 0;
    text-align:center;
    color: black;
    font-size: 1em;
    line-height: 50px;
    word-spacing: 10px;
    text-shadow: 3px 3px 5px #333;
    .myTable a {
        display: block;
        text-align: center;
    </style>
    </head>
    <body>
    <table class="myTable" cellpadding="0" cellspacing="0" border="0">
    <tr>
    <td><h2>1/28/2014</h2></td>
    <td><h2>the Sky</h2></td>
    <td ><h2>Ernest Gann</h2></td>
    <td ><h2>John</h2></td>
    <td ><h2>Jessica</h2></td>
    <td><a href="http://www.amazon.com/FATE-HUNTER-Ernest-K-Gann/dp/0671636030/ref=sr_1 _1?s=books&ie=UTF8&qid=1387462831&sr=1-1&keywords=ernest+gann">Learn More</a></td>
    </tr>
    </table>
    </body>
    </html>

  • Align Grid Lines in chart

    Hi ......
    I am generating a line chart using an xml file...I am setting
    the interval as 250 on X Axis.....
    And I am using a repeated square image as a background.
    Intention is to have that square aligned inside the grid lines.....
    The problem is that the chart data is dynamic... and when the
    no of plottable values are more , then it takes the grid interval
    automatically as 500 and not 250 as the chart gets displayed in the
    same width.........Due to which the grid squares change and the
    alignment to the image gets disturbed.......
    I need to set the width of the chart based on the number of
    values in the xml in such a way that the grid always comes at 250
    and the image is properly aligned in the grid lines......
    Please help me in this regard....
    Or suggest me some other way to achieve the same.....

    I know that I can control the origin of the rulers, but when I change that, the grid lines do not change, which is quite annoying.
    Depends on which version of Illustrator you are using.
    In recent versions turn on global rulers, then change the rulers origin in order to position the grid.

  • JTree display: vertical connecting lines missing

    My JTree uses a subclass of TreeModel to display the contents of a DOM tree and to allow modifications on this tree. I've got a problem with the rendering of the tree: the vertical connecting lines don't show.. (also there seems to be problems with the refreshing of the tree display, even though TreeModelEvents are fired properly, but it might not be related.)
    The problem originates from the TreeModel subclass (other models worked fine), but I can't tell what exactly in the class causes it. I've tried to remove code, etc.. with no effect.
    Has anyone encountered something similar?
    Thanks in advance.
    Simon

    yeah... that's common in jtrees that do not refresh well... you know that something is wrong when the lines have dissappeared.
    when i encountered this problem what i did was to copy the lines from DefaultTreeModel sourcecode where the model is refreshed into my code... that worked fine for me...

  • Printing double, no alignment of lines

    i have 5610 printer,it is printing out of sync the lines are not in place even the colours are out of the place where it should be like outline and colours are not properly aligned,even changed the cartridge,what to do ,is it a hardware problem,how to fix it.

    Hi,
    Check that if you used table borders in attributes of line types.
    And also check if you are using sy-unline...
    Regards,
    Shankar.

  • JTree: how not to render the "tree connecting lines"

    I have a JTree.
    I would like that my custom tree renderer (using windows LAF) did not paint the dotted line that connect the hierarchy nodes. setting the icon to null in the default renderer only do not paint the folder. How can I disable also the painting of the dotted line?
    using
    tree.putClientProperty("JTree.lineStyle", "None")
    I disable it for the whole tree.
    First of all this do not work on Windows LAF. Then I'd like to disable only on certain branches or nodes, that are somehow as "empty containers", while keeping hte default feature for the "good branches".
    any idea?
    thanks
    Edited by: Davide_Gesino on Nov 8, 2009 5:24 AM

    hii,
    one from possible:
    tree.putClientProperty("JTree.lineStyle", "Horizontal"); a lots examples (runnable) at http://www.java2s.com/Code/Java/Swing-JFC/Tree.htm
    ... kopik

  • How do I get a dotted line to connect nodes in a JTree?

    I am trying to recreate a Windows Explorer application, does anyone know how they get the dotted lines to connect the nodes in the JTree????

    JTree uses a specific line style to represent the edges between nodes. The default is no edges, but we can set JTree�s lineStyle client property so that each parent node appears connected to each of its child nodes by an angled line:
    myJTree.putClientProperty("JTree.lineStyle", "Angled");
    We can also set this property such that each tree cell is separated by a horizontal line:
    myJTree.putClientProperty("JTree.lineStyle", "Horizontal");
    To disable the line style:
    myJTree.putClientProperty("JTree.lineStyle", "None");
    As with any Swing component, we can also change the UI resource defaults used for all instances of the JTree class. For instance, to change the color of the lines used for rendering the edges between nodes as described above, we can modify the entry in the UI defaults table for this resource as follows:
    UIManager.put("Tree.hash", new ColorUIResource(Color.lightGray))
    Hope this serves your purpose.
    Regards,
    Sachin Shanbhag

  • Color of parent/child lines in JTree

    Hi,
    i am using a JTree with a custom CellRenderer. This works quite ok but i also want to change the color from the fine lines between parent and child nodes. Right now they are in the typical "SUN" color but cause of my background i need them to be black.
    node
    |
    |--- child1
    |
    |--- child2
    I mean the lines which i just drawn in ASCII Art, really dont know the english word for it.
    Thx

    i am not weird, i am replying to myself, because perhaps someone might need it afterwards.
    you can change the color of the lines between nodes in JTree, therefore you must go the UIManager way:
    UIManager.put("Tree.hash", new ColorUIResource(Color.lightGray));
    This changes all Trees in your application. You can do similar changes on instance basis:
    myJTree.putClientProperty("JTree.lineStyle", "Angled");
    but i havnt figured out why i cant do this for the "Tree.hash" key on instance basis but anyway. Its ok now...

  • JTree - Color setting of linestyle

    Between the root and the node elements there are lines to show the connection between the element/levels. How can I change the color of these lines and/or how can I hide these lines?

    Example:
    private JTree tree;
    tree.putClientProperty("JTree.lineStyle", "None");

  • Setting line color of JTree's

    Hi,
    I want to set the color of JTree's linestyle which areranch lines. what should i set to get the desired result. i set the linestyle by
    tree.putClientProperty("JTree.lineStyle", "Angled");
    How can i set the linecolor?
    Thnaks,
    Sato

    I got it to work with this:
    UIManager.put( "Tree.line", Color.black ); // for horizontal lines
    UIManager.put( "Tree.hash", Color.black ); // for vertical lines

  • How to set JTree lines invisible?

    Hi all,
    I have tried it with adding following code while GUI is initializing.
    here i'm using a custom TreeCellRenderer. But still line are visible :(
    MyTreeCellRenderer renderer = new MyTreeCellRenderer();
    renderer.setBackgroundSelectionColor(new Color(0xffffff));
    renderer.putClientProperty("JTree.lineStyle", "None");
    setCellRenderer(renderer);How to get the thing done correctly?
    kaushalya

    This client property should be set to the tree, not the renderer.
    Anyway it will only work in Java look and feel (Metal).
    Read the tutorial: [Customizing a Tree's Display|http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display]

  • JTree Button, Line and Java look and feel ?

    Hi all,
    I have 3 small problem with my JTree ..
    1) The 'expand' button size ...
    I set a different Icon for each node in a getTreeCellRendererComponent method (from a class that extends DefaultTreeCellRenderer )...
    The icon are 32x32 pixels.. .and the RowHeight and the Font have been changed too. .
    How to change the size of the expand button (+/-) near the text node ? Because on my JTree, the expand/collapse buttons are really small compare to the icon & row height.
    2) No line appear between the node ...
    I try to set the line style property with -> putClientProperty("JTree.lineStyle", "Angled"); in the JTree constructor OR in the DefaultTreeCellRenderer constructor .. but it doesn't work :/
    3) How to set a Java look and feel for my JTree (actually, I have a Windows look and feel ..)
    I know .. I have grouped 3 question in the same topic ...
    If anyone can help me on one of these subjects. .. Thanks !
    Regards,
    Didier

    So, the Problem 1) is solved ..
    About the Problem 2)
    I put the code UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel()) in the main method, like
      //Main method
      public static void main(String[] args) {
        try {
          UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
        catch(Exception e) {
          e.printStackTrace();
        new MainApplication();
      }But it only set a Java L&F on the spliter, and on the toolbar contained in my application... Any idea why the title bar is not changed too ?
    (The title bar stay with a Window$ XP L&F ...)
    About the problem 3)
    When I set the L&F->UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); then the lines appear .. ??!! .. if I set again the previous value UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); .. then the lines desappear ..

Maybe you are looking for