Search code examples
javauser-interfaceoopvariableswindow

What is this parameter used in making a window in java?


The following is some code which makes multiple fields (items). I know that the parameter after new JTextField is the content of the text box, and I understand how item2, 3, and passwordField work, but do not understand item1. In the line of code:

item1 = new JTextField(10); 

What does the (10) mean? I would expect there to be a default number inside that text box, but that is not the case. Any help would be appreciated.

public eventhandling(){
    super("The title");
    setLayout(new FlowLayout());

    item1 = new JTextField(10); 
    add(item1); //Adds item to window

    item2 = new JTextField ("Enter text here"); //Making a text box that has the words "enter text here" in it
    add(item2);

    item3 = new JTextField ("uneditable", 20);
    item3.setEditable(false); //This text field cannot be changed now
    add(item3);

    passwordField = new JPasswordField ("mypass"); //Setting the password field with a default password : "mypass"
    add(passwordField);

Solution

  • First you need to understand that there are different ways to initialize a component. Then you also need to notice that there is a difference between "10" and 10. The first is a string, the second is an integer. That being said, if you want the number 10 to show in the JTextField box then you need to pass "10" in the constructor. If you pass 10 that is telling the constructor to set the 10 columns in the JTextField, that is, it restricts the number of characters that can be entered to 10. Check the API ...

    http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html