)) I'm new to Java, And I have such task:
- Create a text field component class. In the constructor, the default string must be set in the text field.
- When clicking on the field, this record should be erased.
- If user has not entered anything, and the focus from the field has been lost, then the default string should be restored. I have 3 classes: Main, MyComponent (extends JTextField), MyListener (implements MouseListener). Thats my class MyComponent:
public class MyComponent extends JTextField {
private int code;
public MyComponent(String text, int code) {
super(text);
this.code = code;
setFont(new Font("Calibri", Font.PLAIN, 30));
}
public int getCode() {
return code;
}
}
That's my Main:
public static void main(String[] args) {
JFrame frame = new JFrame("My programm");
frame.setBounds(320, 80, 750, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyComponent enterName = new MyComponent("Default name", MyListener.ENTERNAME);
enterName.addMouseListener(new MyListener());
JPanel panel = new JPanel();
JLabel labelName = new JLabel("Enter name: ");
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
panel.add(labelName, c);
c.gridx = 1;
c.insets = new Insets(0, 10, 0, 0);
panel.add(enterName, c);
frame.add(panel);
frame.setVisible(true);
}
and that's MyListener class:
public class MyListener implements MouseListener {
final static public int ENTERNAME = 1;
MyComponent enterName;
@Override
public void mouseClicked(MouseEvent e) {
int code = ((MyComponent)e.getSource()).getCode();
if (code == ENTERNAME) {
System.out.println("Clicked on JTF"); // just to understand that MouseListener is working
// here I need code which in String text from enterName will put "" instead of "Default name"
}
//...
@Override
public void mouseExited(MouseEvent e) {
}
}
As I understand I need to make the text in the JTextField "Default name" disappear when I click the mouse. But I don't understan how should I do that. I also don't understand how to make it so that if the cursor is not on the JTextField, the default text "Default name" is returned.
I tried Reflection in MyListener, but it didn't work out well. That's my attempt:
public class MyListener implements MouseListener {
final static public int ENTERNAME = 1;
MyComponent enterName = new MyComponent("Ivan", MyListener.ENTERNAME);
Field field = enterName.getClass().getField("text");
field.setAccessible(true);
@Override
public void mouseClicked(MouseEvent e) {
int code = ((MyComponent)e.getSource()).getCode();
if (code == ENTERNAME) field.set(enterName, (String) " ");
}
Dear Professionals, please help me! :)
It's much simpler than that. The class has public methods setText and getText to set/get the text. See this tutorial and the javadoc.
You dont't need reflection here, it's completely overkill and if you actually did need it, it would be a sign you were trying to do something the developers of JTextField didn't intend you to do.
It's just:
My advice is that you forget you ever heard about Reflection until you are no longer "new to Java". 99% of the time you will not need it, and the 1% where you do, you should be knowing what you are doing.
In fact, that class does not even have an attribute
text. Here is what setText actually does: