Results 1 to 3 of 3
  1. #1
    bibingigin is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Post Swing tree java code

    My java code is as follows
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    public class jtrexp extends JFrame
    {
    JTree jt;
    DefaultMutableTreeNode top,ff,df,ff1,ff2,ff3,df1,df2,df3;
    JTextField f1;
    	public jtrexp()
    	{
    		
    		setLayout(new BorderLayout());		
    		top=new DefaultMutableTreeNode("Fruits");
    		ff=new DefaultMutableTreeNode("Fresh Fruits");
    		df=new DefaultMutableTreeNode("Dry Fruits");
    		ff1=new DefaultMutableTreeNode("Apple");
    		ff2=new DefaultMutableTreeNode("Orange");
    		ff3=new DefaultMutableTreeNode("Grapes");
    		df1=new DefaultMutableTreeNode("Strawberry");
    		df2=new DefaultMutableTreeNode("Dates");
    		df3=new DefaultMutableTreeNode("Beaches");
    		ff.add(ff1);
    		ff.add(ff2);
    		ff.add(ff3);
    		df.add(df1);
    		df.add(df2);
    		df.add(df3);
    		top.add(ff);
    		top.add(df);
    		jt=new JTree(top);
    		jt.setFont(new Font("Times New Roman",Font.BOLD,20));
    		jt.setRowHeight(40);
    		add(jt);
    		f1=new JTextField(20);
    		add(f1,"south");
    		jt.addMouseListener(new MouseAdapter()
    		{
    			public void mouseClicked(MouseEvent me)
    			{
    			TreePath tp=jt.getPathForLocation(me.getX(),me.getY());
    			if(tp!=null)
    				f1.setText(tp.toString());
    			else
    				f1.setText("");
    		
    			}
    		});
    		
    
    	}
    	public static void main(String arg[])
    	{
    		jtrexp jj=new jtrexp();
    		jj.setSize(400,400);
    		jj.setVisible(true);
    	
    	}
    }
    When i run the code it shows,

    Java Code:
    Exception in thread "main" java.lang.ErrorDo not use jtrexp.setLayout()  use jtrexp.setLayout().getContentPane().setLayout() instead
             at javax.swing.JFrame.createRootPaneException(JFrame.java.406)
             at javax.swing.JFrame.setLayout(JFrame.java.472)
             at jtrexp.<init>(jtrexp.java.13)
             at jtrexp.main(jtrexp.java.54)
    Last edited by DarrylBurke; 02-10-2012 at 07:10 AM. Reason: Added code tags

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Swing tree java code

    Use the code tags to post code. Since you're new here, I've added them for you this time round.

    What's your Java version? and how exactly are you running the code?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Swing tree java code

    And if you're running GNU Java aka GCJ, get rid of all traces of it from your computer and install a Sun/Oracle JDK.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Error displayed during run in swing tree pgm
    By Bibin in forum New To Java
    Replies: 3
    Last Post: 12-24-2011, 10:39 PM
  2. Help me on this code of Java swing
    By gauravfzd in forum New To Java
    Replies: 16
    Last Post: 10-16-2010, 05:27 AM
  3. Tree controls using Swing
    By kabir in forum AWT / Swing
    Replies: 1
    Last Post: 01-05-2008, 09:48 AM
  4. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM

Posting Permissions

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