Results 1 to 2 of 2

Thread: Jtree

  1. #1
    yoga is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Jtree

    //TREE VIEW : 28/01/2011

    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    import javax.swing.border.*; import java.util.Random;
    import javax.swing.colorchooser.*;
    import javax.swing.filechooser.*;
    import javax.accessibility.*;
    import javax.swing.tree.TreePath;
    import java.awt.*;
    import java.awt.event.*;
    import java.beans.*;
    import java.util.*;
    import java.io.*;
    import java.applet.*;
    import java.net.*;
    import javax.swing.tree.*;
    import javax.accessibility.*;
    import javax.swing.plaf.ColorUIResource;
    public class CheckNodeTreeExample extends JFrame {
    JScrollPane sp;
    public CheckNodeTreeExample() {
    super("CheckNode TreeExample");
    String[] strs = {"swing", // 0
    "platf", // 1
    "basic", // 2
    "metal", // 3
    "JTree"}; // 4

    CheckNode[] nodes = new CheckNode[strs.length];
    for (int i=0;i<strs.length;i++) {
    nodes[i] = new CheckNode(strs[i]);
    }
    nodes[0].add(nodes[1]);
    nodes[1].add(nodes[2]);
    nodes[1].add(nodes[3]);
    nodes[0].add(nodes[4]);
    nodes[3].setSelected(true);
    JTree tree = new JTree( nodes[0] );
    tree.setCellRenderer(new CheckRenderer());
    tree.getSelectionModel().setSelectionMode(
    TreeSelectionModel.SINGLE_TREE_SELECTION
    );
    tree.putClientProperty("JTree.lineStyle", "Angled");
    tree.addMouseListener(new NodeSelectionListener(tree));
    sp = new JScrollPane(tree);

    //ModePanel mp = new ModePanel(nodes);
    JTextArea textArea = new JTextArea(3,10);
    //JScrollPane textPanel = new JScrollPane(textArea);
    JButton button = new JButton("print");
    // button.addActionListener(new ButtonActionListener(nodes[0], textArea));
    JPanel panel = new JPanel(new BorderLayout());
    //panel.add(mp, BorderLayout.CENTER);
    // panel.add(button, BorderLayout.SOUTH);

    getContentPane().add(sp, BorderLayout.CENTER);
    getContentPane().add(panel, BorderLayout.EAST);
    //getContentPane().add(textPanel, BorderLayout.SOUTH);
    }

    class NodeSelectionListener extends MouseAdapter {
    JTree tree;
    private Component component;
    Popup popup,popup1,popup2;
    int f;
    NodeSelectionListener(JTree tree) {
    this.tree = tree;
    }

    public void mousePressed(MouseEvent me)
    {
    popup.hide(); //Hide the popup button
    popup1.hide();
    popup2.hide();
    }
    public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    //add(label = new TreeLabel());
    int row = tree.getRowForLocation(x, y);
    TreePath path = tree.getPathForRow(row);
    //TreePath path = tree.getSelectionPath();
    if (path != null) {
    CheckNode node = (CheckNode)path.getLastPathComponent();
    boolean isSelected = ! (node.isSelected());
    node.setSelected(isSelected);
    if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) {
    if ( isSelected) {
    tree.expandPath(path);
    //label.setSelected(false);
    f=1;
    } else {
    tree.collapsePath(path);
    f=0;
    }
    }
    ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
    // I need revalidate if node is root. but why?
    if (row == 0) {
    tree.revalidate();
    tree.repaint();
    }
    }
    }


    public void mouseReleased(MouseEvent me)
    {
    if(f==1){
    JLabel button = new JLabel("Sample");
    button.setBorder(LineBorder.createGrayLineBorder() );
    JButton button1 = new JButton(new ImageIcon( "1.gif" ));
    JButton button2 = new JButton(new ImageIcon( "2.gif" ));
    button1.setPreferredSize(new Dimension(30, 25));
    button2.setPreferredSize(new Dimension(30, 25));
    button.setOpaque(true);
    button.setBackground(Color.white);
    TreePath tp = tree.getPathForLocation(me.getX(), me.getY()); //grtting the tree path of current mouse pointer location
    PopupFactory factory = PopupFactory.getSharedInstance(); //creating popup button
    //int x = random.nextInt(2000);
    //int y = random.nextInt(2000);
    if (tp != null)
    {
    popup = factory.getPopup( component,button,me.getX()+40, me.getY()+60); //place the button popup at given location
    popup1 = factory.getPopup( component,button1,me.getX()+50, me.getY()+100);
    popup2 = factory.getPopup( component,button2,me.getX()+80, me.getY()+100);
    popup.show();
    popup1.show();
    popup2.show(); // show the popup button on screen
    //System.out.println(tp.toString());
    }}
    }




    }
    public static void main(String args[]) {
    try {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf. windows.WindowsLookAndFeel");
    } catch (Exception evt) {}

    CheckNodeTreeExample frame = new CheckNodeTreeExample();
    frame.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    frame.setSize(300, 200);
    frame.setVisible(true);
    }
    }

    class CheckRenderer extends JPanel implements TreeCellRenderer {
    protected JCheckBox check;

    protected TreeLabel label;

    public CheckRenderer() {
    setLayout(null);
    add(check = new JCheckBox());
    add(label = new TreeLabel());
    check.setBackground(UIManager.getColor("Tree.textB ackground"));
    label.setForeground(UIManager.getColor("Tree.textF oreground"));
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean isSelected, boolean expanded, boolean leaf, int row,
    boolean hasFocus) {
    String stringValue = tree.convertValueToText(value, isSelected,
    expanded, leaf, row, hasFocus);
    setEnabled(tree.isEnabled());
    check.setSelected(((CheckNode) value).isSelected());
    label.setFont(tree.getFont());
    label.setText(stringValue);
    label.setSelected(isSelected);
    label.setFocus(hasFocus);
    if (leaf) {
    label.setIcon(UIManager.getIcon("Tree.leafIcon"));
    } else if (expanded) {
    label.setIcon(UIManager.getIcon("Tree.openIcon"));
    } else {
    label.setIcon(UIManager.getIcon("Tree.closedIcon") );
    }
    return this;
    }

    public Dimension getPreferredSize() {
    Dimension d_check = check.getPreferredSize();
    Dimension d_label = label.getPreferredSize();
    return new Dimension(d_check.width + d_label.width,
    (d_check.height < d_label.height ? d_label.height
    : d_check.height));
    }

    public void doLayout() {
    Dimension d_check = check.getPreferredSize();
    Dimension d_label = label.getPreferredSize();
    int y_check = 0;
    int y_label = 0;
    if (d_check.height < d_label.height) {
    y_check = (d_label.height - d_check.height) / 2;
    } else {
    y_label = (d_check.height - d_label.height) / 2;
    }
    check.setLocation(0, y_check);
    check.setBounds(0, y_check, d_check.width, d_check.height);
    label.setLocation(d_check.width, y_label);
    label.setBounds(d_check.width, y_label, d_label.width, d_label.height);
    }

    public void setBackground(Color color) {
    if (color instanceof ColorUIResource)
    color = null;
    super.setBackground(color);
    }

    public class TreeLabel extends JLabel {
    boolean isSelected;

    boolean hasFocus;

    public TreeLabel() {
    }

    public void setBackground(Color color) {
    if (color instanceof ColorUIResource)
    color = null;
    super.setBackground(color);
    }

    public void paint(Graphics g) {
    String str;
    if ((str = getText()) != null) {
    if (0 < str.length()) {
    if (isSelected) {
    g.setColor(UIManager
    .getColor("Tree.selectionBackground"));
    } else {
    g.setColor(UIManager.getColor("Tree.textBackground "));
    }
    Dimension d = getPreferredSize();
    int imageOffset = 0;
    Icon currentI = getIcon();
    if (currentI != null) {
    imageOffset = currentI.getIconWidth()
    + Math.max(0, getIconTextGap() - 1);
    }
    g.fillRect(imageOffset, 0, d.width - 1 - imageOffset,
    d.height);
    if (hasFocus) {
    g.setColor(UIManager
    .getColor("Tree.selectionBorderColor"));
    g.drawRect(imageOffset, 0, d.width - 1 - imageOffset,
    d.height - 1);
    }
    }
    }
    super.paint(g);
    }

    public Dimension getPreferredSize() {
    Dimension retDimension = super.getPreferredSize();
    if (retDimension != null) {
    retDimension = new Dimension(retDimension.width + 3,
    retDimension.height);
    }
    return retDimension;
    }

    public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;
    }

    public void setFocus(boolean hasFocus) {
    this.hasFocus = hasFocus;
    }
    }
    }

    class CheckNode extends DefaultMutableTreeNode {

    public final static int SINGLE_SELECTION = 0;

    public final static int DIG_IN_SELECTION = 4;

    protected int selectionMode;

    protected boolean isSelected;

    public CheckNode() {
    this(null);
    }

    public CheckNode(Object userObject) {
    this(userObject, true, false);
    }

    public CheckNode(Object userObject, boolean allowsChildren,
    boolean isSelected) {
    super(userObject, allowsChildren);
    this.isSelected = isSelected;
    setSelectionMode(DIG_IN_SELECTION);
    }

    public void setSelectionMode(int mode) {
    selectionMode = mode;
    }

    public int getSelectionMode() {
    return selectionMode;
    }

    public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;

    if ((selectionMode == DIG_IN_SELECTION) && (children != null)) {
    Enumeration e = children.elements();
    while (e.hasMoreElements()) {
    CheckNode node = (CheckNode) e.nextElement();
    node.setSelected(isSelected);
    }
    }
    }

    public boolean isSelected() {
    return isSelected;
    }

    // If you want to change "isSelected" by CellEditor,
    /*
    public void setUserObject(Object obj) { if (obj instanceof Boolean) {
    * setSelected(((Boolean)obj).booleanValue()); } else {
    * super.setUserObject(obj); } }
    */

    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. JTree error.. is it a bug?
    By 00jt in forum Advanced Java
    Replies: 6
    Last Post: 06-16-2010, 03:34 PM
  2. Jtree Help
    By miladirooni in forum New To Java
    Replies: 1
    Last Post: 10-28-2009, 12:00 AM
  3. zip to JTree
    By icsbcn in forum AWT / Swing
    Replies: 8
    Last Post: 09-01-2009, 04:26 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. JTree Problem
    By ngpamela in forum XML
    Replies: 0
    Last Post: 12-10-2008, 04:10 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •