0

I am having trouble calling the getText() method on a JTextField object. First, I called panel.getComponent(2) because the second component that I added to panel was the username text field, which I confirmed by running my application and "username" was being printed in the console. This is where I sort of got lost because I knew that this returned the username text component, so I'm confused why when I get an error when I try to call getText(). Does anyone know why this is?

Here is my code:

// MODIFIES: this
// EFFECTS: creates username text field
private void makeUsernameTextField() {
    JTextComponent username = new JTextField();
    username.setName("username");
    username.setBounds(width / 2 - borderThickness - userFieldW / 2,
            height / 2 - borderThickness - textFieldH / 2, textFieldW, textFieldH);
    username.setOpaque(true);
    panel.add(username);
}

@Override
public void actionPerformed(ActionEvent e) {
    String username = panel.getComponent(2).getText();
    String action = e.getActionCommand();

    if (action == "login") {
        login.signIn(username);
        System.out.println(username);
        System.out.println("succesful!");
    } else if (action == "sign up") {
        System.out.println("sign up pressed");
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Allan T
  • 23
  • 3
  • 1
    Are you sure that 2 is the index of your JTextComponent inside the the panel? Because index of 2 means the third component. It should be panel.getComponent(1) if it's the second. The indexes start from 0. – MST Mar 11 '22 at 21:26
  • 1
    A “better” solution would be to make the textfield and instance field of the class, rather then assuming the component order – MadProgrammer Mar 11 '22 at 22:44

1 Answers1

4

I get an error when I try to call getText()

String username = panel.getComponent(2).getText();

The getComponent() method returns a Component which does not have a getText() method.

You need to cast the object to a JTextField:

Component component= panel.getComponent(2); // or 1 if it really is the second component
JTextField textField = (JTextField)component;
String username = textField.getText();

Also:

if (action == "login") 

Don't use "==" for string comparison.

Insetead use the equals(...) method:

if (action.equals("login")) 
camickr
  • 321,443
  • 19
  • 166
  • 288
  • May I ask why I should use .equals()? I get that when we are comparing objects, we should use .equals() so that we can override the hashcode and .equals() to compare the contents of the object, but why is it necessary to use it in this case if I am only comparing strings? Is it just to good practice to use .equals()? – Allan T Mar 12 '22 at 03:34
  • Also, just to clarify, it returns a component with a declared type of component and an actual type of JTextField; that's why getText() cannot be called? – Allan T Mar 12 '22 at 03:45
  • @AllanT Read this: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java (Strings *are* objects.) – VGR Mar 12 '22 at 03:57