java - JOptionPane 或 JFrame 从 JCalendar 返回值

标签 java swing jcalendar

是否可以从 JFrame 或 JOptionPane 返回一个值,其中我有来自 Flib 的 Jcalendar? JCalendar需要自己的Listener类。我有这个:

 org.freixas.jcalendar.JCalendar jd = new org.freixas.jcalendar.JCalendar(org.freixas.jcalendar.JCalendar.DISPLAY_DATE | org.freixas.jcalendar.JCalendar.DISPLAY_TIME,true);
    KalendarPanel kalendar = new KalendarPanel();
    String message = "Choose start date:\n";
    Object[] params = {message, jd};
   JOptionPane.showConfirmDialog(null, params, "Start date", JOptionPane.PLAIN_MESSAGE);

但我不知道如何添加 Listener 类以及如何从 JOptionPane 返回值。在 JFrame 中,我可以添加类监听器,但我不知道如何将值返回给父 JFrame。 JFrame代码:

public class Kalendar extends javax.swing.JFrame {

private Calendar date;
private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:MM:ss");
private String datum;


public Kalendar() {
    initComponents();
    jCalendar1.addDateListener(new MyDateListener());
}  

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jCalendar1 = new org.freixas.jcalendar.JCalendar();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jCalendar1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
    );

    pack();
}// </editor-fold>                        


// Variables declaration - do not modify                     
private org.freixas.jcalendar.JCalendar jCalendar1;
// End of variables declaration                   

class MyDateListener implements DateListener {

    @Override
    public void dateChanged(DateEvent e) {
        datum = sdf.format(e.getSelectedDate().getTime());
    }
}

非常感谢。

最佳答案

这里有一个适合您的解决方案。我希望您可以在您的组件 (JCalendar) 中使用这种方法。

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class ListOptionTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                String[] data = new String[10];
                for (int i = 0; i < 10; i++) {
                    data[i] = "Item " + (i + 1);
                }
                final JList<String> list = new JList<>(data);
                list.addListSelectionListener(new ListSelectionListener() {

                    @Override
                    public void valueChanged(ListSelectionEvent e) {
                        if (!e.getValueIsAdjusting()) {
                            System.out.println("Current selection: " + list.getSelectedValuesList());
                        }
                    }
                });
                int result = JOptionPane.showConfirmDialog(null, new JScrollPane(list), "Make your choice",
                        JOptionPane.OK_CANCEL_OPTION);
                if (result == JOptionPane.OK_OPTION) {
                    System.out.println("Final selection: " + list.getSelectedValuesList());
                } else {
                    System.out.println("No choice");
                }
            }
        });
    }
}

关于java - JOptionPane 或 JFrame 从 JCalendar 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850504/

相关文章:

java - 绑定(bind)错误 : No implementation was bound - Guice

java - Java 中的 SSL 套接字

java - 在 JOptionPane 中显示 JDateChooser

java - 将焦点设置在 JDateChooser 上

java - 在 JDateChooser 中选择日期时,日期不正确。为什么?

java - JNA:将结构指针设置为 NULL

java - Runnable .jar 和 Eclipse run 之间的行为非常不同

java - 这个按位运算符的实现有何意义?

java - 如何让 NVDA 读取 Swing/GUI 元素

java - 按下保存按钮时保存 Jtable 数据