Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Make a JTextField

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Make a JTextField

    I wanted know how to make a JTextField text box with dimensions - length 100 and Width 20
    here is the code of the JTextField text box i made:-
    Echo = new JTextField(20);
    the 20 written is the size of the text box but when set it to 30 the width of the text box increases but the length remains the same

    I hope you understood my problem
    Plz help
    regards


  2. #2
    Member
    Join Date
    Jul 2011
    Posts
    33
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Make a JTextField

    JTextField Echo = new JTextField(20); // sets number of columns of text
    Echo.setSize(w,h); // JTextField inherits from Component. w&h are assumed to be ints for width & height

    You can have a constuct that looks like yours provided that it be preceded with one that looked like

    JTextField Echo;


    This somewhat awkward construct is used for everything but primitive types like int (but not String, which is a class).
    Check out Herb Schildt's Swing : a beginner's guide -- it is awesome


    Good Luck,


    (A+ on your presentation of problem)
    Last edited by meathead; November 2nd, 2011 at 09:59 AM.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Make a JTextField

    If you want help, you'll have to provide an SSCCE that demonstrates the problem- a JTextField of width 20, and a JTextField of width 30. Then tell us what exactly you were expecting to happen compared to what does happen.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Make a JTextField

    @meathead here is the whole program :-
    public class TextField1 extends JApplet implements ActionListener 
       {
        JTextField Input;
        JTextField Echo;
        public JFrame Panel = new JFrame("A. God");
        public JButton button1 = new JButton("Enter");
        LayoutManager Layout;
        public TextField1 () {
        Panel.setSize(700,400);
     
        Echo = new JTextField(100);
     
        Input = new JTextField ("", 20); 
     
        Layout = new FlowLayout ();
     
        Echo.setEditable (false);
        Input.setBackground (Color.white);
        Input.addActionListener (this);
        Panel.setLayout (Layout);
        Panel.add (Echo);
        Panel.add (Input); Panel.add(button1);
     
        Panel.setVisible(true);
        }
        public void actionPerformed (ActionEvent e) {
         Echo.setText (Input.getText());
         Input.setText("");
         if (e.getSource() == button1)
           {
               Echo.setText (Input.getText());
               Input.setText("");          
       }
        }
       }

    i was expecting to get a rectangular text box (Echo) but instead the text box came like this :-
    Capture.jpg

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Make a JTextField

    Thnx Meathead and KevinWorkmen

Similar Threads

  1. Need help int JTextField
    By n00b in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2011, 09:57 AM
  2. how to delete a JTextField?
    By A4Andy in forum AWT / Java Swing
    Replies: 10
    Last Post: August 31st, 2011, 11:33 AM
  3. Replies: 3
    Last Post: June 11th, 2011, 02:40 PM
  4. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  5. Make some text bold, some NOT bold. JTextField
    By danny_felipe in forum Java Theory & Questions
    Replies: 2
    Last Post: May 27th, 2010, 06:44 PM