01: package com.silvermindsoftware.hitch;
02:
03: import com.silvermindsoftware.hitch.swing.EnhancedSpinnerListModel;
04: import com.silvermindsoftware.hitch.swing.EnhancedSpinnerListFormatter;
05:
06: import javax.swing.*;
07: import javax.swing.text.DefaultFormatterFactory;
08: import java.util.List;
09:
10: /**
11: * Copyright 2007 Brandon Goodin
12: *
13: * Licensed under the Apache License, Version 2.0 (the "License");
14: * you may not use this file except in compliance with the License.
15: * You may obtain a copy of the License at
16: *
17: * http://www.apache.org/licenses/LICENSE-2.0
18: *
19: * Unless required by applicable law or agreed to in writing, software
20: * distributed under the License is distributed on an "AS IS" BASIS,
21: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22: * See the License for the specific language governing permissions and
23: * limitations under the License.
24: */
25:
26: public class HitchUtil {
27:
28: /**
29: * Use this when you are using a JSpinner with a list of complex objects. The
30: * standard ListFormatter fails to retain the value of the object in the
31: * JFormattedTextField when using auto-completion and a commitEdit is called.
32: * This method will enhance the JSpinner to work properly and retain the found
33: * object.
34: *
35: * @param items
36: * @return
37: */
38: public static JSpinner getEnhancedJSpinner(List items) {
39:
40: EnhancedSpinnerListModel slm = new EnhancedSpinnerListModel(
41: items);
42:
43: JSpinner spinner = new JSpinner(slm);
44:
45: // set the proper formatter on the spinner editor's JFormattedTextField
46: ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField()
47: .setFormatterFactory(
48: new DefaultFormatterFactory(
49: new EnhancedSpinnerListFormatter(slm)));
50:
51: return spinner;
52: }
53:
54: /**
55: * @see com.silvermindsoftware.hitch.HitchUtil#getEnhancedJSpinner(java.util.List)
56: *
57: * @param items
58: * @return
59: */
60: public static JSpinner getEnhancedJSpinner(Object[] items) {
61:
62: EnhancedSpinnerListModel slm = new EnhancedSpinnerListModel(
63: items);
64:
65: JSpinner spinner = new JSpinner(slm);
66:
67: // set the proper formatter on the spinner editor's JFormattedTextField
68: ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField()
69: .setFormatterFactory(
70: new DefaultFormatterFactory(
71: new EnhancedSpinnerListFormatter(slm)));
72:
73: return spinner;
74: }
75:
76: }
|