dateChooser set todays date

215 Views Asked by At

I want to set the todays/actual date in datechooser field automatically. I've looked a couple of Stackoverflow topics about this and couldnt find a way to complete this. I dont want to output the date with system.out.println(); but I just want to save it to database (MSSQL). So for example let me show you a photo of what I mean and if you have any questions about code I can send it here, but since I dont know what code to put here that will help you I will leave it blank. THANK YOU IN ADVANCE!

enter image description here

public String getDateToString(Date d) {
    DateFormat da = new SimpleDateFormat("dd-MM-yyyy");
    return da.format(d);
}

private void tabelaSelectedIndexChange() {
    final ListSelectionModel rowSM = table.getSelectionModel();
    rowSM.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                return;
            }

            ListSelectionModel rowSM = (ListSelectionModel) e.getSource();
            int selectedIndex = rowSM.getAnchorSelectionIndex();
            if (selectedIndex > -1) {
                Feedback f = ftm.getFeedback(selectedIndex);
                //idField.setText(p.getId().toString());
                arsyejaArea.setText(f.getArsyeja());
                dateChooser.setDate(f.getData());
                rcmb.setSelectedItem(f.getRecetaID());
                recetaCMB.repaint();
            }
        }
    });
}

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    try {
        int row = table.getSelectedRow();

        if (arsyejaArea.getText().trim().isEmpty() || recetaCMB.getSelectedItem() == null || dateChooser.getDate() == null) {
            JOptionPane.showMessageDialog(this, "Nuk i keni mbushur te gjitha hapesirat!");
        } else if (row == -1) {
            Feedback f = new Feedback();
            //p.setId(Integer.parseInt(idField.getText()));
            f.setArsyeja(arsyejaArea.getText());
            f.setData(dateChooser.getDate());
            f.setRecetaID((Receta) rcmb.getSelectedItem());
            fr.create(f);
        } else {
            Feedback f = ftm.getFeedback(row);
            //Id nuk e lejojm me ndryshu vetem emertimin ose fielda tjere qe mundeni me pas
            f.setArsyeja(arsyejaArea.getText());
            f.setData(dateChooser.getDate());
            f.setRecetaID((Receta) rcmb.getSelectedItem());
            fr.edit(f);
        }
        //E krijojm ni metode per me i clear fieldat mbasi ti shtojme
        clear();
        loadTable();
    } catch (CrudFormException ex) {
        JOptionPane.showMessageDialog(this, "E dhena ekziston!");
    }
}
1

There are 1 best solutions below

0
Andrew Thompson On

The JCalendar constructors include at least 3 variants which seem to allow setting the initial or 'default' date. The Java Docs of an API should be the first thing checked when there are questions in regard to achieving a goal.

enter image description here


Given I wrote this minimal reproducible example to make the image seen in a comment above, I'll also share how to do it with a JSpinner. If nothing else of use, it demonstrates how to get the Date of .. right now as mentioned in all 3 of the relevant JCalendar constructors.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.*;

public class DefaultDateInSpinner {

    private JComponent ui = null;
    Date nowDate = new Date(System.currentTimeMillis()); // right NOW!
    Date endDate = new Date(System.currentTimeMillis()+1000000);
    SpinnerDateModel dateModel = new SpinnerDateModel(
            nowDate, nowDate, endDate, Calendar.NARROW_FORMAT);

    DefaultDateInSpinner() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));
        
        JSpinner spinner = new JSpinner(dateModel);
        ui.add(spinner, BorderLayout.LINE_START);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            DefaultDateInSpinner o = new DefaultDateInSpinner();
            
            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);
            
            f.setContentPane(o.getUI());
            f.pack();
            f.setMinimumSize(f.getSize());
            
            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

If unable to make it work in the original code, edit to add an MRE. Note also that this example which can be copy/pasted and run, is shorter than the uncompilable code snippets included in the question.