6

I am using a JDateChooser to allow the user to input date. How do I disable editing option on the text field that appears? I would not want the user to type anything in that text field - input should only be entered by clicking from the calendar. How do I achieve that?

Below is my code:

public class PQReport {
// product quotations report

public JPanel panel;

public PQReport() {
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(125, 300));
    initUI();

}

public void initUI() {
    panel.setLayout(new net.miginfocom.swing.MigLayout());
    JDateChooser chooser = new JDateChooser();
    chooser.setLocale(Locale.US);
    //chooser.isEditable(false);
    chooser.setDateFormatString("yyyy-MM-dd");
    panel.add(new JLabel("Date of Birth:"));
    panel.add(chooser);
    panel.add(new JButton("click"));
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            PQReport rep = new PQReport();
            JFrame f = new JFrame();
            f.setSize(400, 400);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(rep.panel);

        }
    });
    }
 }
Kiki
  • 2,243
  • 5
  • 30
  • 43
CN1002
  • 1,115
  • 3
  • 20
  • 40
  • You have to call `setVisible(true)` after you add all the components. Also, call `pack()` instead of `setSize` after adding everything and before displaying. – user1803551 May 14 '15 at 12:03
  • A text field is used for input. If you don't want it to be editable, use a `JLabel` instead to display the information. Otherwise, you can call `setEditable(false)`. – user1803551 May 14 '15 at 12:06

3 Answers3

14

One approach is to invoke setEditable(false) on the chooser's IDateEditor, JTextFieldDateEditor.

JDateChooser chooser = new JDateChooser();
JTextFieldDateEditor editor = (JTextFieldDateEditor) chooser.getDateEditor();
editor.setEditable(false);

This allows a new date to be chosen, while forbidding changes via the JTextField itself:

set editable false

The alternative, chooser.setEnabled(false), has the effect of disabling the JDateChooser entirely.

set enabled false

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • @MadhukaDilhan: Thank you for commenting; while either approach works, I've update the answer to clarify the difference. – trashgod Jun 01 '16 at 10:14
  • I put your code. it was disable but using chooser can choose the date – Madhuka Dilhan Jun 01 '16 at 11:56
  • @MadhukaDilhan: Correct; your [answer](http://stackoverflow.com/a/37559448/230513) is a good alternative, if the disabled state is acceptable; I inferred that Giovanrich wanted to preclude _editing the text_, not _changing the date_. – trashgod Jun 01 '16 at 12:03
  • When disable chooser using chooser.setEnabled(false) it was OK.But it doesn't enable after chooser.setEnabled(true) .. – chamzz.dot Jan 08 '18 at 08:13
  • Works for me; post a question if you have trouble. – trashgod Jan 08 '18 at 19:13
3

Alternatively, try this code that disables the JDateChooser entirely:

JDateChooser chooser = new JDateChooser();
chooser.setEnabled(false);

image

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Madhuka Dilhan
  • 1,396
  • 1
  • 14
  • 21
  • This is a valid alternative, but it has a different effect, illustrated [here](http://stackoverflow.com/a/30240464/230513). – trashgod Jun 01 '16 at 10:16
1

You have to put:

editor.setEnabled(false);

instead of:

editor.setEditable(false);

So the right code is (I tested it):

JDateChooser chooser = new JDateChooser();
JTextFieldDateEditor editor = (JTextFieldDateEditor) chooser.getDateEditor();
editor.setEnabled(false);
Zain Aftab
  • 703
  • 7
  • 21
bradai
  • 11
  • 1