Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Adding new jtextfield to an open form

  1. #1
    onurdalgic is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default Adding new jtextfield to an open form

    Hello,
    I am quiet new to java. In my program under a button control, I want to add new randomly located and sized jTextField to the main form. Randomness is not the problem but I cant add new object to the form under a button control. After creating the object, what should I do to show the textfield in the form.
    thanks for your concern.

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

    Default

    You show us none of your code or give any necessary details about your problem, and since we can't read minds about the best any of us can do is to refer you to the Swing tutorials: Using Swing Components

  3. #3
    onurdalgic is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Actually I thought that I was clear enough. Sorry about that. I can explain my problem with images:
    in a form screen, there is a button like this below

    after hitting the button a new jtextfield will appear on the form screen like this below

    and after another hit another jtextfield will appear like below


    and this will go on like that the number of the textfield will be endless. so ı dont want to manage this by changing the visibility property of the textfields. So yo can see that there is no code or necessary information that is not given. thanks anyway
    Last edited by onurdalgic; 07-19-2011 at 01:08 PM. Reason: image link corecction

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

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You need to set a suitable layout manager for the form or panel so the button and text fields are laid out the way you want (for vertical layout as in the posted images, use something like BoxLayout using Y_AXIS).

    Then you can add a new text field to the form or panel, and then call validate() and repaint() on the form or panel so the new component is correctly laid out and displayed.

  6. #6
    onurdalgic is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
            // TODO add your handling code here:
           TextField text =new TextField();
           text.setText("TEXT");
           text.setVisible(true);
           text.setSize(200, 200);
           text.setLocation(500, 500);
           text.setBackground(Color.red);
           text.setVisible(true);
           validate();
           repaint();
              
        }
    After adding validate and repaint commands. Still not working, in deed I have never heard layout manager before. May be missing point is that on logic.

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

    Default

    Quote Originally Posted by onurdalgic View Post
    in deed I have never heard layout manager before. May be missing point is that on logic.
    Yeah, that's why we need to see your code, to see where your gaps in knowledge are. I suggest reading this section of the Swing tutorials: Laying out components in a Container

    And then if you are using a code-generator to generate your Swing code (such as the Matisse code generator that comes with NetBeans) that you stop using it but rather use the tutorials to help you learn to create your Swing GUI's by hand. You can find out more here: Using Swing
    Dark likes this.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    And since you are just learning, I would use Swing, not AWT. Swing components start with "J", like JTextField.

Similar Threads

  1. script open form result
    By rprmatias in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 09:01 PM
  2. Adding List to JTextField
    By nipuna in forum AWT / Swing
    Replies: 7
    Last Post: 09-09-2010, 06:28 AM
  3. Adding jTextField() content to the database?
    By Stephen Douglas in forum New To Java
    Replies: 12
    Last Post: 04-08-2010, 09:50 AM
  4. Replies: 6
    Last Post: 11-28-2008, 06:55 AM
  5. Open specific folder form JSP
    By roseline43 in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 07:53 PM

Tags for this Thread

Posting Permissions

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