Mauritz Raes Mauritz Raes - 11 days ago 5
Java Question

Getting text that user entered in an other class

Alright, so I made a class to make a custom textfield.
The class extends a JPanel, so I can set the color of the border.

It looks like this:

public class textfield {

public static JTextField textF = new JTextField();


public static class TextField extends JPanel{

public TextField(int x, int y, int width, int height, String bgColor, String brColor, String fgColor){

this.setLayout(null);
this.setBounds(x, y, width, height);
this.setBackground(new Color(Integer.parseInt(bgColor.substring(0, 3)), Integer.parseInt(bgColor.substring(3, 6)), Integer.parseInt(bgColor.substring(6, 9))));
this.setBorder(BorderFactory.createLineBorder(new Color(Integer.parseInt(brColor.substring(0, 3)), Integer.parseInt(brColor.substring(3, 6)), Integer.parseInt(brColor.substring(6, 9)))));


textF.setBorder(javax.swing.BorderFactory.createEmptyBorder());
textF.setOpaque(false);
textF.setBounds(width / 20, 0, width, height);
textF.setForeground(new Color(Integer.parseInt(fgColor.substring(0, 3)), Integer.parseInt(fgColor.substring(3, 6)), Integer.parseInt(fgColor.substring(6, 9))));
add(textF);

}

}

}


Now this is an example:

TextField userName = new textfield.TextField(1073, 177, 190, 31, "000003006", "120090040", "255255255");
add(userName);


My question now is, how am I able to get the text that the user entered in the textfield? I know how to do it if I only use 1 textfield, but I use multiple.

Thanks in advance!

Answer

Your structure is very strange to me, and I suspect you're missing some fundamental object-oriented concepts and probably just tinkered with this until you "got it to kind of work".

For starters, I don't think these things should be static. That would mean that there can only ever be one of them. Additionally, this should probably just be a simple class and not this embedded class. And fields on a class should be private and expose access only through setter/getter methods where needed.

Consider something like this:

public class TextField extends JPanel{

    private JTextField textF = new JTextField();

    // the constructor you already have goes here
    // replace "textF" references with "this.textF" as needed

}

Now you have a TextField class which is a JPanel and has (as an internal member) a JTextField. None of it is static, so you can create as many of them as you want and each is entirely encapsulated.

Then, to access things in that JTextField object, you can expose more methods on the TextField object:

public class TextField extends JPanel{

    private JTextField textF = new JTextField();

    // the constructor you already have goes here
    // replace "textF" references with "this.textF" as needed

    public String getText() {
        return textF.getText();
    }

}

The getText() method we expose is really just a pass-through to the getText() method that's already on the JTextField. We can expose as many operations we we'd like by creating more methods. If there are a lot of such methods, it might be practical to expose a getter for the textF variable directly, though that would technically violate the Law Of Demeter.

Comments