Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Jtree help

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Lightbulb Jtree help

    I have a problem with my Jtree.....I can add nodes to my JTree, but after expanding a node, I can not add any child nodes to that node

    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.event.*;
    import java.awt.event.*;
    import javax.swing.tree.*;
    import java.awt.Color;
    public class GUIMP extends JFrame implements ActionListener {
    	String Sname="";
    	String Semail="";
    	int iConNum=0;
    	int id=0;
    //icons& images
    ImageIcon mid;
    ImageIcon image;
    ImageIcon del;
    ImageIcon edi;
    ImageIcon exi;
    ImageIcon cle;
    ImageIcon bg1;
    JLabel bgd;
    ImageIcon bg2;
    JLabel bgd2;
    JLabel git;
    //components
    JPanel panel1=new JPanel();
    JPanel panel2= new JPanel();
    JFrame frame2=new JFrame();
    JButton Add;
    JButton Delete;
    JButton Edit;
    JButton Exit;
    JButton Clear;
    JLabel label;
    JTextField name;
    JLabel label2;
    JTextField email;
    JLabel label3;
    JTextField ConNum;
    //Components for frame 2
    JLabel lab;
    JTextField nam;
    JLabel lab2;
    JTextField ema;
    JLabel lab3;
    JTextField num;
    JButton sub;
    JButton can;
    //Jtree
    JTree tree ;
    DefaultMutableTreeNode parent;   
     
        public GUIMP() {
        	//settings
        	//frame2
        	 frame2.setTitle("Adding Info");
        	 frame2.setLocation(325,350);
             frame2.setSize(372, 144);
             //main frame
        	 setTitle("Phonebook Tree");
        	 setLocation(250,200);
             setSize(500,446);
             setDefaultCloseOperation(3);
             setLayout(null);
             frame2.setLayout(null);
             panel1.setLayout(null);
             panel2.setLayout(null);
             panel2.setSize(500,400);
     
              //splitpane
             JSplitPane splitPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, panel1,panel2 ); 
             splitPane.setResizeWeight(0.5);
             splitPane.setContinuousLayout(true);
             splitPane.setOneTouchExpandable(true);
             splitPane.setBounds(0,0,500,1000);
             add(splitPane);
     
             //Buttons
            image=new ImageIcon("add.jpg");
            Add=new JButton(image);
            Add.setBounds(5,5,45,50);
            panel1.add(Add);
            Add.addActionListener(this);
     
            del=new ImageIcon("delete.png");
            Delete=new JButton(del);
            Delete.setBounds(80,5,45,50);
            panel1.add(Delete);
            Delete.addActionListener(this);
     
            edi=new ImageIcon("edit.jpg");
            Edit=new JButton(edi);
            Edit.setBounds(160,5,45,50);
            panel1.add(Edit);
            Edit.addActionListener(this);
     
            cle=new ImageIcon("clear.png");
            Clear=new JButton(cle);
            Clear.setBounds(0,370,65,35);
            panel2.add(Clear);
            Clear.addActionListener(this);
     
            exi=new ImageIcon("exit.gif");
            Exit=new JButton(exi);
            Exit.setBounds(150,370,85,35);
            panel2.add(Exit);
            Exit.addActionListener(this);
     
            //Buttons for Frame 2
            sub=new JButton("Submit");
            sub.setBounds(265,25,100,25);
            sub.addActionListener(this);
            frame2.add(sub);
     
            can=new JButton("Cancel");
            can.setBounds(265,75,100,25);
            can.addActionListener(this);
            frame2.add(can);
     
            //Jtree
            parent= new DefaultMutableTreeNode("Digital Phonbook Contacts", true);
            tree= new JTree(parent);
     
            tree.setBounds(0,80,400,200);
            //------------makes Jtree transparent---------------
            //code snippet from [url=http://java.itags.org/java-core-gui-apis/40693/]Java Core GUI APIs: How to make JTree transparent? - java.itags.org[/url]
            tree.setOpaque(false);
            tree.setCellRenderer(new DefaultTreeCellRenderer(){
            public Component getTreeCellRendererComponent(JTree tree,Object value,
            boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus){
            super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
            this.setBackgroundSelectionColor(null);
            this.setBorderSelectionColor(null);
            this.setBackgroundNonSelectionColor(new Color(0,0,0,0));
            return this;
             }
              });
            //-------------------------------------------------
            panel1.add(tree);
     
     
     
             //TextFields and Labels
             label=new JLabel("Name: ");
             label.setForeground(Color.white);
             label.setBounds(50,5,100,25);
             panel2.add(label);
     
             name=new JTextField();
              name.setBackground(Color.YELLOW);
             name.setBounds(105,10,100,25);
             panel2.add(name);
             name.setEditable(false);
     
             label2=new JLabel("Email:");
             label2.setForeground(Color.white);
             label2.setBounds(50,50,100,25);
             panel2.add(label2);
     
             email=new JTextField();
             email.setBounds(105,50,100,25);
             email.setBackground(Color.YELLOW);
             panel2.add(email);
             email.setEditable(false);
     
             label3=new JLabel("Contact Number:");
             label3.setForeground(Color.white);
             label3.setBounds(5,85,100,25);
             panel2.add(label3);
     
             ConNum=new JTextField();
             ConNum.setBackground(Color.YELLOW);
             ConNum.setBounds(105,85,100,25);
             ConNum.setEditable(false);
             panel2.add(ConNum);
     
             //background
             bg1=new ImageIcon("night.jpg");
             bgd=new JLabel(bg1);
             bgd.setBounds(0,0,257,410);
             panel1.add(bgd);
     
             bg2=new ImageIcon("day.jpg");
             bgd2=new JLabel(bg2);
             bgd2.setBounds(0,0,257,411);
             panel2.add(bgd2);
     
     
     
     
             //Textfields and labels for frame 2
             lab=new JLabel("Name:");
             lab.setBounds (55, 5, 100, 25);
             frame2.add(lab);
     
             nam=new JTextField();
             nam.setBounds (110, 10, 100, 25);
             frame2.add(nam);
     
             lab2=new JLabel("Email:");
             lab2.setBounds (55, 45, 100, 25);
             frame2.add(lab2);
     
             ema=new JTextField();
             ema.setBounds (110, 45, 100, 25);
             frame2.add(ema);
     
             lab3=new JLabel("Contact Number:");
             lab3.setBounds (5, 80, 100, 25);
             frame2.add(lab3);
     
             num= new JTextField();
             num.setBounds (110, 80, 100, 25);
             frame2.add(num);
     
             mid=new ImageIcon("mid.jpg");
             git=new JLabel(mid);
             git.setBounds(0,0,590,189);
             frame2.add(git);
     
     
     
     
     
             //----------------------------
             setVisible(true);
     
     
     
     
        }
     
     
        public void actionPerformed(ActionEvent a)
        	{
        		if(a.getSource()==can){
        			frame2.setVisible(false);
        		}
        		if(a.getSource()==Delete){
     
     
        		}
        		if(a.getSource()==Add){
        			frame2.setVisible(true);
        		}
     
        		if(a.getSource()==Exit){
    			dispose();
    		}
     
    			if(a.getSource()==sub){
    		Sname=nam.getText();
            Semail=ema.getText();
            try
            {
                iConNum=Integer.parseInt(""+num.getText());
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, "Please enter Phone Number");
            }
            if(Sname.equals(""))
            {
                JOptionPane.showMessageDialog(null, "Please Enter Name");
            }
            else
            {
             Contact person=new Contact(Sname, Semail, iConNum);
             Vector<String> bla=new Vector<String>();
             DefaultMutableTreeNode category = null;
             category = new DefaultMutableTreeNode("Contact"+Contact.getID());
             JOptionPane.showMessageDialog(null, "Person Saved");
             parent.add(category);
             DefaultMutableTreeNode name= new DefaultMutableTreeNode(nam.getText());
             DefaultMutableTreeNode email = new DefaultMutableTreeNode(ema.getText());
             DefaultMutableTreeNode number = new DefaultMutableTreeNode(num.getText());
             category.add(name);
             category.add(email);
             category.add(number);
        }
     
    			}
     
     
        	}
     
         public static void main(String[]args)
            {
                GUIMP var=new GUIMP();
                var.setVisible(true);
     
        }
     
     
    }
    Please Help..Thank You!
    Last edited by sman36; December 4th, 2010 at 04:49 AM. Reason: Change questiom


  2. #2
    Junior Member
    Join Date
    Aug 2010
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Jtree help

    just found the solution...I should have use the metod insertnodeinto

Similar Threads

  1. How to show empty directories in JTree ?
    By ni4ni in forum AWT / Java Swing
    Replies: 1
    Last Post: April 30th, 2010, 01:55 AM
  2. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 04:15 AM