1 package wbKerho;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.Frame;
6
7 import javax.swing.JButton;
8 import javax.swing.JComponent;
9 import javax.swing.JDialog;
10 import javax.swing.JOptionPane;
11 import javax.swing.JPanel;
12 import javax.swing.KeyStroke;
13 import javax.swing.UIManager;
14 import javax.swing.border.EmptyBorder;
15
16 import fi.jyu.mit.gui.EditPanel;
17
18 import java.awt.event.ActionListener;
19 import java.awt.event.ActionEvent;
20 import java.awt.event.KeyEvent;
21
22
28 public class KerhonNimiDialog extends JDialog {
29
30 private static final long serialVersionUID = 1L;
31
32 private final JPanel contentPanel = new JPanel();
33 private EditPanel editPanel = new EditPanel();
34
35
36
40 public static void main(String[] args) {
41 try {
42 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
43 } catch (Throwable e) {
44 e.printStackTrace();
45 }
46 String nimi = askName("kelmit");
47 JOptionPane.showMessageDialog(null,"Syötit: " + nimi);
48 }
49
50
51
56 public static String askName(String defName) {
57 String ret = null;
58 KerhonNimiDialog dialog = new KerhonNimiDialog(null);
59 dialog.editPanel.setText(defName);
60 dialog.setVisible(true);
61 ret = dialog.getResult();
62 dialog.dispose();
63 return ret;
64 }
65
66
67 private String result = null;
68
69
70
74 public String getResult() {
75 return result;
76 }
77
78
79
83 protected void setResult(String value) {
84 result = value;
85 }
86
87
91 protected EditPanel getEditPanel() {
92 return editPanel;
93 }
94
95
96
97
98
102 public KerhonNimiDialog(Frame parent) {
103 super(parent,"Nimi",true);
104 setTitle("Kerho");
105 setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);
106 setBounds(100, 100, 371, 268);
107 getContentPane().setLayout(new BorderLayout());
108 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
109
110 contentPanel.registerKeyboardAction(new ActionListener() {
111 @Override
112 public void actionPerformed(ActionEvent actionEvent) {
113 setResult(null);
114 setVisible(false);
115 }
116 }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
117
118 getContentPane().add(contentPanel, BorderLayout.SOUTH);
119 contentPanel.setLayout(new BorderLayout(0, 0));
120
121 editPanel.setColumns(20);
122 editPanel.setCaption("Anna kerhon nimi");
123 contentPanel.add(editPanel, BorderLayout.NORTH);
124 JPanel buttonPane = new JPanel();
125 contentPanel.add(buttonPane, BorderLayout.SOUTH);
126 buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
127
128 JButton okButton = new JButton("OK");
129 okButton.addActionListener(new ActionListener() {
130 @Override
131 public void actionPerformed(ActionEvent arg0) {
132 setResult(getEditPanel().getText());
133 setVisible(false);
134 }
135 });
136 buttonPane.add(okButton);
137 getRootPane().setDefaultButton(okButton);
138
139 JButton cancelButton = new JButton("Cancel");
140 cancelButton.addActionListener(new ActionListener() {
141 @Override
142 public void actionPerformed(ActionEvent arg0) {
143 setResult(null);
144 setVisible(false);
145 }
146 });
147 cancelButton.setActionCommand("Cancel");
148 buttonPane.add(cancelButton);
149
150 TiedotPanel tiedotPanel = new TiedotPanel();
151 getContentPane().add(tiedotPanel, BorderLayout.CENTER);
152 }
153
154
155
156 }
157