0

I want to perform some function in FocusEvent of JDatePicker. I am using below code for implementing 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);
            }

        });
    }
});

This code not working. Is any error in this code?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
User123
  • 71
  • 3
  • 14

1 Answers1

1

I'm not a particular fan of JDatePicker, for a number of personal reasons.

You could implement your own version which provided you with the functionality your after or you could try SwingLabs, SwingX JXDatePicker instead, for example

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

    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Property(Change)Support/Listener – mKorbel Jun 01 '15 at 10:27
  • @mKorbel To what? `PropertyChangeListener` is about as good as adding a `ChangeListener` to the `UtilDateModel` for `JDatePicker`, which we've established isn't sufficient for the OP's needs – MadProgrammer Jun 01 '15 at 10:28
  • there is/are implemented Property(Change)Support/Listener, because whats reason to add FocusListener to derived JSpinner / JFormattedTextField, maybe only if the value changes then is reason for firing an event, – mKorbel Jun 01 '15 at 10:33
  • @mKorbel I don't know why the OP wants to do something when the field is focused, I just know they don't care about monitoring state changes. Go ask the OP why they need this particular feature :P – MadProgrammer Jun 01 '15 at 10:34
  • agree as aside Focus is there implemented and I think (most popular JXxxDate/Calendar) that correctly, because in most cases Selection with Caret works (last Caret possition is top) – mKorbel Jun 01 '15 at 10:37
  • I'm awaiting that, missing there, in your answer == Focus works with/for derived JSpinner / J(Formatted)TextField only, that is fully qualified nonsence – mKorbel Jun 01 '15 at 10:39
  • @mKorbel Sorry, I miss your point. The example is runnable and clearly demonstrates that a `focusGained` event is triggered when focus moves into the field. The `JXDatePicker` exposes the `JFormattedField`. Would you suggest trying to add it directly to the `JXDatePicker`, cause that would be my personal preference, assuming it worked – MadProgrammer Jun 01 '15 at 10:44
  • I already added JXDatepicker and all worked fine for me. In my application in component FocusEvent I want to highlight the value of that component in Image.So I ask this question. – User123 Jun 01 '15 at 11:10