Results 1 to 4 of 4
  1. #1
    Bibin is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Post Error displayed during run in swing tree pgm

    The error is :
    Exceptionin thread "main" java.lang.Error: Do not use jtrexp.setLayout() use )jtrexp.geContentPane().setLayout() instead
    atjavax.swing.JFrame.createRootPaneException(JFram e.java:406)

    atjavax.swing.JFrame.setLayout(JFrame.java:472)
    at jtrexp.<init>(jtrexp.java.13)
    at jtrexp.main(jtrexp.java:54)

    MY code is:

    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);
    	
    	}
    }
    Last edited by pbrockway2; 12-24-2011 at 10:41 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error displayed during run in swing tree pgm

    When posting code use the code tags, Put [code] at the start of the code and [/code] at the end so that the formatting is preserved.

    -----

    The error message is quite straight forward as regards how you should change the code. What don't you understand about what to change? To see why it occurs you should read a textbook or tutorial. Oracle have an online tutorial which explains the point at Using Top-Level Containers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    -----

    Also, Java clases should begin with a capital letter.

  3. #3
    Bibin is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Error displayed during run in swing tree pgm

    thank you...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Error displayed during run in swing tree pgm

    You're welcome.

Similar Threads

  1. Updating text displayed in a Swing Application
    By Fubarable in forum AWT / Swing
    Replies: 1
    Last Post: 09-16-2011, 06:31 AM
  2. Swing components are not well-displayed
    By Josep_16 in forum AWT / Swing
    Replies: 1
    Last Post: 08-21-2011, 03:37 AM
  3. Replies: 4
    Last Post: 04-13-2011, 04:35 AM
  4. Replies: 0
    Last Post: 02-28-2009, 05:08 AM
  5. Tree controls using Swing
    By kabir in forum AWT / Swing
    Replies: 1
    Last Post: 01-05-2008, 10:48 AM

Posting Permissions

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