Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Help with JTextBox please?

    Hi guys, before I tell you my problem I just want you to know that I looked online but nowhere did I find why this error is happening; I am creating a panel and adding it to the JFrame and then adding a textbox to the panel. The program runs, but the textbox is not displayed? Did I do it completely wrong or is it a quick fix? Thank you!

    Java Code:
    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.*;
    public class Box {
    Box(){
         JFrame f =  new JFrame("Text Box");
         f.setSize(400,400);
         f.setVisible(true);
         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         JPanel p = new JPanel();
          f.add(p);
          JTextField t = new JTextField("Enter numbers here.", 15);
          p.add(t);
      }
    public static void main(String[] args) {
    new Box();
        }
    }

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

    Default Re: Help with JTextBox please?

    Resize the JFrame and tell me what happens.... the JTextField appears doesn't it?

    Solution:
    1) don't set the size of the JFrame.
    2) Instead set the preferredSize of the JPanel (or override its getPreferredSize() method)
    3) First add your components to the JFrame
    4) *Then* call pack and setVisible(true) on the JFrame *after* adding all components to it.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Help with JTextBox please?

    Quote Originally Posted by Fubarable View Post
    Resize the JFrame and tell me what happens.... the JTextField appears doesn't it?

    Solution:
    1) don't set the size of the JFrame.
    2) Instead set the preferredSize of the JPanel (or override its getPreferredSize() method)
    3) First add your components to the JFrame
    4) *Then* call pack and setVisible(true) on the JFrame *after* adding all components to it.
    Oh... It did work after resizing it! Ok, I got it; from now on rather than set the size of the JFrame I will set a preferred size for the panel. Thank you so much Fubarable.

Similar Threads

  1. Restricting size to a jtextbox
    By wired-in=p in forum AWT / Swing
    Replies: 2
    Last Post: 03-06-2012, 04:16 AM
  2. how to save a pdf file as named in a Jtextbox.
    By seredi in forum New To Java
    Replies: 6
    Last Post: 09-20-2011, 03:27 PM
  3. Complex GUI 60 JComboBox + 40+ JTextBox
    By Paul_White in forum New To Java
    Replies: 8
    Last Post: 05-12-2011, 07:51 PM
  4. Jtextbox with dropdown list
    By vijay_s in forum AWT / Swing
    Replies: 1
    Last Post: 05-08-2008, 10:17 PM
  5. Jtextbox with dropdown list
    By vijay_s in forum AWT / Swing
    Replies: 3
    Last Post: 05-08-2008, 06:26 AM

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
  •