JavaFX Radio button disable/reenable text fields - java

#FXML
private void isDelivery(ActionEvent event){
if (rdDelivery.isArmed() == true){
txtAddress.setDisable(true);
txtEmail.setDisable(true);
}
else {
txtAddress.setDisable(true);
txtEmail.setDisable(true);
}
}
This code works to disable the text fields but after the first press they stay disabled and wont come back on when the radio button is pressed over and over.
So after first press the textfields become disabled permanently

You got a typo in this logic:
if (rdDelivery.isArmed() == true){
txtAddress.setDisable(true);
txtEmail.setDisable(true);
}
Change to:
#FXML
private void isDelivery(ActionEvent event){
txtAddress.setDisable(!rdDelivery.isArmed());
txtEmail.setDisable(!rdDelivery.isArmed());
}
It's not clear from your post if you want these fields enabled when the radio button is selected, and not otherwise, so I assumed that the fields are not disabled when the radio button is selected.
No matter what, your logic says to disable those fields.
It might make things easier to use property binding to control the disabled state of your fields. If the radio button is checked, then you can enable the fields and if not, disable them using the selectedProperty() of the radio button.
Something like this (in your initialize method, or similar):
rdDelivery.selectedProperty().bind(Bindings.not(txtAddress.disabledProperty()));
rdDelivery.selectedProperty().bind(Bindings.not(txtEmail.disabledProperty()));

You've written the code to disable them when it is checked, but you haven't done anything to handle when it is unchecked. You cannot expect the computer to infer this.
You need to listen for when the button is unchecked and respond appropriately.

Related

How to enable a disabled button by clicking on same button in android java?

I want to disable button and enable it again by clicking on same button in android java app.
I think you have misunderstood something. When a button is disabled, that means that all clicks on it will be ignore. That would include a click to enable it.
In short, what you are asking for cannot work.
Now, you could implement a button that toggles between "on" and "off" states when you click it. There is an existing control for that: https://developer.android.com/guide/topics/ui/controls/togglebutton
That may be what you really need.
What you want:
You need a feature where once the Button is clicked it enters the disabled state and when clicked the next time it should come back to the active state.
Problem:
As #Stephen C pointed out, once the Button is disabled it can't be clicked again and brought back to the active state.
Solution
So instead of disabling the Button, we can just make the user feel that the Button is disabled.
How:
short noOfClicks = 0; //declare it as top level state variable
Button mButton = (Button) findViewById(R.id.your_button);
mButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
++noOfClicks //this counts the number of times button is clicked
if( noOfClicks % 2 == 0 ){ //user has clicked the button from disable state
//do your work here
mButton.setAlpha(1.0f); // this will bring back button to its original opacity
}else { //user has clicked the button from enabled state
//do your work here
mButton.setAlpha(0.5f);// this will grey out the button(actually, it changes the opacity of the button and gives a disabled look to the button)
}
}
});
What Just Happened:
Whenever the user click's the Button, noOfClicks will get incremented by one, the if-else check will determine whether the Button currently is in the abled or disabled state, depending on which we apply setAplha() method which controls the opacity of the Button.
Conclusion:
Honestly speaking we just used an ugly hack which provided a false sensation to the user that the Button gets disabled when he clicks it for the first time and gets enabled when he clicks it for the second time.

How to enable a button when text is entered in a text field in Netbeans

