Java: How to set value of one JDateChooser to another JDateChooser? - java

I have two JDateChoosers, One with label 'Start Date' & another with 'End Date'.I have two radio buttons 'single Day' & 'Multiple Day'.If I select 'Single Day' I want to display same date to 'End Date' which selected in 'Start Date'. And I also want to clear these JDateChooser fileds on CLEAR_BUTTON_CLICK.How do I write this? I am using this control first time..
Plz,help me..
Thanks in advance..

I'm assuming that you are talking about JDateChooser from JCalendar. Am I correct? The JDateChooser fires a PropertyChangeEvent when its date is changed. So, to set the date of another JDateChooser, you need to add an event handler to the "source" component to deal with the change event. When it is fired, you take the date of the component using getDate() method and set it to the target component, using setDate() method. As you are using a component suite that I don't have installed here, it is difficult to implement a correct solution to you.
Take a look in the docs: http://www.toedter.com/en/jcalendar/api/com/toedter/calendar/JDateChooser.html
I think that reading this you will be able to do what you want.
Edit: Here is some code. Try to use it. I really don't have certain that it will works since I didn't test it.
// sourceDateChooser and targetDateChooser MUST be final,
// since they will be accessed inside a anonymous inner class
sourceDateChooser.addPropertyChangeListener( new PropertyChangeListener(){
#Override
public void propertyChange(PropertyChangeEvent evt) {
// the docs of JDateChooser says that when the date is modified, a "date" property change is fired
if ( evt.getPropertyName().equals( "date" ) ) {
targetDateChooser.setDate( sourceDateChooser.getDate() );
}
}
});

Related

The value of jDateChooser is back to the default value

I'm using jDateChooser in NetBeans, when i run the program and choose any date, it is not choosing correctly.
For example, if i choose 05-05-2020, it shows 29-12-2018.
Also, if it catch the selected date, when i press on another item (any button or text field), the Date back to its default.
I didn't add any code related to it, only take it from tools box and put it on the Frame.
What is the problem?
The problem was that i am using wrong date format, it was YYYY-MM-dd, the correct one is yyyy- MM-dd

Customize Android CalendarView - Display DropdownMenu

I have a project where I use a CalendarView, the built in Calendar for android.
So, I'm facing some difficulties to achieve some specific tasks for this Calendar. And I really hope I can find the solution here on StackOverflow.
Is there any possible way of setting each day on a onClickListener?
If not, Then stop reading.
This is the problem I have:
I would like to display a dropdown menu from the selected day, with a few elements that the user can select. If the user select some element, than the state will change for the dropdown menu ONLY for the selected date i.e. if user click on 13/1 the dropdown should display 10 elements. If user select one value, "lunch", then the dropdown menu must change so if user click again only "lunch" and "delete" will appear, not the other 9 elements which were displayed from first user interaction.
The problem I have here, is that the dropdown menu is connected to ALL dates, so I believe I need a onClickListener for each date in the calendar, therefore I wonder if it is possible.
I have been struggling with using different libraries from the internet, especially from GitHub without any success so I hope there's a way of achieve this via the built-in calendar.
Best regards
Is there any possible way of setting each day on a onClickListener?
Yes.
Try this way, In your onClickListener,
1. You need calendar instance.
Calendar calInstance=Calendar.getInstance();
2. SimpleDateFormatter required.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
3. Create a Date Object
Date date = new Date();
4. Create string to store current date on each click listener.
String currentDate=sdf.format(date);
5. Perform your required operations on this currentDate that you have received.

Dispay non-String variable to JLabel

