3

I was wondering if there was any way to use word wrapping with JTrees. I am using HTML in the node's text, I'm not sure if that's important or not.

screenshot

  • Can you post your code on what you tried? – Beniton Jun 13 '16 at 11:30
  • I have no idea of what I can try now unfortunately. I had thought of replacing the cell renderer by a JLabel, but that would do anything since the default one already is one. –  Jun 13 '16 at 11:32
  • 1
    That's tricker than I thought, +1, I'd also like to see a nice solution for this. It's fiddly to get the heights of the (wrapped!) rows right, and to still assign a proper width to each renderer component. – Marco13 Jun 13 '16 at 14:26

1 Answers1

3

Ok you can try like below.

        JTree tree = new JTree();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Animals");
        DefaultTreeModel model = new DefaultTreeModel(root);
        tree.setModel(model);
        root.add(new DefaultMutableTreeNode(new Animal("Dog","ACS")));
        root.add(new DefaultMutableTreeNode(new Animal("Cat","BCS")));
        root.add(new DefaultMutableTreeNode(new Animal("Lion","FCS")));
        root.add(new DefaultMutableTreeNode(new Animal("Tiger","<html>Hello World!<br>blahblahblah</html>")));

        DefaultTreeSelectionModel sModel = new DefaultTreeSelectionModel(); 
        sModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.setSelectionModel(sModel);
        tree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override
            public void valueChanged(TreeSelectionEvent selection) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)selection.getPath().getLastPathComponent();
                if(selectedNode.isLeaf()){
                    Animal animal = (Animal)selectedNode.getUserObject();
                }
            }
        });
        tree.setCellRenderer( new DefaultTreeCellRenderer(){

            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                    boolean leaf, int row, boolean hasFocus) {
                if(value != null ){
                    DefaultMutableTreeNode node =  (DefaultMutableTreeNode)value;
                    if(node.isLeaf()){
                        Animal animal = (Animal)((DefaultMutableTreeNode)value).getUserObject();

                        this.setText(animal.name);
                    }else {
                        return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                    }
                } 
                return this;
            }

        });

This gives me below output.

enter image description here

Hope it help you.

For auto wrap the content you can use html

Please refer to Andrew's answer in this link

Community
  • 1
  • 1
Beniton
  • 1,503
  • 1
  • 12
  • 21
  • Thank you, but this is not the problem i was thinking about, I was thinking about this : http://imgur.com/UhC8AKg see the text is cut off –  Jun 13 '16 at 12:25
  • @utybo Do you see the _Hello World! blahblahblah_ it is an html. You can use html to wrap you long content. – Beniton Jun 13 '16 at 12:35
  • I did see that, but you are explicitly making a line break, which is not what i would like to do. I would like to have a way to wordwrap like with JTextArea's setLineWrap(boolean) for example –  Jun 13 '16 at 12:42
  • 1
    @utybo It would auto wrap if you use you html like this _"

    Hello World! asdasd asd asd asd as da das. das da sd asd asd asd asdasd asd asd, asd as blahblahblah

    "_
    – Beniton Jun 13 '16 at 12:51
  • Well, it would work but it is not really convenient since the JTree would be resized by the user. It would be a pretty "bad" word wrap in my opinion, since word wrapping should theorically be done depending on the width of the component. It is already done by default by JLabels and pretty much any component, but not in JTrees for some reason. –  Jun 13 '16 at 12:54
  • @utybo The RenderComponent is JLabel so as per your comment it should be already working. Agian the width can be modified based on the resize of the renderer component. So it is possible. – Beniton Jun 13 '16 at 12:59
  • 1
    The thing is it (the simple way with just ) doesn't work... so i guess i have to make a complicated system to detect the width of the label (since the label width with getWidth would be incorrect) and apply it to the html... imo it's kind of too complicated for something like that. I guess i'll use something else than JTrees for my current project, but i still wonder if there is a clean, simple solution to this –  Jun 13 '16 at 13:04
  • @utybo Sure i will update my answer if i come across some other solution. – Beniton Jun 13 '16 at 13:09