0

FocusEventof で何らかの機能を実行したいJDatePicker。を実装するために以下のコードを使用していますFocusListener

Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
UtilDateModel model = new UtilDateModel();
Calendar today=Calendar.getInstance();
Date todayDate=new Date();
today.setTime(todayDate);
model.setDate(today.get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DATE));
model.setSelected(true);
JDatePanelImpl datePanel =new JDatePanelImpl(model, p);
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel,new DateLabelFormatter());

datePicker.addFocusListener(new FocusListener() {

    @Override
    public void focusLost(FocusEvent e) {
        // TODO Auto-generated method stub
        System.out.println("fcus lost");
    }

    @Override
    public void focusGained(FocusEvent e) {
        // TODO Auto-generated method stub
        System.out.println("focus gained");
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                //repaint();
                displayImage(categoryAttributeObj,imGroupObj);
            }

        });
    }
});

このコードは機能しません。このコードにエラーはありますか?

4

1 に答える 1

1

JDatePickerいくつかの個人的な理由から、私は の特定のファンではありません。

JXDatePicker後で機能を提供する独自のバージョンを実装するか、代わりにSwingLabs や SwingX を試すことができます。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.JXDatePicker;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            add(new JButton("Before"));
            JXDatePicker picker = new JXDatePicker();
            picker.getEditor().addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    System.out.println("You have foucs");
                }
            });
            add(picker);
            add(new JButton("After"));
        }

    }
}
于 2015-06-01T10:24:34.173 に答える