We help IT Professionals succeed at work.

another checkbox problem

dlcnet
dlcnet asked
on
Medium Priority
189 Views
Last Modified: 2013-11-23
Hi all!
 I have a check button with (enable/disable). 2nd I have a textfield which can contains a date and also a callendar button which controls the input on the date field. The checkbox is supposed to have a indefinite period state. When the checkbox is checked, and the user wants to click on the calendar an alert message should come.

 Thank you
Comment
Watch Question

btn.addActionListener( new ActionListener(){
  public void actionPerformed( ActionEven ae ){
     if( chk.getModel().isSelected() ) JOPtionPane.showMessageDialog(parentComponent, "Your warning message!", "Title", JOPtionPane.WARNING_MESSAGE )

   //your code here to open and show the calendar!
  }
}) ;
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Or if you only want to run in on the client (or if you posted this question in the wrong TA - new to JAVA):

What does the button to open the calendar look like?
If just a form button that uses onClick, change to

<input type="checkbox" name="wholePeriod" onClick="this.form.calendarInputField.disabled=this.checked">Whole period
<br><input type="text" name="calendarInputField">
<input type="button"  onClick="if (this.form.wholePeriod.checked) alert('Please uncheck the checkbox');
else {
showCalendar(); // or whatever your calendar code looks like
}">


Please change the fieldnames appropriately...

Michel
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> When the checkbox is checked, and the user wants to click on the calendar an alert message should come.
In that case I would simply disable the textfield (containing a date and also a callendar button)
Imo, preventing that a user does something unwanted is always better than let him/her do it and give afterwards a message to say (s)he wasn't allowed to do it.

So,

yourCheckBtn.addActionListener( new ActionListener() {
     public void actionPerformed( ActionEvent e) {
          if (!yourCheckBtn.isSelected())
                return;
          yourTextField.setEnabled(false);
     }
});
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Is that not more elegantly written as

          yourTextField.setEnabled(yourCheckBtn.isSelected());

?
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Yes,

yourCheckBtn.addActionListener( new ActionListener() {
     public void actionPerformed( ActionEvent e) {
          if (!yourCheckBtn.isSelected())
                return;
          yourTextField.setEnabled(false);
     }
});

can be written shorter as:

yourCheckBtn.addActionListener( new ActionListener() {
     public void actionPerformed( ActionEvent e) {
          yourTextField.setEnabled(yourCheckBtn.isSelected());
     }
});

Whether is more elegant or better readable is a matter of taste...
zzynxSr. Software engineer
CERTIFIED EXPERT
Commented:
Correction!

Both code snippets need a correction:
yourCheckBtn.addActionListener( new ActionListener() {
     public void actionPerformed( ActionEvent e) {
          if (!yourCheckBtn.isSelected())
                yourTextField.setEnabled(true);  // no return here, 
                                       // since that would mean the textfield never gets enabled again
          else
                yourTextField.setEnabled(false);
     }
});
 
which is indeed probably better written as:
 
yourCheckBtn.addActionListener( new ActionListener() {
     public void actionPerformed( ActionEvent e) {
          yourTextField.setEnabled(!yourCheckBtn.isSelected());  // the ! was forgotten
     }
});

Open in new window

Author

Commented:

 @zzynx and @mplungjan
 Thank you both for the sollutions. initially it was mented for Javascript only but since in the near future it wil be a Java application too I can say I got an answer for both problems. Therefore I splited the points in 2, and gave to mplungian 300 since my solution now was needed for javascript.



 
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
thanx 4 axxepting
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
You are welcome

About the not (!) I am used to set items .disabled=true so well spotted

Explore More ContentExplore courses, solutions, and other research materials related to this topic.