I'm very new at coding Java and Netbeans. So basically, I have a "save" button and three text fields, I want to enable the Button when these three text fields are edited and disable the button when one of them is empty. Also I'm wondering where I should put my codes. Since it's Netbeans I'm only familiar with ActionPerformed methods, there you can set an action when a button is pressed.
If you can keep it simple it would be appreciated!
public project() {
initComponents();
//Here I want the window to appear in the middle of the screen
setLocationRelativeTo(null);
if(txfField1.getText().equals("")){
btnSave.setEnabled(false);
}
else {
btnSave.setEnabled(true);
}
}
I tried with this code on only one of the three text fields and It does not work, the button is always enabled. The button is initially disabled. Additionally I have also tried to put my code below this method:
public class project extends javax.swing.JFrame {
You can use event handlers to change the state of the button. For example, if you have one text field and you want to change the state of the button depending on the data inside the text field, you could use something like
if (!jTextField1.getText().equals("")) {
jButton1.setEnabled(true);
} else {
jButton1.setEnabled(false);
}
and the event handler you can use
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
You can generate this automatically in Netbeans by going to the event tab when you have clicked on a component in the design view.
It seems in your example you have the right idea, however you need to update the button using events such as key pressing, key releasing etc
You can add it in onblur() method of those text boxes.
If it can, you can add a validation with an error message on click of save button, which might be more meaningful.

Textfield loses focus when user clicks on checkbox (Java Swing)

So I'm using a JPasswordField to get the user input, and then give instantaneous feedback regarding the strength of the entered password. My problem is that if the user clicks on the 'hide' checkbox, the string in the textfield isn't immediately masked by '●', but works only when the textfield regains focus. I've tried to use component.getFocus within the mouseListener, but it doesn't seem to work.
Here's what this particular listener looks like:
inputT.addMouseListener(new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent hideClicked){
if (hideC.isSelected()){
inputT.setEchoChar('•');
inputT.requestFocus();
}
if (!hideC.isSelected()){
inputT.setEchoChar('\u0000');
inputT.requestFocus();
}
}
});
Show the feedback to the user in a different field than the password field that they are typing into. Ideally a separate read only component that is to the right of the password in your GUI. Your password strength feedback would then be updated by a listener on the password field, but the JPasswordField could then just work like normal.
From the JavaDoc: http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocus%28%29
public void requestFocus()
Requests that this Component gets the input focus. Refer to Component.requestFocus() for a complete description of this method.
Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of requestFocusInWindow(). If you would like more information on focus, see How to Use the Focus Subsystem, a section in The Java Tutorial.
Also, as MadProgrammer pointed out - your listener is on the JPasswordField instead of the JCheckBox.
Don't use mouse listener. For doing on selection, use ItemListener:
checkBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent evt) {
if(evt.getStateChange()==ItemEvent.SELECTED)
jPasswordField1.setEchoChar((char)0);
else jPasswordField1.setEchoChar('*');
}
});
Well, the above code works perfectly for me.
Start by attaching a ActionListener to the the checkbox, this means that user can click or press the space bar to activate the check box
In the actionPerformed method, check the state of the checkbox and the required changes.
Use JPasswordField#selectAll or JPassword#requestFocusInWindow (usually I do it the other way round ;)). If these do not work, you might even consider using JPassword#repaint to force the field to repaint

Double if statement in java (swing, JFrame) not working

I'm trying to make a form so that when a user checks a checkbox and clicks a button, some code will execute. I've tried to do this in an if statement and nothing happens when I do the 2 things. I am doing this in Java with Swing.
Here is the code:
private class theHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
String tftext;
tftext = tf1.getText();
if (event.getSource()==b1)
if(event.getSource()==cb1)
JOptionPane.showMessageDialog(null, tftext, "title", JOptionPane.INFORMATION_MESSAGE);
b1 is a button, cb1 is a checkbox and tf1 is a textfield.
Event.getSource() won't reference two different objects, it should reference the unique source of a single event, e.g. a Button in the case of a button click. Your nested statement will never execute.
It sounds like you should be handling the button click, and within that event handler check the state (checked or not) of the checkbox. If the checkbox is checked, then show your dialog.
Basically what you are saying there is that if the event came from the button and the event came from the checkbox, show a message.
This is not possible because one event cannot be triggered by a button and a checkbox in the same time. You cannot click on both in the same time.

How to control the focus listener upon action listener?

I have a panel. This panel has one text field and a button. The text field has focus listener to search some db value, if not value is written it shows an exit display message when tab.
But, when edit the text field and button clicked without pressing tab key, following order occurs:
1) focus lost
2) action listener
Problem is the calling the focus lost, action listener should be call when
edit into text field ---> button clicked(without tab into text field)
Would you please kindly share your idea ?
"If the user leaves after typing something then call the action listner without calling lost focus
Okay, firstly, you can't not have focus lost fired, however, you can ignore it
public void focusLost(FocusEvent evt) {
if (textField.getText().length() > 0) {
// call action
} else {
// show error message
}
}
Okay, now that we can ignore the focus event, how to fire the action event?
Well, surprisingly, this really simple
button.doClick();

Categories

Resources