Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By camickr

Thread: Textfields not accepting length arguements

  1. #1
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Textfields not accepting length arguements

    First thing is that this portion is part of a much larger program, as such a main method is needed to run.

    The problem is that the following bit of code doesn't accept any length argument passed for JTextField. the buttons will perform no actions however in this snipet, i have another program that is using the same thing shown here and is accepting all arguments, so i'm really confused. what am i doing wrong?
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class SplashGUI extends JFrame implements ActionListener
    {
    	private JLabel userEmail, password, forgotPrompt;
    	private JTextField emailInput, passwordInput;
    	private JButton loginBtn, registerBtn, forgotBtn;
    	private String tempStr1;
    	private String tempStr2;
    	
    	public SplashGUI()
    	{
    		makeGUI();
    	}
    	
    	private void makeGUI()
    	{
    		this.setTitle("SNS - Log In");
    		this.setSize(500,500);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		Container pane = getContentPane();
    		
    		userEmail = new JLabel("Email: ");
    		emailInput = new JTextField(50);
    		password = new JLabel("Password: ");
    		passwordInput = new JTextField(50);
    		loginBtn = new JButton("Log In");
    		registerBtn = new JButton("Register");
    		forgotBtn = new JButton ("Forgot Password?");
    		loginBtn.addActionListener(this);
    		registerBtn.addActionListener(this);
    		forgotBtn.addActionListener(this);
    		
    		pane.setLayout(new FlowLayout());
    		pane.add(userEmail);
    		pane.add(emailInput);
    		pane.add(password);
    		pane.add(passwordInput);
    		pane.add(loginBtn);
    		pane.add(registerBtn);
    		pane.add(forgotBtn);
    		
    		this.setVisible(true);
    	}
    }
    Here is a snipping of what it is doing.
    Textfields not accepting length arguements-problem-snipet.png

    I am trying to get it smaller, that size expands past the current window size, so you'll need to resize it to see.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,254
    Rep Power
    19

    Default Re: Textfields not accepting length arguements

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Textfields not accepting length arguements

    The length argument of JTextfield indicates the size of the box, not the maximum amount of characters that are allowed. If you want to limit the amount of characters, you need a DocumentFilter, as described in this tutorial: Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  4. #4
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Textfields not accepting length arguements

    Yea, I am actually trying to limit the size of the field, not the amount of characters in it. I was using the characters to show how far beyond 50 it is. I have another program that is using the same type of jtextfield with an argument of (2), and although you can enter x amount of character in this field, only 2 are shown which tells me that one has the correct width for the field. yet in this program it's not adjusting the jtextfield size. just wondering what the issue is and how to fix it.

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

    Default Re: Textfields not accepting length arguements

    The "50" creates a text field that will hold 50 characters the width of "W". Since numbers are narrower than "W" it appears like there is a lot of extra space. When you only have 2 characters you don't notice this as much. The solution:

    a) use a number less than 50
    b) change the font to be a monospaced font then all characters will take up the same space.
    SurfMan likes this.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Textfields not accepting length arguements

    Ah, sorry, I misunderstood your question. Follow camickr's directions.

    Alternatively, use a layout that sizes the components for you, like GridBagLayout, or if you can use external libraries, MigLayout or JGoodies FormLayout.

  7. #7
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: Textfields not accepting length arguements

    I had done that at school. I tested it with 2 and it wasn't working, it didn't resize it. but at my home computer it's resized now. thanks again!

Similar Threads

  1. Replies: 2
    Last Post: 01-22-2013, 05:21 AM
  2. Accepting value for phone number field
    By sgiri37 in forum New To Java
    Replies: 2
    Last Post: 03-22-2012, 09:20 AM
  3. Replies: 0
    Last Post: 09-10-2009, 12:35 PM
  4. [SOLVED] commandline arguements
    By raghu9198 in forum New To Java
    Replies: 3
    Last Post: 12-23-2008, 09:47 AM
  5. Socket programming - accepting files
    By ravian in forum Networking
    Replies: 2
    Last Post: 11-29-2007, 10:40 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
  •