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 8 of 8

Thread: Need help int JTextField

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

    Default Need help in JTextField

    ok i just created a text box with JTextField
    here is the code:-
    public class TextField1 extends JApplet implements ActionListener 
       {
        JTextField Input;
        JTextField Echo;
     
        LayoutManager Layout;
        public TextField1 () {
        Panel.setSize(700,400);
     
        Echo = new JTextField ("", 20);
        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.setVisible(true);
        }
        public void actionPerformed (ActionEvent e) {
         Echo.setText (Input.getText());
         Input.setText("");
        }
       }

    this is sub-class of the main class and Panel is JFrame
    i need help in changing the length of Echo(not the bredth).
    Plz Help
    Regards.
    Last edited by n00b; November 1st, 2011 at 08:22 AM.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Need help int JTextField

    what do you mean by length?
    Altho just an estimated guess at what your trying to do..

    Try using BorderLayout:

    Panel.setLayout(new BorderLayout());
    Panel.add(Echo, BorderLayout.WEST);

    otherwise try to create a new panel.

    add the panel to the JFrame and the textbox to the panel:

    pan = new JPanel();
    pan.add(Echo);
     
    Panel.add(pan);

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

    Default Re: Need help int JTextField

    i meant to say to change the length of the text box created with JTextField (Echo)

    Echo = new JTextField ("", 20);

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Need help int JTextField

    simply change the 20 to longer or shorter depending on the size you want it..

    Echo = new JTextField(10);

    Also dont bother putting "", before the int if you do not want to have default text displayed in your textbox.
    as later on you could always use

    Echo.setText("text wanted...");

    If my understanding is wrong please let me know, I'm half asleep whilst doing multiple things. Sorry.

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

    Default Re: Need help int JTextField

    i m not that n00b lolz
    but when i increase the value,the width of the textbox increases but length remains the same

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Need help int JTextField

    oh sorry.. do you mean you would like to only allow a certain amount of characters to be entered into the textbox ?

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Need help int JTextField

    woot if i finaly understood you ^.^ think i need to have abit of a sleep now lol.


    Limiting the amount of characters that can be put in a textfield before you cannot type anymore:
    import java.awt.FlowLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.PlainDocument;
     
    class JTextFieldLimit extends PlainDocument {
      private int limit;
      JTextFieldLimit(int limit) {
        super();
        this.limit = limit;
      }
     
      JTextFieldLimit(int limit, boolean upper) {
        super();
        this.limit = limit;
      }
     
      public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
        if (str == null)
          return;
     
        if ((getLength() + str.length()) <= limit) {
          super.insertString(offset, str, attr);
        }
      }
    }
     
    public class Main extends JFrame {
      JTextField textfield1;
     
      JLabel label1;
     
      public void init() {
        setLayout(new FlowLayout());
        label1 = new JLabel("max 10 chars");
        textfield1 = new JTextField(15);
        add(label1);
        add(textfield1);
        textfield1.setDocument(new JTextFieldLimit(10));
     
        setSize(300,300);
        setVisible(true);
      }
    }

    Congratulations you now know everything about a JTextField lol..
    Last edited by macko; November 1st, 2011 at 08:48 AM.

  8. The Following User Says Thank You to macko For This Useful Post:

    n00b (November 1st, 2011)

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

    Default Re: Need help int JTextField

    lol...Yea you finally understood me
    but it was not exactly what i wanted but i got some help and got the solution thanks

Similar Threads

  1. JTextField ActionListener
    By macko in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 30th, 2011, 01:53 AM
  2. drawString and JTextField
    By that_guy in forum AWT / Java Swing
    Replies: 9
    Last Post: January 29th, 2011, 02:43 AM
  3. Jtextfield Validation
    By nimishalex in forum AWT / Java Swing
    Replies: 8
    Last Post: December 11th, 2010, 02:42 AM
  4. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM