Example usage for javax.swing JTree getCellRenderer

List of usage examples for javax.swing JTree getCellRenderer

Introduction

In this page you can find the example usage for javax.swing JTree getCellRenderer.

Prototype

public TreeCellRenderer getCellRenderer() 

Source Link

Document

Returns the current TreeCellRenderer that is rendering each cell.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTree tree = new JTree();
    tree.setEditable(true);/*  w ww . j a v  a  2s.  c om*/

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    DefaultCellEditor comboEditor = new DefaultCellEditor(new JTextField());

    comboEditor.getCellEditorValue();

    TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:EditLeafSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Editable Tree");
    JTree tree = new JTree();
    tree.setEditable(true);/*from  w  w w.j  a va  2 s  .c  o m*/

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    TreeCellEditor editor = new LeafCellEditor(tree, renderer);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TreeNodeVector.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("JTreeSample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Vector<String> v1 = new TreeNodeVector<String>("Two", new String[] { "Mercury", "Venus", "Mars" });
    Vector<Object> v2 = new TreeNodeVector<Object>("Three");
    v2.add(System.getProperties());
    v2.add(v1);//from  w w  w.  j  av a  2s .c  o  m
    Object rootNodes[] = { v1, v2 };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(renderer.getBackgroundNonSelectionColor());
    renderer.setBackgroundNonSelectionColor(backgroundSelection);

    Color textSelection = renderer.getTextSelectionColor();
    renderer.setTextSelectionColor(renderer.getTextNonSelectionColor());
    renderer.setTextNonSelectionColor(textSelection);

    frame.add(new JScrollPane(tree), BorderLayout.CENTER);

    frame.setSize(300, 300);
    frame.setVisible(true);

}

From source file:TreeEdit.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Object array[] = { Boolean.TRUE, Boolean.FALSE, "Hello" };
    JTree tree = new JTree(array);
    tree.setEditable(true);/*from w w  w.j  a  v  a  2s  .  c o  m*/
    tree.setRootVisible(true);

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    String elements[] = { "A", "B", "C", "D" };
    JComboBox comboBox = new JComboBox(elements);
    comboBox.setEditable(true);
    TreeCellEditor comboEditor = new DefaultCellEditor(comboBox);
    TreeCellEditor editor = new DefaultTreeCellEditor(tree, renderer, comboEditor);
    tree.setCellEditor(editor);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public FileTree(File dir) {
    setLayout(new BorderLayout());
    JTree tree = new JTree(addNodes(null, dir));
    tree.addTreeSelectionListener(e -> {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
        System.out.println("You selected " + node);
    });/*from www .j  ava  2 s  .  com*/
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    JScrollPane scrollpane = new JScrollPane();
    scrollpane.getViewport().add(tree);
    add(BorderLayout.CENTER, scrollpane);
}

From source file:org.eclipse.jubula.rc.swing.tester.util.TreeOperationContext.java

/**
 * {@inheritDoc}/*from   ww w .  j  a  v a2  s .  c om*/
 */
public String getRenderedText(final Object node) {
    return (String) getQueuer().invokeAndWait("getRenderedText", new IRunnable() { //$NON-NLS-1$
        public Object run() {
            int row = getRowForTreeNode(node);
            JTree tree = (JTree) getTree();
            Component cellRendererComponent = tree.getCellRenderer().getTreeCellRendererComponent(tree, node,
                    false, tree.isExpanded(row), m_model.isLeaf(node), row, false);
            try {
                return TesterUtil.getRenderedText(cellRendererComponent);
            } catch (StepExecutionException e) {
                // This is a valid case in JTrees since if there is no text
                // there is also no renderer 
                log.warn("Renderer not supported: " + //$NON-NLS-1$
                cellRendererComponent.getClass(), e);
                return null;
            }
        }
    });
}