Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hide horizontal and vertical lines in a JTree

843806Oct 23 2007 — edited Oct 24 2007
Hi,

suppose we have three JTrees in a Windows L&F where the second one shall not show any vertical or horizontal lines for a node. If this restriction would be true for all three JTrees one could invoke
UIManager.put("Tree.paintLines", Boolean.FALSE)
.

However only the second one must not show any lines. I tried something like
tree.putClientProperty("Tree.paintLines", Boolean.FALSE);
tree.updateUI();
But unfortunately this does not work as the lines are still shown. Furthermore we need to set "on/off" the lines dynamically dependend on the user data.

Does anyone have a clue to solve this problem?

Thx.

Comments

camickr
-> Does anyone have a clue to solve this problem?

Take a look at the source code to see what that particular property does. Then maybe you will be able to override the UI to provide an on/off switch at a table level somehow.
794342
set your own UI, overriding these (to do nothing)
paintHorizontalLine(..)
paintVerticalLine(..)
795537
Try with
tree.putClientProperty("JTree.lineStyle", "None");
Bye.
843806
JTree.lineStyle
only works on metal l&f.

To override the UI should work but it is very ugly to set the ui each time the user changed the data. Beside this imagine your code will be excuted on a windows, linux and mac os.

Do I really have to create my own UI for each l&f just to hide the lines for a node? Is there no other way?
794342
only tested on windows, but this seems simple enough
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
class Testing
{
  boolean showLines = false;
  public void buildGUI()
  {
    JButton btn = new JButton("Show/Hide Lines");
    final JTree tree = new JTree();
    tree.setUI(new javax.swing.plaf.basic.BasicTreeUI(){
      protected void paintHorizontalLine(Graphics g,JComponent c,int y,int left,int right){
        if(showLines) super.paintHorizontalLine(g,c,y,left,right);
      }
      protected void paintVerticalLine(Graphics g,JComponent c,int x,int top,int bottom){
        if(showLines) super.paintVerticalLine(g,c,x,top,bottom);
      }
    });
    JFrame f = new JFrame();
    f.getContentPane().add(new JScrollPane(tree),BorderLayout.CENTER);
    f.getContentPane().add(btn,BorderLayout.SOUTH);
    f.setSize(200,200);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    btn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
        showLines = !showLines;
        tree.repaint();
      }
    });
  }
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        try{UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");}catch(Exception e){}
        new Testing().buildGUI();
      }
    });
  }
}
1 - 5
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Nov 21 2007
Added on Oct 23 2007
5 comments
2,852 views