This question already has answers here:
How to disable Button when TextField is empty?
(3 answers)
Closed 7 years ago.
how do you disable buttons under a certain condition? For example, i have many text fields and buttons, when those text fields are empty one of my buttons should be disabled. i already have this code.
if(txtID.getText().isEmpty()&&txtG.getText().isEmpty()
&&txtBP.getText().isEmpty()&&txtD.getText().isEmpty()
&&txtSP.getText().isEmpty()&&txtCons.getText().isEmpty()){
btnAdd.setDisable(true);
}
else{
btnAdd.setDisable(false);
}
is there an easier way to do this? Also if i add text into those areas shouldnt the button be re enable its self?
Create a BooleanBinding using the textfield's textProperty() and then bind it with the Button's disableProperty().
For enabling the button if any of the textfields is not empty.
// I have added 2 textFields, you can add more...
BooleanBinding booleanBind = Bindings.and(text1.textProperty().isEmpty(),
text2.textProperty().isEmpty());
button.disableProperty().bind(booleanBind);
For more than 2 textfields
BooleanBinding booleanBind = Bindings.and(text1.textProperty().isEmpty(),
text2.textProperty().isEmpty()).and(text3.textProperty().isEmpty());
Or, a better approach is to use and directly on the property:
BooleanBinding booleanBind = text1.textProperty().isEmpty()
.and(text2.textProperty().isEmpty())
.and(text3.textProperty().isEmpty());
For enabling the button only if all of the textfields have text.
Just replace and with or.
BooleanBinding booleanBind = text1.textProperty().isEmpty()
.or(text2.textProperty().isEmpty())
.or(text3.textProperty().isEmpty());
in swing we can disable a button as follow:
JButton start = new JButton("Start");
start.setEnabled(false);
but in javaFX this function(setEnabled) changed to setDisabled, so we can use this code in javaFX:
start.setDisable(false);
Related
This question already has answers here:
How to set up a listener on the selected text in TextView
(3 answers)
Closed 1 year ago.
i want a Help just which way i follow to get a listener for the selected text in TextView,
what i want is when the user select a specified text, a button of options will appear.
i hope you help me
Have you tried ActionListener? It will be called when user interacts or performs any action on that particular component. You can get the selected content by component.getSelectedText() or similar method. Do not write separate listeners for mouse and keyboard operations as it might interfere with one another and cause problems.
This question already has answers here:
How to disable button if the TextField empty?
(3 answers)
Closed 1 year ago.
In my javafx application i have some scenes with textfields and button can i set the button on disabled when it s empty and enabled when it s not ? like a test on fields when im typing i have to change the value of the proprety of the value ! and when i changed when it s empty i can t enable it after i typed some thing if (tv_np.getText().length()<1) {btn_c.setDisable(true); }else btn_c.setDisable(false);
You can simply use the textproperty of the textfield and bind the disabled property of the button to it:
btn_c.disableProperty().bind(tv_np.textProperty().isEmpty());
I'm trying to make a game where I get questions from the database and the JRadioButton shows these questions.
How do I define the radio button?
I can code to get the answers from the database but how do I set up my radio button?
You must know how to fetch data from the database since you say you know how to get the answers from the database, so let's assume you've retrieved a question that you want to apply to your radio button. We'll call it myQuestion.
JRadioButton has a constructor which takes a String and a boolean as arguments, representing the String that the radio button should be displayed, and whether the radio button should be selected.
JRadioButton myRadioButton = new JRadioButton(myQuestion, false);
This gives you an unselected radio button with your question as text.
Now what if you want to change your question at some point? You can use JRadioButton's setText() method to change the radio button's text at any time.
myRadioButton.setText(myNewQuestion);
This question already has an answer here:
Clearing a group of radio buttons in Java
(1 answer)
Closed 9 years ago.
How do you reset/clear a JRadiobutton? clearing a TextField is as simple as surenameField.setText(""); is there any similar way with the RadioButton?
You mean
radioButton.setSelected(false);
?
If you want to clear the text of a JRadioButton named radio, you would call radio.setText(""). If you want to de-select the button, you call radio.setSelected(false). Note that this does not actually fire events, so if you have a listener registered to respond to de-select events, this won't notify that listener.
For more information, you can read the official documentation for JRadioButton here
JRadioButton API
Could you specify what you mean by "reset"?
The following things are possible:
Remove the button from the program: you can do this by calling the remove method from its container ([container].remove([radioButton], eg. myPanel.remove(myRadioButton).
Remove the text from the button: you can do this by doing what you set: myRadioButton.setText("")
in a VAADIN Component a User should have the Option to insert one to N Answers to a Question.
Initial there should be one Textfield for the first Answer with a Button ("Add another Answer")
At Button Push there should added a new Textfield for the second Answer, and so on ...
My Question is, how can I realize the dynamical loading of a new Textfield at Button Push?
I Added a picture of how I imagine this Problem.
Just add a click listener to the button which adds a text field to the layout. Basic Vaadin, no tricks.
If your code does not work, add it to your question.
Put the Textfield in a vertical Layout of its own, whenever the button is clicked, add a new Textfield to the vertical layout.
when its time to retrieve the answers, get the reference to the vertical layout and call getChildren(), this will return all the TextFields in the vertical layout.
something like this:
List<String> answers = verticalLaout.getChildren()
.stream()
.map(TextField::getValue)
.collect(asList());