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.
Related
How can i display and image when button1 is pressed, this is the code im using to check for a button press:
public class clientFrameButtons {
public void frameClient(){........
....
button1.addActionListener(new ActionListener(){ //ActionListener checks for button press
public void actionPerformed(ActionEvent e){
// if button is pressed the following will happen
I've tried implementing an if statement and looked at several sources online but cannot find a answer to fit my needs.
Thanks.
ActionEvent is a logical event those intention is to signal the fact that a button was actioned. So it doesn't provide any information about the user inputs that have lead to the action (it may be a shortcut, a mouse press, your mouse could have been set to left-handed, etc). If you want to care about mouse, then you must catch mouse events, with an appropriate MouseListener.
So your problem can be expressed in two ways:
you want to display an image when a GUI button is actioned then use ActionListener.
you want to make different things depending on the mouse button pressed while over a GUI button, then use MouseListener.
#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.
I'm writing a piece of software in Java, with a GUI programmed using swing. The user has to do this two operations in a loop:
write some words in a text field (let's call it A) and press Enter
click a button (let's call it B)
After Enter is pressed, some other fields show some data. After the button is clicked, all text fields are cleared and the procedure restarts. The text field to fill in is always A and the button to click is always B. This is what I would like to implement:
After the text field is filled and Enter is pressed, the button is somehow selected so that if the user presses Enter again the button is clicked
After the button is pressed, the text field is selected and in "write mode", so that the user can immediately start typing
What is the easiest method to implement this? Below I'm reporting what I have already tried.
I tried this to make the button selected immediately after Enter is pressed in the text field:
this.getRootPane().setDefaultButton(ButtonNextQuestion);
but when I press Enter, it is registered both by the text field and the button and I cannot see intermediate data.
I tried also with these lines:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
in this case everything seems working, but when I press enter the button is not clicked.
Thanks to AdamK!
The focus to the button was given like this:
ButtonNextQuestion.requestFocus();
ButtonNextQuestion.setSelected(true);
I had to add a listener to the button like this:
private void ButtonNextQuestionKeyPressed(java.awt.event.KeyEvent evt){
if(evt.getKeyCode() == 10) {//ENTER pressed: click
ButtonNextQuestion.doClick();
}
}
Then again to automatically select and make the text field ready to be written I used:
TextYourAnswer.requestFocus();
I have to make three radio buttons. On selection of the third radio button I have to show some text boxes. I also have to immediately remove the text boxes when the third radio button is unselected and any one of the other two is selected. I am looking for a callback when the JRadio button is unselected. I have supplied an ActionListener but it gets called only on selection and not on unselection. How do I achieve this toggle functionality with java JRadio button.
radioCustomButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Callback called");
if(radioCustomButton.isSelected()){
System.out.println("Checked");
}
}
});
This is printing "Callback called" only on check but not on uncheck. I have added this radioCustomButton object in ButtonGroup as well.
I am left with the only option of supplying action listener to other two radio buttons and then write code to remove the text boxes.
Don't use an ActionListener to check when the button is selected.
Instead use an ItemListener and you will be notified when the button is selected and unselected.
See How to Write an Item Listener for an example.
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();