Add days to a Java textfield through combobox

两盒软妹~` 提交于 2019-12-11 05:46:05

问题


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) {                                                

}                                               

回答1:


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



来源:https://stackoverflow.com/questions/33537545/add-days-to-a-java-textfield-through-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!