0

So I want to create a subclass of JSpinner so that I can hide all the configuration. The problem is when I put this object on JFrame, I get UI not found error.

I can't find out what I missed..

public class Time extends JSpinner {

    public Time() {
        super();
        SpinnerDateModel SpinnerModel = new SpinnerDateModel();
        this.setModel(SpinnerModel);

        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(this, "hh:mm a");
        this.setEditor(dateEditor);
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
user2628641
  • 2,035
  • 4
  • 29
  • 45

2 Answers2

2

Worked for me. Try it like this :

class Test {
    public static void main(String[] f) {
        JFrame myFrame = new JFrame();
        myFrame.add(new Time());
        myFrame.setVisible(true);
    }
}

class Time extends JSpinner {

    public Time() {
        super();
        SpinnerDateModel SpinnerModel = new SpinnerDateModel();
        this.setModel(SpinnerModel);

        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(this, "hh:mm a");
        this.setEditor(dateEditor);
    }
}
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225
2

I want to create a subclass of JSpinner so that I can hide all the configuration.

It's difficult to justify extending JSpinner in this context. As an alternative, consider using a factory method to create and configure the spinner.

private JSpinner createSpinner() {
    JSpinner spinner = new JSpinner();
    SpinnerDateModel SpinnerModel = new SpinnerDateModel();
    spinner.setModel(SpinnerModel);
    JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(spinner, "hh:mm a");
    spinner.setEditor(dateEditor);
    return spinner;
}

A related example is examined here; see the edit history for details.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045