Results 1 to 2 of 2
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Adding components to a panel

    Hi,

    I need to add a text field to a panel and I need to use the Jcomponent class, this is what I have done using the Box class:
    Java Code:
    public class System
    {
    private JTextField name = new JTextField(10);
    ...
    ...
    ...
    private void build
    {
    Box box = Box.createVerticalBox();
    box.add(new JLabel("Name"));
    box.add(Box.createHorizontalStrut(10));
    box.add(name);
    add(box)
    }
    ...
    }
    I need to change the size of the text field to 50x20, so this is what I have done using the Jcomponent class instead of box in the build method above:
    Java Code:
    Dimesnsion size = new Dimension(50, 20);
    component.setPreferredSize(size);
    This is incorrect and incomplete, please clarify what I am doing wrong

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

    Default

    in this line:
    component.setPreferredSize(size);
    what is component?

    If you are setting the preferred size of the textfield, I would think that you would need to call:
    name.setPreferredSize(size);
    unless component was the name of a parameter to a method that you've created.

Similar Threads

  1. adding components to frame
    By roaan in forum New To Java
    Replies: 4
    Last Post: 07-05-2009, 03:30 PM
  2. panel positioning
    By shwein in forum New To Java
    Replies: 4
    Last Post: 09-09-2008, 05:15 PM
  3. How to add more components to main panel
    By aneesahamedaa in forum AWT / Swing
    Replies: 4
    Last Post: 08-21-2008, 06:31 PM
  4. Replies: 5
    Last Post: 08-02-2008, 08:05 PM
  5. add image on panel
    By samiksha.goel in forum AWT / Swing
    Replies: 4
    Last Post: 08-02-2008, 07:38 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
  •