dateChooser设置今天的日期。

huangapple go评论152阅读模式
英文:

dateChooser set todays date

问题

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!");
    }
}
英文:

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!

dateChooser设置今天的日期。

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

得分: 1

JCalendar 构造函数 包括至少 3 种变体,似乎允许设置初始或“默认”日期。当涉及实现目标时,API 的 Java 文档应该是首先检查的内容。

给定我编写了这个最小的可复现示例,以便在上面的评论中看到图片,我还将分享如何使用 JSpinner 完成相同的操作。如果没有其他有用的东西,它演示了如何在所有 3 个相关的 JCalendar 构造函数中获取 现在的 Date

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);
    }
}

若在原始代码中无法使其工作,请编辑添加一个 MRE(最小可复现示例)。注意,这个可以复制粘贴并运行的示例比问题中包含的不可编译的代码片段要短。

英文:

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.

dateChooser设置今天的日期。


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.

<img src="https://i.stack.imgur.com/s73o6.png">

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 = () -&gt; {
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.

huangapple
  • 本文由 发表于 2020年6月29日 17:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/62635465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定