2

I am trying to render a JTree leaf to have a JPanel containing an editable JTextArea so that a user can type in text and be able to scroll down as they adding text,

and also using a wrapper to limit the text in width so it stretches down to allow more space rather scrolling left and right.

I have implemented the following so far but it appears weird, when I run the code, as if there are two leafs, the default one and the rendered on. Any type of help is highly appreciated.

I just want to know if what I am want to do is achievable and if not, where can I modify.

I don't want to change the icons, just the leaf only.

import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author Razaractor
 */
class LeafRenderer extends AbstractCellEditor implements
        TreeCellRenderer, TreeCellEditor {

    //Text area to be place on JPanel
    JTextArea text = new JTextArea();
    DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

    @Override
    public Object getCellEditorValue() {
        return text.getText();
    }

    @Override
    public boolean isCellEditable(final EventObject event) {
        return true;
    }

    @Override
    public Component getTreeCellRendererComponent(
            final JTree tree, final Object value,
            final boolean selected,
            final boolean expanded,
            final boolean leaf,
            final int row,
            final boolean hasFocus) {
        //Default leaf
        Component render = nonLeafRenderer.getTreeCellRendererComponent(tree,
                value, selected, expanded, leaf, row, hasFocus);
        if (selected && leaf) {
            final JPanel panel = new JPanel();
            BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
            panel.setLayout(layout);
            text.setColumns(20);
            text.setLineWrap(true);
            text.setRows(5);
            text.setWrapStyleWord(true);
            text = new JTextArea(String.valueOf(value));
            text.addComponentListener(new ComponentListener() {

                @Override
                public void componentResized(ComponentEvent e) {
                    panel.setSize(panel.getPreferredSize());
                }

                @Override
                public void componentShown(ComponentEvent e) {
                }

                @Override
                public void componentMoved(ComponentEvent e) {
                }

                @Override
                public void componentHidden(ComponentEvent e) {
                }
            });
            JScrollPane scroll = new JScrollPane(text);
            panel.add(scroll);
            render = panel;
            return panel;
        }
        return render;
    }

    @Override
    public Component getTreeCellEditorComponent(
            final JTree tree,
            final Object value,
            final boolean isSelected,
            final boolean expanded,
            final boolean leaf,
            final int row) {
        Component editor = nonLeafRenderer.getTreeCellRendererComponent(tree,
                value, true, expanded, leaf, row, true);
        final JPanel p = new JPanel();
        if (editor instanceof JPanel) {
            editor = (JPanel) editor;
        }
        return editor;
    }

    public void itemStateChanged(ItemEvent itemEvent) {
        if (stopCellEditing()) {
            fireEditingStopped();
        }
    }
}

enter image description here

enter image description here

Raymond Nakampe
  • 371
  • 7
  • 22
  • 1
    there are three choices 1) post an question about JTree, then you can continue with Renderer an Editor, 2) post an [SSCCE](http://sscce.org/) demonstrated your issues 3) let it be, and then we too, but maybe bolt of thunder :-) – mKorbel Sep 09 '12 at 20:27
  • 1
    @ Keppil, thanx for the advise, I just managed to post it. @ mKorbel, I will go with option 2. :^D – Raymond Nakampe Sep 09 '12 at 21:09
  • See also [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513), which implements `TreeCellEditor`. – trashgod Sep 10 '12 at 03:46

1 Answers1

0

I don't know what you realy want to achieve but try these changes.

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.util.EventObject;
import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 * @author Razaractor
 */
class LeafRenderer extends AbstractCellEditor implements
        TreeCellRenderer, TreeCellEditor {

    //Text area to be place on JPanel
    JTextArea text = new JTextArea();
    DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();

    @Override
    public Object getCellEditorValue() {
        return text.getText();
    }

    @Override
    public boolean isCellEditable(final EventObject event) {
        return true;
    }

    @Override
    public Component getTreeCellRendererComponent(
            final JTree tree, final Object value,
            final boolean selected,
            final boolean expanded,
            final boolean leaf,
            final int row,
            final boolean hasFocus) {
        //Default leaf
        Component render = nonLeafRenderer.getTreeCellRendererComponent(tree,
                value, selected, expanded, leaf, row, hasFocus);
        if (selected && leaf) {
            final JPanel panel = new JPanel();
            BoxLayout layout = new BoxLayout(panel, BoxLayout.X_AXIS);
            panel.setLayout(layout);
            //Changed code
            //moved from
            text = new JTextArea(String.valueOf(value));
            text.setColumns(20);
            text.setLineWrap(true);
            text.setRows(5);
            text.setWrapStyleWord(true);
            //here. You were creating just ordinary JTextArea.
            text.addComponentListener(new ComponentListener() {

                @Override
                public void componentResized(ComponentEvent e) {
                    panel.setSize(panel.getPreferredSize());
                }

                @Override
                public void componentShown(ComponentEvent e) {
                }

                @Override
                public void componentMoved(ComponentEvent e) {
                }

                @Override
                public void componentHidden(ComponentEvent e) {
                }
            });
            JScrollPane scroll = new JScrollPane(text);
            //changed Code
            //if you want to hide scrollbars uncomment one of these
            //scroll.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
            //scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 0));
            panel.add(scroll);
            render = panel;
            return panel;
        }
        return render;
    }

    @Override
    public Component getTreeCellEditorComponent(
            final JTree tree,
            final Object value,
            final boolean isSelected,
            final boolean expanded,
            final boolean leaf,
            final int row) {
        Component editor = nonLeafRenderer.getTreeCellRendererComponent(tree,
                value, true, expanded, leaf, row, true);
        final JPanel p = new JPanel();
        if (editor instanceof JPanel) {
            editor = (JPanel) editor;
        }
        return editor;
    }

    public void itemStateChanged(ItemEvent itemEvent) {
        if (stopCellEditing()) {
            fireEditingStopped();
        }
    }
}
Riso
  • 338
  • 4
  • 11
  • My aim is to have a JPanel with a JTextArea replace the default editable leaf which JTree has. So I am not sure that what I am implementing is actually what is needed to achieve what I want. And thanx for the scroll bar correction it now has it. I want to return a JPanel containing the JTextArea to allow user to scroll up and down restricting the text by ending lines so it wont be long horizontal lines. – Raymond Nakampe Sep 09 '12 at 23:21
  • I just managed to get rid of the default leaf look,I removed the selected condition and now have leaf only.Now I have what i want the leaf to like, like in the second picture I posted, I am having problems with having to type in text in the JTextArea so it end lines by using the wrapper but it still uses the default editor inputting style i.e it doesn't end lines. How can I go about that ? Thank you – Raymond Nakampe Sep 10 '12 at 09:51
  • 1
    Try to set your JTree editable (jtree.setEditable(true)) and JTextArea in your panel focusable (text.setFocusable(true)) – Riso Sep 10 '12 at 10:34
  • I already have the JTree set to editable, i tried the setFocusable(true) but its still not responding, I shall maybe try requestFocus(), thanx Riso – Raymond Nakampe Sep 10 '12 at 12:31