How do I reset/clear JRadioButton in Java [duplicate] - java

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("")

Related

how to set a listener for the selected text in TextView in java? [duplicate]

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.

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? [duplicate]

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());

Enabling/Disabling buttons in JavaFX [duplicate]

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);

How to write a code for default close button in JFrame? [duplicate]

This question already has answers here:
setDefaultCloseOperation to show a JFrame instead
(3 answers)
Closed 8 years ago.
I want to write a code to default close button in JFrame. I just want to ask whether "to save the content or not?" using another JDialog or JOptionPane when click on close button.
Where do I have to write the code? As default close button is not in design of JFrame, and no such event found for JFrame window too, how can I add event listener to that button?
Check out Closing an Application for a couple of solutions:
using a WindowListener and overriding the windowClosing(...) event to display your option pane.
using a simple API so you only need to provide a message or write an ActionListener for more complicated processing.

Multiple swt buttons, same listener [duplicate]

This question already has an answer here:
Creating multiple identical text verify listeners in eclipse-rcp/swt
(1 answer)
Closed 8 years ago.
I have an application which I want to develop in SWT, and I was wondering if there is a possibility to use a single Selection Listener for multiple buttons. For example I have a menu bar which contains an "open" menu item, and I also have a toolbar where I have an open button, can I use the same listener for both? I would be glad to see just a simple example if there is a possibility, and an explanation if there is no possibility to do what's above.
In general it's possible ... your listener will have to check the event's source attribute to determine where the event was fired from.
If you're attaching the listener to different object's, you probably want to implement a generic Listener instead of an object specific listener.

Categories

Resources