I wonder if JLabel really can't display a value beside String and Icon. For example for displaying Clock variable..
Clock clock = Clock.systemDefaultZone();
And the JLabel setText() method as following..
label.setText(clock);
I'm aware that setText() method only works with String parameter. But since clock variable can't be converted to String, i really have no idea how to display it on the label. Or what should i use instead of JLabel to display Clock variables.
I tried label.setText(Clock.valueOf(clock));, until later i realized that only works for primitive data (or no?). I really stuck at this moment.
I'm going to assume you're using java.time.Clock. You can convert the current time into an ISO-8601 string by doing clock.instant().toString().
Therefore, you can put the clock in the label using:
label.setText(clock.instant().toString());
If you want the label to show the clock updating, you may need to use something like a Timer to reset the label text every second.
You can change the formatting if you don't want ISO-8601 with this to set your own format.
JLabel will set the text to an object
if you have overridden the toString() method
and call to it..
As
System.out.println(clock); //this would automatically call the toString() method.
For this.. You would have to directly call it.
label.setText(clock.toString());

Funny Behaviour in GWT CellTable?

I've created a test case of my CellTable issue as a self contained
panel that can easily be imported into any project:
http://pastebin.com/zDLPKUNh
I've also posted this question on Google Groups GWT discussions.
Basically I have two Date class fields in my row model, a startDate
and an endDate. Each Date has two columns in the CellTable (called
batchTable), one to display the actual date (a DatePickerCell) and the
other being a text input cell for the time.
When the FieldUpdater of the startTime or endTime is fired we parse
the value, save the new time and call batchTable.setRowData() with the
updated object and row index.
The problem is that when FieldUpdater is fired, the cells do not
update?
I specifically edited the FieldUpdater of the endTime cell to
be an hour later than what it was set at.
I've checked as best as I can that all the gets and sets of the
respective startDate and endDate are in order, but I'm thinking that
there's something about CellTable I'm not getting.
Apologies if I've missed anything.
I'm running:
GWT 2.3
I've tested it in the latest Chrome and IE9.
Regards,
Julian
According to the google docs it should be enough to just change the field in your object and then call
cellTable.redraw();
in the update() method.
You could also try to call setRowData on your ListDataProvider instead of your CellTable. However calling setRowData on the ListDataProvider would probably end up in a call to every added display of the ListDataProvider which would be the same as calling setRowData on the CellTable directly.
I faced the same problem. In my case I had visiblerange set incorrectly. First parameter is index and second parameter is length. Make sure those two are set up correctly. After that the cell table displays correctly. I did not need to call redraw.

How can I hide a DatePicker icon when its DateTextField is hidden?

I'm using a DatePicker (org.apache.wicket.extensions.yui.calendar.DatePicker — Javadoc) in a form. The form consists of two fields. The first field is a dropdown, and the second changes dynamically based on the first. If "text" is selected, a textbox comes up; if "list" is selected, a dropdown menu comes up; and if "date" is selected, a date picker and associated field come up.
Here's a simplified version of my current code:
DateTextField dateField = new DateTextField("dateField", // ...
DatePicker datePicker = new DatePicker();
dateField.add(datePicker);
fieldOne.add(new OnChangeAjaxBehavior() {
protected void onUpdate(AjaxRequestTarget target) {
if (fieldOne.equalsIgnoreCase("list"))
{
dateField.setVisible(false);
// datePicker.setVisible(false); // This line is impossible
listField.setVisible(true);
textField.setVisible(false);
}
else if { /* similar visibility settings for dates and text */ }
}
});
The form currently works properly for changing to the date picker. The problem arises when date is selected and then the user selects something else. The date field disappears, but the date picker remains on the screen. Unfortunately, it doesn't seem to have a setVisible() method, or any equivalent. In fact, it's not even a true Wicket Component.
How can I make them appear/disappear together? If I can't set the picker's visibility directly, can I tie it to the field's visibility somehow?
By far the easiest yet still fairly maintainable solution is to group the components you want to hide together in a WebMarkupContainer. In the markup you'll probably want to use a <div> element for the container, but this depends on what you want to hide. (Sometimes the markup itself prevents you from using this method but let's hope your markup doesn't. :))
Then just change the visibility of the container.
A good idea with AjaxListeners also is to call .setOutputMarkupId(true) on the Component you want to handle. Otherwise you are possibly not able to "locate" it.
In case you are not rendering it on load, but intend to add the component later, I think you should also call setOutputMarkupPlaceholderTag(true).
This helped me several times...

Categories

Resources