error with jdatechooser cannot find method in java - java

I am getting an error that it can't find the method getModel() and I am not sure what to do about this. I am trying to make it so that when a button is clicked, the dob variable is set to the value in the jdatechooser.
public Calendar getDOB()
{
return dob;
}
JDateChooser jdc = new JDateChooser();
JCalendar jc = new JCalendar();
Calendar calendar;
UtilDateModel model = new UtilDateModel();
model.setDate(1990, 8, 24);
JDatePanelImpl datePanel;
JDatePickerImpl datePicker;
Person samplePerson = new Person();
btnSubmit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
samplePerson.setDOB(calendar.getModel().getValue());
}
});

You're calling the getModel method on your Calendar instance (which doesn't have a getModel method) instead of on your JDatePanel instance

nevermind! I got it! The following line worked for me.
samplePerson.setDOB(jdc.getCalendar());

Related

How to detect which week and month are clicked on a JCalendar?

I'm creating a Java GUI calendar application using the JCalendar Library, and I want to be able to detect which week of which month was clicked by the user so I can then open a new JFrame.
This is the code I have right now, I just sysouted the DAY_OF_WEEK_IN_MONTH because I wanted to see if it was working, but all I get are singular int values, which are not useful. Any advice would be greatly appreciated.
EDIT: The JCalendar Library I'm using is https://toedter.com/jcalendar/.
JCalendar calendar = new JCalendar();
calendar.setBounds(416, 70, 304, 243);
frame.getContentPane().add(calendar);
calendar.addPropertyChangeListener("calendar", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
final Calendar c = (Calendar) e.getNewValue();
System.out.println(c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
}
});

Java-JDatePicker Not Displaying anything

i am trying to get the date from the JDatePicker but my problem is eventhough i selected the date its not displaying in the result textbox.
here is my code
can anyone help to how to solve this problem.
public class Datepicker extends JFrame {Datepicker(){
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());
UtilDateModel model=new UtilDateModel();
Properties p=new Properties();
p.put("text.today","Today");
p.put("text.month","Month");
p.put("text.year","Year");
JDatePanelImpl panel=new JDatePanelImpl(model, p);
JDatePickerImpl datepic=new JDatePickerImpl(panel,null);
datepic.setBounds(220,350,120,30);
frame.add(datepic);
Date selectedDate=(Date)datepic.getModel().getValue();
frame.setSize(300, 300);
frame.setVisible(true);}
ThankYou.
Please, instead of
JDatePickerImpl datepic=new JDatePickerImpl(panel,null);
do this as follow, it's worked for me :
datePicker = new JDatePickerImpl(datePanel,new DateLabelFormatter());
public class DateLabelFormatter extends AbstractFormatter {
private String datePattern = "yyyy-MM-dd";
private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);
#Override
public Object stringToValue(String text) throws ParseException {
return dateFormatter.parseObject(text);
}
#Override
public String valueToString(Object value) throws ParseException {
if (value != null) {
Calendar cal = (Calendar) value;
return dateFormatter.format(cal.getTime());
}
return "";
}
}

Add days to a Java textfield through combobox

LINK TO GUI IMAGE HERE -------> http://imgur.com/uPD0K5S
public class MainMenu extends javax.swing.JFrame {
public MainMenu() {
initComponents();
cmbRoomNumber.setEnabled(false);
jPanel1.setVisible(false);
btnBook.setEnabled(false);
//SETTING COMBOBOXES TO NONE
cmbPhotoId.setSelectedIndex(-1);
cmbStayDuration.setSelectedIndex(-1);
//LABELS VALIDATION
jlblNameVer.setVisible(false);
//SETTING DATE TODAY
Date now = new Date();
//Set date format as you want
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
this.ftxtCheckinDate.setText(sf.format(now));
}
As you can see i want to add days to Check-out Date(ftxtCheckOutDate) depending on how many days selected in the combobox(cmbStayDuration)
Im using netbeans JFrame
Thanks :)
private void cmbStayDurationActionPerformed(java.awt.event.ActionEvent evt) {
}
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, combobox number);
Basically Calendar class has a function to add days.
Get the date now, get the combo box day, then add it.
For example:
public static void main(String[] args) {
// TODO code application logic here
Calendar c = Calendar.getInstance();
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
c.setTime(d);
System.out.println(sdf.format(c.getTime()));
c.setTime(d);
c.add(Calendar.DATE, 10);
System.out.println(sdf.format(c.getTime()));
}
Output:
05/11/2015
15/11/2015
As for changing the value of Check-out Date form as the ComboBox changes, you can add either an ActionListener to listen to it change.
Example

Select "Today" in Vaadin’s calendar widgets, DateField and InlineDateField?

Vaadin has a pair of nice calendar widgets, DateField & InlineDateField.
One feature I've not detected: Can the user get back to "Today" after perusing various months and dates?
Or must I add my own separate "Today" button? At least I could do so for InlineDateField, but not DateField.
I don't believe so, I think you would have to code it yourself.
I think its the same for JodaTime too.
I'm sure you already figured this out, but here is some simple code for anyone else!
final DateField x = new DateField();
final InlineDateField y = new InlineDateField();
HorizontalLayout layout = new HorizontalLayout();
layout.setSpacing(true);
layout.addComponent(x);
layout.addComponent(y);
Button button = new Button("Today");
layout.addComponent(button);
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Date date = new Date();
x.setValue( date );
y.setValue( date );
}
});
this.setContent(layout);

Toedter Get Date From JDateChooser

I am using toedter JDateChooser, and I am having problems retrieving the date picked from it.
jDateChooser2.setDateFormatString("dd-MMMM-yy");
jDateChooser2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jDateChooser2MouseClicked(evt);
}
});
private void jDateChooser2MouseClicked(java.awt.event.MouseEvent evt) {
Date dateFromDateChooser = jDateChooser2.getDate();
System.out.println(dateFromDateChooser);
}
How can I retrieve the date? Is there a better way to do it? I think the listener is not being fired or triggered. i tried replacing the listener with:
System.out.println("triggered");
Still there are no output.
Basically, you don't want to listener for MouseEvents, as these could be changing the state of the component in a number of ways, most of which you don't want to know about.
You should be monitoring the date property change event, for example...
JDateChooser dateChooser = new JDateChooser();
dateChooser.addPropertyChangeListener("date", new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
Date date = (Date)evt.getNewValue();
System.out.println("Date changed " + date);
}
});
Just beware, this could be triggered in response to calling setDate or by the user selecting a date from the picker, generally, you won't be able to tell

Categories