-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueFrame.java
More file actions
186 lines (160 loc) · 4.38 KB
/
ValueFrame.java
File metadata and controls
186 lines (160 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/** @author Gergely Kota
ValueFrame is a popup menu that prompts for a value to select.
It shows a JTextField in a modal JDialog, and senses when
a value has been selected. At this point, it minimizes the window
thereby allowing the program flow to continue. The selected value
is then returned.
*/
public class ValueFrame extends JDialog
{
private ValueChooser ic;
private String val;
private boolean cancel;
/** return value when cancel is selected */
public static final int ILLEGAL = Integer.MIN_VALUE;
private static final int UNSET = Integer.MAX_VALUE;
private static ValueFrame instance;
// initializer creates the single instance the first time
// the class is accessed
static
{
instance = new ValueFrame();
}
// force single instance of IntFrame via static initializer
// this is because the same task is done each time - the window
// pops up and returns a selected color.
private ValueFrame()
{
// setSize(400, 300);
setModal(true);
setLocation(300, 300);
setResizable(false);
getContentPane().add(ic = new ValueChooser(), BorderLayout.CENTER);
JPanel jp = new JPanel(new GridLayout(1,2));
JButton jb1 = new JButton("Select");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
val = ic.get();
}
});
jp.add(jb1);
JButton jb2 = new JButton("Cancel");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cancel = true;
}
});
jp.add(jb2);
getContentPane().add(jp, BorderLayout.SOUTH);
pack();
}
public static synchronized String getString(String title)
{
instance.setTitle(title);
instance.val = null;
instance.cancel = false;
Timer check = new Timer(2, null);
check.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(instance.val != null)
instance.hide();
if(instance.cancel)
instance.hide();
}
});
check.start();
instance.show();
check.stop();
return instance.val;
}
/** pauses the flow of the program until a String is entered.
It then returns that String and allows the calling program to continue.
@return the entered String
*/
public static synchronized String getString()
{
return getString("Enter a String");
}
/** pauses the flow of the program until an int is entered.
It then returns that int and allows the calling program to continue.
@return the entered int
*/
public static synchronized int getInt(String title)
{
try
{
String s = getString(title);
if(s == null)
return ILLEGAL;
return Integer.parseInt(s);
}
catch(Exception e) {return getInt();}
}
public static synchronized int getInt()
{
return getInt("Enter an Int");
}
/** pauses the flow of the program until a double is entered.
It then returns that double and allows the calling program to continue.
@return the entered double
*/
public static synchronized double getDouble(String title)
{
try
{
String s = getString(title);
if(s == null)
return (double) ILLEGAL;
return Double.parseDouble(s);
}
catch(Exception e) {return getDouble();}
}
public static synchronized double getDouble()
{
return getDouble("Enter a Double");
}
// customized JPanel with a JTextField and reader methods.
private class ValueChooser extends JPanel
{
private JTextField jtf;
public ValueChooser()
{
setPreferredSize(new Dimension(150,30));
setLayout(new BorderLayout());
add(jtf = new JTextField());
}
public String get()
{
try
{
val = jtf.getText();
jtf.setText("");
return val;
}
catch(Exception e) {return null;}
}
}
/* -------------------------------------------------- */
/* -------------------------------------------------- */
/* -------------------------------------------------- */
public static void main(String[] args)
{
System.out.println("Started");
double i = ValueFrame.getDouble();
System.out.println("Ended");
System.out.println(i);
try
{
Thread.sleep(2000);
}
catch(Exception e) {}
System.out.println("Started");
i = ValueFrame.getDouble();
System.out.println("Ended");
System.out.println(i == ILLEGAL);
System.out.println(i);
}
}