This question already has an answer here:
How to clear the JTextField by clicking JButton [duplicate]
(1 answer)
Closed 6 years ago.
I am writing a program and I want the JTextfield to be blank after the input has been given.
For example; user inputs a number, he presses the add button and the textfield should be blank again and should not contain the number. I tried using null but it doesnt work. If you need the code tell me, I will edit it.
you can just do
textfieldname.setText("");
Related
This question already has answers here:
Java - placeholder on textfield
(3 answers)
java swing JTextField set PlaceHolder [duplicate]
(4 answers)
Closed 7 months ago.
What I'm trying to do.
I'm basically trying to add a search bar to my program, made out of a JTextArea.
What I would like to do, is add an "example" into the search bar (greyed out text, which disapears when the JTextArea is activated). I've attempted this by changing the text colour, and adding a mouseListener to setText("");. However, this means that every time I click on the bar, it empties, which isn't something I want. (Say I typed in a really long string and had to retype it because it was 1 character wrong).
JTextField searchBar = new JTextField("Search...");
searchBar.setForeground("Color.GRAY");
searchBar.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
searchBar.setForeground(Color.BLACK);
searchBar.setText("");
}
}
What I'd like to know
How can I make it so that, each time the frame is repainted, the search bar will display search... in grey, until clicked, after which it remains persistent.
I hope it's clear why I'm trying to do, any help is appreciated.
Thanks.
This question already has an answer here:
How to setEnabled() in javafx-8?
(1 answer)
Closed 4 years ago.
I'm working with JavaFX. I have a disabled TextField.
private TextField numberField;
How can I make the TextField enable/editable?
I have used:
numberField.setVisible(true);
But it's not working.
You can use
numberField.setEditable(true)
numberField.setDisable(false)
setEditable(true) will actually make numberField edible and setDisable(false) will make sure numberField is not greyed out.
As the name suggests, setVisible(boolean) will only make the object visible/invisible.
setVisible(boolean) is used to change visible property but for your case you need those two:
numberField.setEditable(true);
numberField.setDisable(false);
This question already has an answer here:
Output not getting redirecting to proper Jtextarea
(1 answer)
Closed 6 years ago.
I'm trying to make a Heads or Tails game in Java at the moment and I'm struggling with getting the output text into my text area.
What I have is at the moment is:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (Math.random() < 0.5)
System.out.println("Heads");
else
System.out.println("Tails");
jTextArea1.update(jTextArea1.getGraphics());
}
I'm trying to make sure when the button is pressed it displays but to no avail. It's been bugging me for a bit.
System.out doesn't go to any text area. It's the standard out (although you could redirect it, but let's not get into that here). Use jTextArea1.setText("Heads"); to update the textarea contents. For appending you'll need to get the existing text first.
This question already has answers here:
Java Autocomplete TextField (Ajax style)
(3 answers)
Closed 9 years ago.
How We create a textfield behave as autocomplete by onkeypress evnet in java as Database is Mysql.
You should use a DocumentFilter (thanks MadProgrammer) instead of a KeyListener. The former is more reliable and easier to deal with.
Also instead of a JTextField you should use a JComboBox instead to show a drop down after the user begins typing.
Now I will not put the effort into creating some code for you since you did not show effort in asking the question.
Read up on:
DocumentFilter
JComboBox
This question already has answers here:
How to add hyperlink in JLabel?
(12 answers)
Closed 7 years ago.
I have taken one JFrame in which we have shown the information about our firm
there is one information(label) like:
website: www.samteksystems.com
I want that whenever I click on www.samteksystems.com it should display that website.
Please help me in this.
You can add a mouseListener to the label, and in the mouseReleased() method, you could use Desktop.browse() to visit the URL.