Results 1 to 4 of 4
  1. #1
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default Manually adding a component

    Hello everyone,

    Although my last thread received no replies :( , this seems to be the only NetBeans forum that receives replies, so I will continue to seek help here :). What I'm looking to do is to add a component, say a textfield or something, manually to another component, such as a JPanel, for every entry in an array.

    What I did was customize the code for a jPanel that I added, and supplemented the code with this:

    for (int i = 0; i < array.length; i++) {
    javax.swing.JButton blah = new javax.swing.JButton();
    jPanel2.add(blah, i);
    }

    The add method's information box mentioned that the container that the component is added to may need to be validated after the add. I've tried also including jPanel2.validate(), but to no avail. Regardless of whether the for loop is there or not, the add method seems not to work. When I add a component using the NetBeans gui, the code that is generated is not jPanel2.add, but rather jPanel2Layout.addComponent etc. Yet there is no way to manually change that code. Frustrating!!!

  2. #2
    skaspersen is offline Member
    Join Date
    Jul 2008
    Posts
    31
    Rep Power
    0

    Default

    You need to make sure that you have an appropriate layoutmanager installed for the JPanel. In this case the best candidate would be a BoxLayout .


    If you are using BoxLayout use the Jpanel.add(Component) method.
    If you are using a layoutmanager that needs constraints use the Jpanel.add(Component,Object)

  3. #3
    djc
    djc is offline Member
    Join Date
    Jul 2008
    Posts
    10
    Rep Power
    0

    Default

    Wow, I didn't even notice the option to set layout when I right clicked containers....Thanks a lot.

  4. #4
    e1luca is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Same for LayeredPane (in NetBeans 6.5)

    For a jPanel your solution works great. Tnx!
    How about for a jLayeredPane? This one doesn't display a "set Layout" upon right click.
    I'm trying to add a jButton array to a Layered Pane and hit rock-bottom of despair.

Similar Threads

  1. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 12:26 PM
  2. Adding file contents to Choice component
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-07-2008, 10:06 AM
  3. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 10:09 AM
  4. Help with custom component
    By Falcon1 in forum AWT / Swing
    Replies: 8
    Last Post: 07-21-2007, 01:39 PM
  5. Java Chart Component
    By Jamie in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 07:07 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
  •