?
Solved

another checkbox problem

Posted on 2008-01-29
9
Medium Priority
?
164 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
0
Comment
Question by:dlcnet
9 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 20766443
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!
  }
}) ;
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1200 total points
ID: 20766518
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 20775393
>> 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);
     }
});
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20775436
Is that not more elegantly written as

          yourTextField.setEnabled(yourCheckBtn.isSelected());

?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 20775499
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...
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 800 total points
ID: 20775521
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

0
 

Author Comment

by:dlcnet
ID: 20794872

 @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.



 
0
 
LVL 37

Expert Comment

by:zzynx
ID: 20795400
thanx 4 axxepting
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20795536
You are welcome

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month6 days, 2 hours left to enroll

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question