Results 1 to 8 of 8
  1. #1
    adi.shoukat is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Cannot add component to JPanel

    Hi,
    I am making a GUI in Netbeans. On a button-click i want to add a JTree in a JPanel but I am not able to add that JTree in my JPanel.
    Can anyone tell me what code should I write in "buttonMouseReleased" ??

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Quote Originally Posted by adi.shoukat View Post
    Hi,
    I am making a GUI in Netbeans. On a button-click i want to add a JTree in a JPanel but I am not able to add that JTree in my JPanel.
    Can anyone tell me what code should I write in "buttonMouseReleased" ??
    tell us why you are not able to add the jtree to your panel? exception? compiler error message? the jtree is not visible? which code do you use ? .....

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

    Default

    For starters, go through the Swing tutorial on How to Use Buttons, Check Boxes, and Radio Buttons linked from the API for AbstractButton, JButton and other button classes where you will learn what listener to use. Hint: it's not a MouseListener.

    db

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    For starters, go through the Swing tutorial on How to Use Buttons, Check Boxes, and Radio Buttons linked from the API for AbstractButton, JButton and other button classes where you will learn what listener to use. Hint: it's not a MouseListener.

    db
    I agreed with Darryl. Newbies should do this, before deals with IDEs.

  5. #5
    adi.shoukat is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    @eRaaaa
    Quote Originally Posted by eRaaaa View Post
    tell us why you are not able to add the jtree to your panel? exception? compiler error message? the jtree is not visible? which code do you use ? .....

    No Exception, that code runs but JTree is not visible on JPanel.
    Here is my Code that i am writing in buttonReleased:
    Java Code:
    JTree tree1 = new JTree();
            JScrollPane scroll1 = new JScrollPane(tree1);
            scroll1.validate();
            mainPanel.add(scroll1);
            
            mainPanel.validate();
            mainPanel.setVisible(true);
            mainPanel.setVisible(true);

  6. #6
    adi.shoukat is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    For starters, go through the Swing tutorial on How to Use Buttons, Check Boxes, and Radio Buttons linked from the API for AbstractButton, JButton and other button classes where you will learn what listener to use. Hint: it's not a MouseListener.

    db
    You think I am writing code in wrong Listener ?? I've tried attaching the debugger and also prompted a message before my code. When I press the button my code in executed. But I am worried why its result isn't visible on JPanel.
    Yes you are right I should go through the tutorials first. But unfortunately I've submit my work on Monday. Can you please make your hint a little more clear ??
    Thanks

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The answer for a JButton is to use an ActionListener, but you would have found this out quicker than waiting for a forum reply if you had just peeked at the Swing tutorials mentioned by Darryl. Please feel free to show some effort. Also based on the code you've posted, I doubt anyone will be able to help you since it doesn't show what the problem is, and I fear that your problem may be due to your using NetBeans to generate your GUI, and that you are using NetBean's default GroupLayout. If so, then you would do well to hand-craft your Swing code and use other layouts as well outlined in the layout section of the Swing tutorials.
    Last edited by Fubarable; 10-14-2010 at 06:29 PM.

  8. #8
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I fear that your problem may be due to your using NetBeans to generate your GUI, and that you are using NetBean's default GroupLayout. If so, then you would do well to hand-craft your Swing code and use other layouts as well outlined in the layout section of the Swing tutorials.
    Agreed!


    Change your JPanel Layout to something other than the Free Design (NetBean's default GroupLayout). If you are adding your other component to the JPanel after the NetBeans generated init() code, then your component won't always show up if your JPanel Layout is set to the Free Design layout.

    I know this is a few months old, but I thought I would post this anyway in case someone else came across this same issue while using NetBeans, and they were reading this thread for a possible solution.

Similar Threads

  1. Component not updating??!!
    By XmisterIS in forum New To Java
    Replies: 5
    Last Post: 09-03-2010, 11:25 AM
  2. Component to use ?
    By pbaudru in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2010, 09:55 AM
  3. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 PM
  4. Modifying and reloading JPanel component
    By gopher in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2009, 04:15 AM
  5. how to remove all component in container(Jpanel)
    By SaYuNaRa in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 04:22 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
  •