source: src/main/java/genius/gui/panels/DefaultOKCancelDialog.java@ 209

Last change on this file since 209 was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.6 KB
Line 
1package genius.gui.panels;
2
3import java.awt.BorderLayout;
4import java.awt.Frame;
5import java.awt.Panel;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8
9import javax.swing.JButton;
10import javax.swing.JDialog;
11
12/**
13 * open a modal OK/Cancel dialog. you must implement ok() and getPanel() to make
14 * this working. Panel opens, gets result and returns result only when you call
15 * getResult() Typical use: new YourOKCancelDialog(yourframe).getResult();
16 *
17 * @author wouter 19aug08
18 *
19 */
20public abstract class DefaultOKCancelDialog extends JDialog {
21
22 private static final long serialVersionUID = 9106032262508127313L;
23 JButton okbutton = new JButton("OK");
24 JButton cancelbutton = new JButton("Cancel");
25 Object the_result = null; // will be set after ok button is pressed. null in
26 // other cases (eg cancel)
27
28 /**
29 *
30 * @param owner
31 * is the parent frame, used only to center the dialog properly.
32 * Probably can be null
33 * @param title
34 * the title of the dialog
35 */
36 public DefaultOKCancelDialog(Frame owner, String title) {
37 super(owner, title, true); // modal dialog.
38 getContentPane().setLayout(new BorderLayout());
39 }
40
41 /**
42 * this function computes the result of the dialog. You may return null if
43 * the user entered illegal choices or somehow cancelled the dialog. This
44 * function will only be called when user presses OK button, which also
45 * finishes the dialog and closes the dialog window.
46 */
47 public abstract Object ok();
48
49 /**
50 * this fucnction returns the actual contents for the dialog panel I
51 * implemented this as a function, because we need it before opening the
52 * window.
53 *
54 * @return a Panel containing the actual dialog contents.
55 *
56 */
57 public abstract Panel getPanel();
58
59 /** call this to get the result. Do not override, instead override ok(). */
60 public Object getResult() {
61 // actionlisteners MUST be added before putting buttons in panel!
62 okbutton.addActionListener(new ActionListener() {
63 public void actionPerformed(ActionEvent e) {
64 // System.out.println("OK pressed");
65 the_result = ok();
66 setVisible(false);
67 }
68 });
69
70 cancelbutton.addActionListener(new ActionListener() {
71 public void actionPerformed(ActionEvent e) {
72 // System.out.println("cancel pressed");
73 setVisible(false);
74 }
75 });
76
77 Panel buttonrow = new Panel(new BorderLayout());
78 buttonrow.add(okbutton, BorderLayout.WEST);
79 buttonrow.add(cancelbutton, BorderLayout.EAST);
80
81 add(buttonrow, BorderLayout.SOUTH);
82
83 add(getPanel(), BorderLayout.CENTER);
84
85 pack();
86 setLocationRelativeTo(getOwner());
87 setVisible(true); // block until closing window.
88 return the_result;
89 }
90}
Note: See TracBrowser for help on using the repository browser.