Results 1 to 4 of 4
  1. #1
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default Manually position JTextfield problem

    hey i've got a problem with the following code in my JFrame class

    Java Code:
    setBounds(halfWidth/2,halfHeight/2,350, 330);
    Container c = getContentPane(); 
    c.setLayout(null); 
    				  
    Font f = new Font("Sanserif", Font.BOLD, 30);
    riddleField = new JTextField("", 9);
    riddleField.setFont(f);
    riddleField.setBounds(200, 250, 90, 30);
    RiddlePanel rp = new RiddlePanel();
    rp.add(riddleField);
    rp.setBounds(0,0,350,330);
    c.add(rp);
    The problem is I want to manually position my JTextField (riddleField) at the position 200,250 with width 90 and height 30 within my JPanel (rp). The frame is non-resizeable. I don't want to use any layout manager i just want it at the position that I've specified. But the problem is my JTextField is always drawn at the very top of the panel which is wrong. So there's something wrong with the way i've used the setBounds() method on my JTextField and I don't know what it is. Could someone please tell me how to have my JTextField set at the right position in my JPanel. I have to position a few objects in specific positions so if I know how to fix this i can apply the same to the others.

    Thanks heaps, i appreciate all the help. :)

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Post the smallest full program that is compilable and runnable and demonstrates the problem.

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    I don't want to use any layout manager
    Why? Using Layout Managers is much easier. In this case all you need to do is:

    a) add an EmptyBorder to the panel
    b) set the preferred size of the text field
    c) add the text field to the panel and it will paint exactly where you want it to.

    Null layouts are more trouble than they are worth.

  4. #4
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default

    1. create a jpanel, set the layout to null
    2. create the component (the JTextField), set the font, then set the boundaries.
    3. add to the JPanel
    [why are you annoyed with my sig?]

Similar Threads

  1. Manually adding a component
    By djc in forum NetBeans
    Replies: 3
    Last Post: 04-01-2009, 11:51 AM
  2. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 01:36 AM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. Got problem with JtextField
    By hungleon88 in forum AWT / Swing
    Replies: 4
    Last Post: 12-06-2008, 03:01 PM
  5. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 11:26 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
  •