forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyDialog.java
More file actions
95 lines (76 loc) · 2.42 KB
/
MyDialog.java
File metadata and controls
95 lines (76 loc) · 2.42 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
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
/**
* Main class for game. Does window stuff and display
*/
public class MyDialog extends JDialog{
// must make instance var since we need it to pass data
DialogPanel dialogPanel;
public MyDialog(JFrame parent){
// must call JDialog constructor,
// final arg true makes show() blocking.
super(parent,"Color Dialog",true);
setSize(200,200); // size in pixels
// new default close action -- equiv to setVisible(false)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Add panel
dialogPanel= new DialogPanel(this);
getContentPane().add(dialogPanel);
}
public Color getCurrent(){
// just forward request
return(dialogPanel.getCurrent());
}
}
class DialogPanel extends JPanel{
// the dialog that contains me, needed to call isVisible(false)
JDialog dialog;
// This color is not used to draw, it is just the 'model' that
// this dialog alters. This data is retrieved by the main app
// through the getCurrent9) method
Color current = Color.black;
// Handler for Mouse presses and clicks
// Extends MouseAdapator
// (which implements MouseListener with empty methods)
// We use an inner class so we can access to data on JPanel
JButton toggle = new JButton("Toggle");
JButton green = new JButton("Green");
JButton ok = new JButton("OK");
// Button listener. Now an inner class so we
// can access mypanel data.
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// check source, if green button, set color
if(e.getSource() == green){
current = Color.green;
}
else if(e.getSource() == toggle){ // do toggle stuff
if(current.equals(Color.red))
current = Color.blue;
else
current = Color.red;
}
else{ // OK
// setVisible(false) close dialog and causes show() to return!
dialog.hide();
}
}
}
DialogPanel(JDialog dialog){
// make one instance of listener and add to all
this.dialog = dialog;
ButtonHandler b = new ButtonHandler();
toggle.addActionListener(b);
green.addActionListener(b);
ok.addActionListener(b);
// add buttons (and text field)
add(toggle);
add(green);
add(ok);
}
public Color getCurrent(){
return(current);
}
}