summaryrefslogtreecommitdiffstats
path: root/lib/example
diff options
context:
space:
mode:
Diffstat (limited to 'lib/example')
-rw-r--r--lib/example/GridBagVersusTable.java119
-rw-r--r--lib/example/GridVersusTable.java88
-rw-r--r--lib/example/MyClass.java47
-rw-r--r--lib/example/Preferred.java127
-rw-r--r--lib/example/RadTool.java620
-rw-r--r--lib/example/Simple.java54
-rw-r--r--lib/example/TypicalGui.java215
7 files changed, 598 insertions, 672 deletions
diff --git a/lib/example/GridBagVersusTable.java b/lib/example/GridBagVersusTable.java
index 53e45d6..0ed367a 100644
--- a/lib/example/GridBagVersusTable.java
+++ b/lib/example/GridBagVersusTable.java
@@ -1,140 +1,133 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Font;
5import java.awt.*; 5import java.awt.Frame;
6import java.awt.event.*; 6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.event.WindowAdapter;
9import java.awt.event.WindowEvent;
10import java.awt.event.WindowListener;
7import javax.swing.JButton; 11import javax.swing.JButton;
8import layout.TableLayout; 12import layout.TableLayout;
9import layout.TableLayoutConstraints;
10 13
11 14
15public class GridBagVersusTable {
12 16
13public class GridBagVersusTable
14{
15 17
16
17
18 protected static void makeButton 18 protected static void makeButton
19 (Frame frame, String name, GridBagLayout gridbag, GridBagConstraints c) 19 (Frame frame, String name, GridBagLayout gridbag,
20 { 20 GridBagConstraints c) {
21 JButton button = new JButton(name); 21 JButton button = new JButton(name);
22 gridbag.setConstraints(button, c); 22 gridbag.setConstraints(button, c);
23 frame.add(button); 23 frame.add(button);
24 } 24 }
25 25
26 26
27 27 protected static Frame showGridBagWindow() {
28 protected static Frame showGridBagWindow ()
29 {
30 // Create layout and contraints object 28 // Create layout and contraints object
31 GridBagLayout gridbag = new GridBagLayout(); 29 GridBagLayout gridbag = new GridBagLayout();
32 GridBagConstraints c = new GridBagConstraints(); 30 GridBagConstraints c = new GridBagConstraints();
33 31
34 // Create frame 32 // Create frame
35 Frame frame = new Frame("GridBagLayout"); 33 Frame frame = new Frame("GridBagLayout");
36 frame.setFont (new Font("Helvetica", Font.PLAIN, 14)); 34 frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
37 frame.setLayout (gridbag); 35 frame.setLayout(gridbag);
38 36
39 // Create buttons, add buttons, and apply constraints 37 // Create buttons, add buttons, and apply constraints
40 c.fill = GridBagConstraints.BOTH; 38 c.fill = GridBagConstraints.BOTH;
41 c.weightx = 1.0; 39 c.weightx = 1.0;
42 makeButton (frame, "Button1", gridbag, c); 40 makeButton(frame, "Button1", gridbag, c);
43 makeButton (frame, "Button2", gridbag, c); 41 makeButton(frame, "Button2", gridbag, c);
44 makeButton (frame, "Button3", gridbag, c); 42 makeButton(frame, "Button3", gridbag, c);
45 43
46 c.gridwidth = GridBagConstraints.REMAINDER; //end of row 44 c.gridwidth = GridBagConstraints.REMAINDER; //end of row
47 makeButton (frame, "Button4", gridbag, c); 45 makeButton(frame, "Button4", gridbag, c);
48 46
49 c.weightx = 0.0; //reset to the default 47 c.weightx = 0.0; //reset to the default
50 makeButton (frame, "Button5", gridbag, c); //another row 48 makeButton(frame, "Button5", gridbag, c); //another row
51 49
52 c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row 50 c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
53 makeButton (frame, "Button6", gridbag, c); 51 makeButton(frame, "Button6", gridbag, c);
54 52
55 c.gridwidth = GridBagConstraints.REMAINDER; //end of row 53 c.gridwidth = GridBagConstraints.REMAINDER; //end of row
56 makeButton (frame, "Button7", gridbag, c); 54 makeButton(frame, "Button7", gridbag, c);
57 55
58 c.gridwidth = 1; //reset to the default 56 c.gridwidth = 1; //reset to the default
59 c.gridheight = 2; 57 c.gridheight = 2;
60 c.weighty = 1.0; 58 c.weighty = 1.0;
61 makeButton (frame, "Button8", gridbag, c); 59 makeButton(frame, "Button8", gridbag, c);
62 60
63 c.weighty = 0.0; //reset to the default 61 c.weighty = 0.0; //reset to the default
64 c.gridwidth = GridBagConstraints.REMAINDER; //end of row 62 c.gridwidth = GridBagConstraints.REMAINDER; //end of row
65 c.gridheight = 1; //reset to the default 63 c.gridheight = 1; //reset to the default
66 makeButton (frame, "Button9", gridbag, c); 64 makeButton(frame, "Button9", gridbag, c);
67 makeButton (frame, "Button10", gridbag, c); 65 makeButton(frame, "Button10", gridbag, c);
68 66
69 // Show frame 67 // Show frame
70 frame.pack(); 68 frame.pack();
71 frame.setLocation (0, 10); 69 frame.setLocation(0, 10);
72 frame.show(); 70 frame.show();
73 71
74 return frame; 72 return frame;
75 } 73 }
76 74
77 75
78 76 protected static Frame showTableWindow() {
79 protected static Frame showTableWindow ()
80 {
81 // Create frame 77 // Create frame
82 Frame frame = new Frame("TableLayout"); 78 Frame frame = new Frame("TableLayout");
83 frame.setFont(new Font("Helvetica", Font.PLAIN, 14)); 79 frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
84 80
85 // Set layout 81 // Set layout
86 double f = TableLayout.FILL; 82 double f = TableLayout.FILL;
87 double p = TableLayout.PREFERRED; 83 double p = TableLayout.PREFERRED;
88 double size[][] = {{f, f, f, f}, {p, p, p, p, f}}; 84 double size[][] = {{f, f, f, f}, {p, p, p, p, f}};
89 85
90 TableLayout layout = new TableLayout(size); 86 TableLayout layout = new TableLayout(size);
91 frame.setLayout (layout); 87 frame.setLayout(layout);
92 88
93 // Create buttons labeled Button1 to Button10 89 // Create buttons labeled Button1 to Button10
94 int numButton = 10; 90 int numButton = 10;
95 JButton button[] = new JButton[numButton + 1]; 91 JButton button[] = new JButton[numButton + 1];
96 92
97 for (int i = 1; i <= numButton; i++) 93 for (int i = 1; i <= numButton; i++) {
98 button[i] = new JButton("Button" + i); 94 button[i] = new JButton("Button" + i);
99 95 }
96
100 // Add buttons 97 // Add buttons
101 frame.add (button[1], "0, 0"); 98 frame.add(button[1], "0, 0");
102 frame.add (button[2], "1, 0"); 99 frame.add(button[2], "1, 0");
103 frame.add (button[3], "2, 0"); 100 frame.add(button[3], "2, 0");
104 frame.add (button[4], "3, 0"); 101 frame.add(button[4], "3, 0");
105 frame.add (button[5], "0, 1, 3, 1"); 102 frame.add(button[5], "0, 1, 3, 1");
106 frame.add (button[6], "0, 2, 2, 2"); 103 frame.add(button[6], "0, 2, 2, 2");
107 frame.add (button[7], "3, 2, 3, 2"); 104 frame.add(button[7], "3, 2, 3, 2");
108 frame.add (button[8], "0, 3, 0, 4"); 105 frame.add(button[8], "0, 3, 0, 4");
109 frame.add (button[9], "1, 3, 3, 3"); 106 frame.add(button[9], "1, 3, 3, 3");
110 frame.add (button[10], "1, 4, 3, 4"); 107 frame.add(button[10], "1, 4, 3, 4");
111 108
112 // Show frame 109 // Show frame
113 frame.pack(); 110 frame.pack();
114 frame.setLocation (400, 10); 111 frame.setLocation(400, 10);
115 frame.show(); 112 frame.show();
116 113
117 return frame; 114 return frame;
118 } 115 }
119 116
120
121 117
122 public static void main (String args[]) 118 public static void main(String args[]) {
123 {
124 WindowListener listener = 119 WindowListener listener =
125 (new WindowAdapter() 120 (new WindowAdapter() {
126 { 121 public void windowClosing(WindowEvent e) {
127 public void windowClosing (WindowEvent e) 122 System.exit(0);
128 {
129 System.exit (0);
130 }
131 } 123 }
124 }
132 ); 125 );
133 126
134 Frame frame = showGridBagWindow(); 127 Frame frame = showGridBagWindow();
135 frame.addWindowListener (listener); 128 frame.addWindowListener(listener);
136 129
137 frame = showTableWindow(); 130 frame = showTableWindow();
138 frame.addWindowListener (listener); 131 frame.addWindowListener(listener);
139 } 132 }
140} 133}
diff --git a/lib/example/GridVersusTable.java b/lib/example/GridVersusTable.java
index c5746c4..890d2e9 100644
--- a/lib/example/GridVersusTable.java
+++ b/lib/example/GridVersusTable.java
@@ -1,108 +1,100 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Font;
5import java.awt.*; 5import java.awt.Frame;
6import java.awt.event.*; 6import java.awt.GridLayout;
7import java.awt.event.WindowAdapter;
8import java.awt.event.WindowEvent;
9import java.awt.event.WindowListener;
7import javax.swing.JButton; 10import javax.swing.JButton;
8import layout.TableLayout; 11import layout.TableLayout;
9 12
10 13
11
12public class GridVersusTable { 14public class GridVersusTable {
13 15
14 16
15 17 protected static Frame showGridWindow() {
16 protected static Frame showGridWindow ()
17 {
18 // Create frame 18 // Create frame
19 Frame frame = new Frame("GridLayout"); 19 Frame frame = new Frame("GridLayout");
20 frame.setFont (new Font("Helvetica", Font.PLAIN, 14)); 20 frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
21 frame.setLayout (new GridLayout(2, 0)); 21 frame.setLayout(new GridLayout(2, 0));
22 22
23 // Create and add buttons 23 // Create and add buttons
24 frame.add (new JButton("One")); 24 frame.add(new JButton("One"));
25 frame.add (new JButton("Two")); 25 frame.add(new JButton("Two"));
26 frame.add (new JButton("Three")); 26 frame.add(new JButton("Three"));
27 frame.add (new JButton("Four")); 27 frame.add(new JButton("Four"));
28 28
29 // Show frame 29 // Show frame
30 frame.pack(); 30 frame.pack();
31 frame.setLocation (0, 10); 31 frame.setLocation(0, 10);
32 frame.show(); 32 frame.show();
33 33
34 return frame; 34 return frame;
35 } 35 }
36 36
37 37
38 38 protected static Frame showTableWindow() {
39 protected static Frame showTableWindow ()
40 {
41 // Create frame 39 // Create frame
42 Frame frame = new Frame("TableLayout"); 40 Frame frame = new Frame("TableLayout");
43 frame.setFont (new Font("Helvetica", Font.PLAIN, 14)); 41 frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
44 42
45 // Set layout 43 // Set layout
46 double f = TableLayout.FILL; 44 double f = TableLayout.FILL;
47 double size[][] = {{f, f}, {f, f}}; 45 double size[][] = {{f, f}, {f, f}};
48 frame.setLayout (new TableLayout(size)); 46 frame.setLayout(new TableLayout(size));
49 47
50 // Create and add buttons 48 // Create and add buttons
51 frame.add (new JButton("One"), "0, 0"); 49 frame.add(new JButton("One"), "0, 0");
52 frame.add (new JButton("Two"), "1, 0"); 50 frame.add(new JButton("Two"), "1, 0");
53 frame.add (new JButton("Three"), "0, 1"); 51 frame.add(new JButton("Three"), "0, 1");
54 frame.add (new JButton("Four"), "1, 1"); 52 frame.add(new JButton("Four"), "1, 1");
55 53
56 // Show frame 54 // Show frame
57 frame.pack(); 55 frame.pack();
58 frame.setLocation (200, 10); 56 frame.setLocation(200, 10);
59 frame.show(); 57 frame.show();
60 58
61 return frame; 59 return frame;
62 } 60 }
63 61
64 62
65 63 protected static Frame showTableWindow2() {
66 protected static Frame showTableWindow2 ()
67 {
68 // Create frame 64 // Create frame
69 Frame frame = new Frame("TableLayout"); 65 Frame frame = new Frame("TableLayout");
70 frame.setFont (new Font("Helvetica", Font.PLAIN, 14)); 66 frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
71 67
72 // Set layout 68 // Set layout
73 double f = TableLayout.FILL; 69 double f = TableLayout.FILL;
74 double size[][] = {{f, f}, {f, f}}; 70 double size[][] = {{f, f}, {f, f}};
75 frame.setLayout (new TableLayout(size)); 71 frame.setLayout(new TableLayout(size));
76 72
77 // Create and add buttons 73 // Create and add buttons
78 frame.add (new JButton("One"), "0, 0"); 74 frame.add(new JButton("One"), "0, 0");
79 frame.add (new JButton("Two"), "1, 1"); 75 frame.add(new JButton("Two"), "1, 1");
80 76
81 // Show frame 77 // Show frame
82 frame.pack(); 78 frame.pack();
83 frame.setLocation (400, 10); 79 frame.setLocation(400, 10);
84 frame.show(); 80 frame.show();
85 81
86 return frame; 82 return frame;
87 } 83 }
88 84
89 85
90 86 public static void main(String args[]) {
91 public static void main (String args[])
92 {
93 WindowListener listener = 87 WindowListener listener =
94 (new WindowAdapter() 88 (new WindowAdapter() {
95 { 89 public void windowClosing(WindowEvent e) {
96 public void windowClosing (WindowEvent e) 90 System.exit(0);
97 {
98 System.exit (0);
99 }
100 } 91 }
92 }
101 ); 93 );
102 94
103 Frame frame = showGridWindow(); 95 Frame frame = showGridWindow();
104 frame.addWindowListener(listener); 96 frame.addWindowListener(listener);
105 97
106 frame = showTableWindow(); 98 frame = showTableWindow();
107 frame.addWindowListener(listener); 99 frame.addWindowListener(listener);
108 100
diff --git a/lib/example/MyClass.java b/lib/example/MyClass.java
index 632e99a..ef47ace 100644
--- a/lib/example/MyClass.java
+++ b/lib/example/MyClass.java
@@ -1,47 +1,44 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Button;
5import java.awt.*; 5import java.awt.Frame;
6import java.awt.event.*; 6import java.awt.event.WindowAdapter;
7import java.awt.event.WindowEvent;
7import layout.TableLayout; 8import layout.TableLayout;
8 9
9public class MyClass 10public class MyClass {
10{
11 11
12 public static void main (String args[]) 12 public static void main(String args[]) {
13 {
14 Frame frame = new Frame("MyTitle"); 13 Frame frame = new Frame("MyTitle");
15 frame.setBounds (100, 100, 300, 300); 14 frame.setBounds(100, 100, 300, 300);
16 15
17 double size[][] = 16 double size[][] =
18 {{-2.0, 10.0, 50.0, -1.0, 10.0}, // Columns 17 {{-2.0, 10.0, 50.0, -1.0, 10.0}, // Columns
19 {-2.0, 10.0, 0.25, -1.0, 10.0}}; // Rows 18 {-2.0, 10.0, 0.25, -1.0, 10.0}}; // Rows
20 19
21 frame.setLayout (new TableLayout(size)); 20 frame.setLayout(new TableLayout(size));
22 21
23 Button button; 22 Button button;
24 button = new Button("3, 3, R, C"); 23 button = new Button("3, 3, R, C");
25 frame.add (button, "3, 3, R, C"); 24 frame.add(button, "3, 3, R, C");
26 button = new Button("3, 3, L, T"); 25 button = new Button("3, 3, L, T");
27 frame.add (button, "3, 3, L, T"); 26 frame.add(button, "3, 3, L, T");
28 button = new Button("2, 3, C, T"); 27 button = new Button("2, 3, C, T");
29 frame.add (button, "2, 3, C, T"); 28 frame.add(button, "2, 3, C, T");
30 button = new Button("3, 2, L, C"); 29 button = new Button("3, 2, L, C");
31 frame.add (button, "3, 2, L, C"); 30 frame.add(button, "3, 2, L, C");
32 button = new Button("2, 2, F, F"); 31 button = new Button("2, 2, F, F");
33 frame.add (button, "2, 2, F, F"); 32 frame.add(button, "2, 2, F, F");
34 button = new Button("3, 3, C, C"); 33 button = new Button("3, 3, C, C");
35 frame.add (button, "3, 3, C, C"); 34 frame.add(button, "3, 3, C, C");
36 35
37 frame.addWindowListener 36 frame.addWindowListener
38 (new WindowAdapter() 37 (new WindowAdapter() {
39 { 38 public void windowClosing(WindowEvent e) {
40 public void windowClosing (WindowEvent e) 39 System.exit(0);
41 { 40 }
42 System.exit (0); 41 }
43 }
44 }
45 ); 42 );
46 43
47 frame.show(); 44 frame.show();
diff --git a/lib/example/Preferred.java b/lib/example/Preferred.java
index aa1e8a6..f5fc54e 100644
--- a/lib/example/Preferred.java
+++ b/lib/example/Preferred.java
@@ -1,106 +1,97 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Container;
5import java.awt.*; 5import java.awt.event.WindowAdapter;
6import java.awt.event.*; 6import java.awt.event.WindowEvent;
7import javax.swing.*; 7import javax.swing.JButton;
8import javax.swing.JFrame;
9import javax.swing.JLabel;
10import javax.swing.JPanel;
11import javax.swing.JTextField;
8import layout.TableLayout; 12import layout.TableLayout;
9 13
10 14
15public class Preferred extends JFrame {
16
17
18 public Preferred() {
19 super("The Power of Preferred Sizes");
11 20
12public class Preferred extends JFrame
13{
14
15
16
17 public static void main (String args[])
18 {
19 new Preferred();
20 }
21
22
23
24 public Preferred ()
25 {
26 super ("The Power of Preferred Sizes");
27
28 Container pane = getContentPane(); 21 Container pane = getContentPane();
29 22
30 // b - border 23 // b - border
31 // f - FILL 24 // f - FILL
32 // p - PREFERRED 25 // p - PREFERRED
33 // vs - vertical space between labels and text fields 26 // vs - vertical space between labels and text fields
34 // vg - vertical gap between form elements 27 // vg - vertical gap between form elements
35 // hg - horizontal gap between form elements 28 // hg - horizontal gap between form elements
36 29
37 double b = 10; 30 double b = 10;
38 double f = TableLayout.FILL; 31 double f = TableLayout.FILL;
39 double p = TableLayout.PREFERRED; 32 double p = TableLayout.PREFERRED;
40 double vs = 5; 33 double vs = 5;
41 double vg = 10; 34 double vg = 10;
42 double hg = 10; 35 double hg = 10;
43 36
44 double size[][] = 37 double size[][] =
45 {{b, f, hg, p, hg, p, b}, 38 {{b, f, hg, p, hg, p, b},
46 {b, p, vs, p, vg, p, vs, p, vg, p, vs, p, vg, p, b}}; 39 {b, p, vs, p, vg, p, vs, p, vg, p, vs, p, vg, p, b}};
47 40
48 TableLayout layout = new TableLayout(size); 41 TableLayout layout = new TableLayout(size);
49 pane.setLayout (layout); 42 pane.setLayout(layout);
50 43
51 // Create all controls 44 // Create all controls
52 JLabel labelName = new JLabel("Name"); 45 JLabel labelName = new JLabel("Name");
53 JLabel labelAddress = new JLabel("Address"); 46 JLabel labelAddress = new JLabel("Address");
54 JLabel labelCity = new JLabel("City"); 47 JLabel labelCity = new JLabel("City");
55 JLabel labelState = new JLabel("State"); 48 JLabel labelState = new JLabel("State");
56 JLabel labelZip = new JLabel("Zip"); 49 JLabel labelZip = new JLabel("Zip");
57 50
58 JTextField textfieldName = new JTextField(10); 51 JTextField textfieldName = new JTextField(10);
59 JTextField textfieldAddress = new JTextField(20); 52 JTextField textfieldAddress = new JTextField(20);
60 JTextField textfieldCity = new JTextField(10); 53 JTextField textfieldCity = new JTextField(10);
61 JTextField textfieldState = new JTextField(2); 54 JTextField textfieldState = new JTextField(2);
62 JTextField textfieldZip = new JTextField(5); 55 JTextField textfieldZip = new JTextField(5);
63 56
64 JButton buttonOk = new JButton("OK"); 57 JButton buttonOk = new JButton("OK");
65 JButton buttonCancel = new JButton("Cancel"); 58 JButton buttonCancel = new JButton("Cancel");
66 JPanel panelButton = new JPanel(); 59 JPanel panelButton = new JPanel();
67 panelButton.add (buttonOk); 60 panelButton.add(buttonOk);
68 panelButton.add (buttonCancel); 61 panelButton.add(buttonCancel);
69 62
70 // Add all controls 63 // Add all controls
71 pane.add (labelName, "1, 1, 5, 1"); 64 pane.add(labelName, "1, 1, 5, 1");
72 pane.add (textfieldName, "1, 3, 5, 3"); 65 pane.add(textfieldName, "1, 3, 5, 3");
73 pane.add (labelAddress, "1, 5, 5, 5"); 66 pane.add(labelAddress, "1, 5, 5, 5");
74 pane.add (textfieldAddress, "1, 7, 5, 7"); 67 pane.add(textfieldAddress, "1, 7, 5, 7");
75 pane.add (labelCity, "1, 9"); 68 pane.add(labelCity, "1, 9");
76 pane.add (textfieldCity, "1, 11"); 69 pane.add(textfieldCity, "1, 11");
77 pane.add (labelState, "3, 9"); 70 pane.add(labelState, "3, 9");
78 pane.add (textfieldState, "3, 11"); 71 pane.add(textfieldState, "3, 11");
79 pane.add (labelZip, "5, 9"); 72 pane.add(labelZip, "5, 9");
80 pane.add (textfieldZip, "5, 11"); 73 pane.add(textfieldZip, "5, 11");
81 pane.add (panelButton, "1, 13, 5, 13"); 74 pane.add(panelButton, "1, 13, 5, 13");
82 75
83 allowClosing(); 76 allowClosing();
84 pack(); 77 pack();
85 setResizable (false); 78 setResizable(false);
86 show(); 79 show();
87 } 80 }
88 81
89 82 public static void main(String args[]) {
90 83 new Preferred();
91 public void allowClosing () 84 }
92 { 85
86 public void allowClosing() {
93 addWindowListener 87 addWindowListener
94 (new WindowAdapter() 88 (new WindowAdapter() {
95 { 89 public void windowClosing(WindowEvent e) {
96 public void windowClosing (WindowEvent e) 90 System.exit(0);
97 { 91 }
98 System.exit (0); 92 }
99 }
100 }
101 ); 93 );
102 } 94 }
103 95
104 96
105
106} 97}
diff --git a/lib/example/RadTool.java b/lib/example/RadTool.java
index 8ef0c55..75e7635 100644
--- a/lib/example/RadTool.java
+++ b/lib/example/RadTool.java
@@ -1,22 +1,33 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Choice;
5import java.awt.*; 5import java.awt.Color;
6import java.awt.event.*; 6import java.awt.Component;
7import java.util.*; 7import java.awt.Container;
8import javax.swing.*; 8import java.awt.Dimension;
9import java.awt.Frame;
10import java.awt.Graphics;
11import java.awt.Panel;
12import java.awt.TextArea;
13import java.awt.event.ActionEvent;
14import java.awt.event.ActionListener;
15import java.awt.event.MouseListener;
16import java.awt.event.WindowAdapter;
17import java.awt.event.WindowEvent;
18import java.util.ArrayList;
19import javax.swing.JButton;
20import javax.swing.JLabel;
21import javax.swing.JPanel;
22import javax.swing.JTextField;
9import layout.TableLayout; 23import layout.TableLayout;
10import layout.TableLayoutConstraints; 24import layout.TableLayoutConstraints;
11import support.GeneralDialog; 25import support.GeneralDialog;
12 26
13 27
28public class RadTool extends Frame implements ActionListener {
14 29
15public class RadTool extends Frame implements ActionListener
16{
17 30
18
19
20 private JTextField textfieldColumnNumber; 31 private JTextField textfieldColumnNumber;
21 private JTextField textfieldColumnSize; 32 private JTextField textfieldColumnSize;
22 private JTextField textfieldRowNumber; 33 private JTextField textfieldRowNumber;
@@ -37,20 +48,11 @@ public class RadTool extends Frame implements ActionListener
37 private ArrayList boxList; 48 private ArrayList boxList;
38 49
39 50
40
41 public static void main (String args[])
42 {
43 new RadTool();
44 }
45
46
47
48 /** 51 /**
49 * Constructs the user interface. 52 * Constructs the user interface.
50 */ 53 */
51 54
52 public RadTool() 55 public RadTool() {
53 {
54 // Create frame 56 // Create frame
55 super("Example of Dynamic Rows and Columns"); 57 super("Example of Dynamic Rows and Columns");
56 58
@@ -61,9 +63,9 @@ public class RadTool extends Frame implements ActionListener
61 double p = TableLayout.PREFERRED; 63 double p = TableLayout.PREFERRED;
62 double size[][] = 64 double size[][] =
63 {{b, p, s, p, s, p, s, p, s, p, s, p, s, p, f, b}, 65 {{b, p, s, p, s, p, s, p, s, p, s, p, s, p, f, b},
64 {f, s, p, s, p, s, 100, s, p, b}}; 66 {f, s, p, s, p, s, 100, s, p, b}};
65 67
66 setLayout (new TableLayout(size)); 68 setLayout(new TableLayout(size));
67 69
68 // Create static controls 70 // Create static controls
69 JLabel labelColumn = new JLabel("Column"); 71 JLabel labelColumn = new JLabel("Column");
@@ -81,106 +83,105 @@ public class RadTool extends Frame implements ActionListener
81 buttonAddRow = new JButton("Add"); 83 buttonAddRow = new JButton("Add");
82 buttonRemoveRow = new JButton("Remove"); 84 buttonRemoveRow = new JButton("Remove");
83 buttonResizeRow = new JButton("Resize"); 85 buttonResizeRow = new JButton("Resize");
84 86
85 textArea = new TextArea(); 87 textArea = new TextArea();
86 columnHeader = new ArrayList(); 88 columnHeader = new ArrayList();
87 rowHeader = new ArrayList(); 89 rowHeader = new ArrayList();
88 boxList = new ArrayList(); 90 boxList = new ArrayList();
89 buttonShowLayout = new JButton("Show Layout"); 91 buttonShowLayout = new JButton("Show Layout");
90 buttonGenerateCode = new JButton("Generate Code"); 92 buttonGenerateCode = new JButton("Generate Code");
91 93
92 // Add static controls 94 // Add static controls
93 add (labelColumn, " 1, 2"); 95 add(labelColumn, " 1, 2");
94 add (textfieldColumnNumber, " 3, 2"); 96 add(textfieldColumnNumber, " 3, 2");
95 add (labelColumnSize, " 5, 2"); 97 add(labelColumnSize, " 5, 2");
96 add (textfieldColumnSize, " 7, 2"); 98 add(textfieldColumnSize, " 7, 2");
97 add (buttonAddColumn, " 9, 2"); 99 add(buttonAddColumn, " 9, 2");
98 add (buttonRemoveColumn, "11, 2"); 100 add(buttonRemoveColumn, "11, 2");
99 add (buttonResizeColumn, "13, 2"); 101 add(buttonResizeColumn, "13, 2");
100 102
101 add (labelRow, " 1, 4"); 103 add(labelRow, " 1, 4");
102 add (textfieldRowNumber, " 3, 4"); 104 add(textfieldRowNumber, " 3, 4");
103 add (labelRowSize, " 5, 4"); 105 add(labelRowSize, " 5, 4");
104 add (textfieldRowSize, " 7, 4"); 106 add(textfieldRowSize, " 7, 4");
105 add (buttonAddRow, " 9, 4"); 107 add(buttonAddRow, " 9, 4");
106 add (buttonRemoveRow, "11, 4"); 108 add(buttonRemoveRow, "11, 4");
107 add (buttonResizeRow, "13, 4"); 109 add(buttonResizeRow, "13, 4");
108 110
109 add (textArea, "1, 6, 14, 6"); 111 add(textArea, "1, 6, 14, 6");
110 add (buttonShowLayout, "1, 8, 7, 6"); 112 add(buttonShowLayout, "1, 8, 7, 6");
111 add (buttonGenerateCode, "9, 8, 12, 8"); 113 add(buttonGenerateCode, "9, 8, 12, 8");
112 114
113 // Listen for button events 115 // Listen for button events
114 buttonAddColumn.addActionListener (this); 116 buttonAddColumn.addActionListener(this);
115 buttonRemoveColumn.addActionListener (this); 117 buttonRemoveColumn.addActionListener(this);
116 buttonResizeColumn.addActionListener (this); 118 buttonResizeColumn.addActionListener(this);
117 buttonAddRow.addActionListener (this); 119 buttonAddRow.addActionListener(this);
118 buttonRemoveRow.addActionListener (this); 120 buttonRemoveRow.addActionListener(this);
119 buttonResizeRow.addActionListener (this); 121 buttonResizeRow.addActionListener(this);
120 buttonShowLayout.addActionListener (this); 122 buttonShowLayout.addActionListener(this);
121 buttonGenerateCode.addActionListener (this); 123 buttonGenerateCode.addActionListener(this);
122 124
123 // Add a panel for RadTool controls 125 // Add a panel for RadTool controls
124 panel = new JPanel(); 126 panel = new JPanel();
125 panel.setBackground (Color.white); 127 panel.setBackground(Color.white);
126 add (panel, "0, 0, 15, 0"); 128 add(panel, "0, 0, 15, 0");
127 129
128 // Create the layout manager for the panel 130 // Create the layout manager for the panel
129 double size2[][] = {{p, -1}, {p, -1}}; 131 double size2[][] = {{p, -1}, {p, -1}};
130 layout = new TableLayout(size2); 132 layout = new TableLayout(size2);
131 panel.setLayout (layout); 133 panel.setLayout(layout);
132 updateHeader(); 134 updateHeader();
133 updateBox(); 135 updateBox();
134 136
135 // Allow user to close the window to terminate the program 137 // Allow user to close the window to terminate the program
136 addWindowListener 138 addWindowListener
137 (new WindowAdapter() 139 (new WindowAdapter() {
138 { 140 public void windowClosing(WindowEvent e) {
139 public void windowClosing (WindowEvent e) 141 System.exit(0);
140 { 142 }
141 System.exit (0); 143 }
142 }
143 }
144 ); 144 );
145 145
146 // Show frame 146 // Show frame
147 setBackground (Color.lightGray); 147 setBackground(Color.lightGray);
148 setBounds (100, 100, 500, 400); 148 setBounds(100, 100, 500, 400);
149 show(); 149 show();
150 } 150 }
151 151
152 152 public static void main(String args[]) {
153 153 new RadTool();
154 }
155
154 /** 156 /**
155 * Handles all action events. 157 * Handles all action events.
156 */ 158 */
157 159
158 public void actionPerformed (ActionEvent e) 160 public void actionPerformed(ActionEvent e) {
159 {
160 // Get row and column information from text fields 161 // Get row and column information from text fields
161 int row = getInt(textfieldRowNumber); 162 int row = getInt(textfieldRowNumber);
162 int col = getInt(textfieldColumnNumber); 163 int col = getInt(textfieldColumnNumber);
163 double rowSize = getDouble(textfieldRowSize); 164 double rowSize = getDouble(textfieldRowSize);
164 double colSize = getDouble(textfieldColumnSize); 165 double colSize = getDouble(textfieldColumnSize);
165 166
166 // Get source of the event 167 // Get source of the event
167 Object source = e.getSource(); 168 Object source = e.getSource();
168 169
169 try 170 try {
170 {
171 // Update layout 171 // Update layout
172 if (source == buttonAddColumn) 172 if (source == buttonAddColumn) {
173 layout.insertColumn (col, colSize); 173 layout.insertColumn(col, colSize);
174 else if (source == buttonRemoveColumn) 174 } else if (source == buttonRemoveColumn) {
175 layout.deleteColumn (col); 175 layout.deleteColumn(col);
176 else if (source == buttonResizeColumn) 176 } else if (source == buttonResizeColumn) {
177 layout.setColumn (col, colSize); 177 layout.setColumn(col, colSize);
178 else if (source == buttonAddRow) 178 } else if (source == buttonAddRow) {
179 layout.insertRow (row, rowSize); 179 layout.insertRow(row, rowSize);
180 else if (source == buttonRemoveRow) 180 } else if (source == buttonRemoveRow) {
181 layout.deleteRow (row); 181 layout.deleteRow(row);
182 else if (source == buttonResizeRow) 182 } else if (source == buttonResizeRow) {
183 layout.setRow (row, rowSize); 183 layout.setRow(row, rowSize);
184 }
184 185
185 // Update headers, etc. to reflect layout's change 186 // Update headers, etc. to reflect layout's change
186 updateHeader(); 187 updateHeader();
@@ -189,258 +190,242 @@ public class RadTool extends Frame implements ActionListener
189 // Layout and repaint panel since the layout has changed 190 // Layout and repaint panel since the layout has changed
190 panel.doLayout(); 191 panel.doLayout();
191 panel.repaint(); 192 panel.repaint();
192 193
193 // Update layout's description 194 // Update layout's description
194 textArea.setText(layout.toString()); 195 textArea.setText(layout.toString());
195 196
196 // Generate code if desired 197 // Generate code if desired
197 if (source == buttonGenerateCode) 198 if (source == buttonGenerateCode) {
198 generateCode(); 199 generateCode();
199 } 200 }
200 catch (Throwable error) 201 } catch (Throwable error) {
201 {
202 error.printStackTrace(); 202 error.printStackTrace();
203 textArea.setText (error.toString()); 203 textArea.setText(error.toString());
204 } 204 }
205 } 205 }
206 206
207 207
208
209 /** 208 /**
210 * Converts the text in a text field to an integer. 209 * Converts the text in a text field to an integer.
211 */ 210 */
212 211
213 private int getInt (JTextField field) 212 private int getInt(JTextField field) {
214 {
215 int value = 0; 213 int value = 0;
216 214
217 try 215 try {
218 {
219 value = Integer.parseInt(field.getText()); 216 value = Integer.parseInt(field.getText());
217 } catch (NumberFormatException e) {
220 } 218 }
221 catch (NumberFormatException e) {} 219
222
223 return value; 220 return value;
224 } 221 }
225 222
226 223
227
228 /** 224 /**
229 * Converts the text in a text field to a double. 225 * Converts the text in a text field to a double.
230 */ 226 */
231 227
232 private double getDouble (JTextField field) 228 private double getDouble(JTextField field) {
233 {
234 double value = -1.0; 229 double value = -1.0;
235 230
236 try 231 try {
237 {
238 value = Double.parseDouble(field.getText()); 232 value = Double.parseDouble(field.getText());
233 } catch (NumberFormatException e) {
239 } 234 }
240 catch (NumberFormatException e) {} 235
241
242 return value; 236 return value;
243 } 237 }
244 238
245
246 239
247 /** 240 /**
248 * Updates all the row and columns headers by bruce force. The headers 241 * Updates all the row and columns headers by bruce force. The headers
249 * are removed and then recreated based on the current layout configuration. 242 * are removed and then recreated based on the current layout configuration.
250 */ 243 */
251 244
252 private void updateHeader () 245 private void updateHeader() {
253 {
254 TableLayoutConstraints c = new TableLayoutConstraints 246 TableLayoutConstraints c = new TableLayoutConstraints
255 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL); 247 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL);
256 248
257 double size[] = layout.getColumn(); 249 double size[] = layout.getColumn();
258 250
259 for (int i = columnHeader.size() - 1; i >= 0; i--) 251 for (int i = columnHeader.size() - 1; i >= 0; i--) {
260 {
261 JButton header = (JButton) columnHeader.remove(i); 252 JButton header = (JButton) columnHeader.remove(i);
262 panel.remove(header); 253 panel.remove(header);
263 } 254 }
264 255
265 for (int i = 0; i < size.length; i++) 256 for (int i = 0; i < size.length; i++) {
266 {
267 c.col1 = c.col2 = i; 257 c.col1 = c.col2 = i;
268 JButton header = new JButton("" + i + ": " + size[i]); 258 JButton header = new JButton("" + i + ": " + size[i]);
269 columnHeader.add(i, header); 259 columnHeader.add(i, header);
270 panel.add (header, c); 260 panel.add(header, c);
271 } 261 }
272 262
273 c.col1 = c.col2 = 0; 263 c.col1 = c.col2 = 0;
274 size = layout.getRow(); 264 size = layout.getRow();
275 265
276 for (int i = rowHeader.size() - 1; i >= 0; i--) 266 for (int i = rowHeader.size() - 1; i >= 0; i--) {
277 {
278 JButton header = (JButton) rowHeader.remove(i); 267 JButton header = (JButton) rowHeader.remove(i);
279 panel.remove(header); 268 panel.remove(header);
280 } 269 }
281 270
282 for (int i = 0; i < size.length; i++) 271 for (int i = 0; i < size.length; i++) {
283 {
284 c.row1 = c.row2 = i; 272 c.row1 = c.row2 = i;
285 JButton header = new JButton("" + i + ": " + size[i]); 273 JButton header = new JButton("" + i + ": " + size[i]);
286 rowHeader.add(i, header); 274 rowHeader.add(i, header);
287 panel.add (header, c); 275 panel.add(header, c);
288 } 276 }
289 } 277 }
290 278
291 279
292
293 /** 280 /**
294 * Updates the boxes used to illustrate cells. This is a brute force, 281 * Updates the boxes used to illustrate cells. This is a brute force,
295 * unoptimized method. 282 * unoptimized method.
296 */ 283 */
297 284
298 private void updateBox () 285 private void updateBox() {
299 { 286 for (int i = boxList.size() - 1; i >= 0; i--) {
300 for (int i = boxList.size() - 1; i >= 0; i--)
301 {
302 Box box = (Box) boxList.remove(i); 287 Box box = (Box) boxList.remove(i);
303 panel.remove(box); 288 panel.remove(box);
304 } 289 }
305 290
306 TableLayoutConstraints constraint = new TableLayoutConstraints 291 TableLayoutConstraints constraint = new TableLayoutConstraints
307 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL); 292 (0, 0, 0, 0, TableLayout.FULL, TableLayout.FULL);
308 293
309 double col[] = layout.getColumn(); 294 double col[] = layout.getColumn();
310 double row[] = layout.getRow(); 295 double row[] = layout.getRow();
311 296
312 for (int c = 1; c < col.length; c++) 297 for (int c = 1; c < col.length; c++) {
313 for (int r = 1; r < row.length; r++) 298 for (int r = 1; r < row.length; r++) {
314 {
315 constraint.col1 = constraint.col2 = c; 299 constraint.col1 = constraint.col2 = c;
316 constraint.row1 = constraint.row2 = r; 300 constraint.row1 = constraint.row2 = r;
317 Box box = new Box(); 301 Box box = new Box();
318 panel.add (box, constraint); 302 panel.add(box, constraint);
319 } 303 }
304 }
320 } 305 }
321 306
322 307
323
324 /** 308 /**
325 * Generates code based on the current layout and Smiley controls. 309 * Generates code based on the current layout and Smiley controls.
326 */ 310 */
327 311
328 private void generateCode () 312 private void generateCode() {
329 {
330 String code = 313 String code =
331 "import java.awt.*;\n" + 314 "import java.awt.*;\n" +
332 "import java.awt.event.*;\n" + 315 "import java.awt.event.*;\n" +
333 "import layout.TableLayout;\n\n" + 316 "import layout.TableLayout;\n\n" +
334 317
335 "public class MyClass\n" + 318 "public class MyClass\n" +
336 "{\n\n" + 319 "{\n\n" +
337 320
338 " public static void main (String args[])\n" + 321 " public static void main (String args[])\n" +
339 " {\n" + 322 " {\n" +
340 " Frame frame = new Frame(\"MyTitle\");\n" + 323 " Frame frame = new Frame(\"MyTitle\");\n" +
341 " frame.setBounds (100, 100, 300, 300);\n\n" + 324 " frame.setBounds (100, 100, 300, 300);\n\n" +
325
326 " double size[][] =\n" +
327 " {{";
342 328
343 " double size[][] =\n" +
344 " {{";
345
346 double size[] = layout.getColumn(); 329 double size[] = layout.getColumn();
347 330
348 if (size.length > 0) 331 if (size.length > 0) {
349 code += size[0]; 332 code += size[0];
333 }
350 334
351 for (int i = 1; i < size.length; i++) 335 for (int i = 1; i < size.length; i++) {
352 code += ", " + size[i]; 336 code += ", " + size[i];
337 }
353 338
354 code += "}, // Columns\n" + 339 code += "}, // Columns\n" +
355 " {"; 340 " {";
356 341
357 size = layout.getRow(); 342 size = layout.getRow();
358 343
359 if (size.length > 0) 344 if (size.length > 0) {
360 code += size[0]; 345 code += size[0];
346 }
361 347
362 for (int i = 1; i < size.length; i++) 348 for (int i = 1; i < size.length; i++) {
363 code += ", " + size[i]; 349 code += ", " + size[i];
350 }
364 351
365 code += "}}; // Rows\n\n" + 352 code += "}}; // Rows\n\n" +
366 " frame.setLayout (new TableLayout(size));\n\n" + 353 " frame.setLayout (new TableLayout(size));\n\n" +
367 " Button button;\n"; 354 " Button button;\n";
368 355
369 Component component[] = panel.getComponents(); 356 Component component[] = panel.getComponents();
370 357
371 for (int i = 0; i < component.length; i++) 358 for (int i = 0; i < component.length; i++) {
372 { 359 if (component[i] instanceof Smiley) {
373 if (component[i] instanceof Smiley)
374 {
375 TableLayoutConstraints c = layout.getConstraints(component[i]); 360 TableLayoutConstraints c = layout.getConstraints(component[i]);
376 String constraint = "" + c.col1 + ", " + c.row1 + ", "; 361 String constraint = "" + c.col1 + ", " + c.row1 + ", ";
377 362
378 if ((c.col1 == c.col2) && (c.row1 == c.row2)) 363 if ((c.col1 == c.col2) && (c.row1 == c.row2)) {
379 {
380 String h[] = {"L", "C", "F", "R"}; 364 String h[] = {"L", "C", "F", "R"};
381 String v[] = {"T", "C", "F", "B"}; 365 String v[] = {"T", "C", "F", "B"};
382 constraint += h[c.hAlign] + ", " + v[c.vAlign]; 366 constraint += h[c.hAlign] + ", " + v[c.vAlign];
383 } 367 } else {
384 else
385 constraint += c.col2 + ", " + c.row2; 368 constraint += c.col2 + ", " + c.row2;
386 369 }
370
387 code += 371 code +=
388 " button = new Button(\"" + constraint + "\");\n" + 372 " button = new Button(\"" + constraint + "\");\n" +
389 " frame.add (button, \"" + constraint + "\");\n"; 373 " frame.add (button, \"" + constraint + "\");\n";
390 } 374 }
391 } 375 }
392 376
393 code += 377 code +=
394 "\n" + 378 "\n" +
395 " frame.addWindowListener\n" + 379 " frame.addWindowListener\n" +
396 " (new WindowAdapter()\n" + 380 " (new WindowAdapter()\n" +
397 " {\n" + 381 " {\n" +
398 " public void windowClosing (WindowEvent e)\n" + 382 " public void windowClosing (WindowEvent e)\n" +
399 " {\n" + 383 " {\n" +
400 " System.exit (0);\n" + 384 " System.exit (0);\n" +
401 " }\n" + 385 " }\n" +
402 " }\n" + 386 " }\n" +
403 " );\n\n" + 387 " );\n\n" +
404 " frame.show();\n" + 388 " frame.show();\n" +
405 " }\n" + 389 " }\n" +
406 "}\n"; 390 "}\n";
407 391
408 textArea.setText (code); 392 textArea.setText(code);
409 } 393 }
410 394
411 395
412
413 /** 396 /**
414 * This inner class is a component that looks like a box. 397 * This inner class is a component that looks like a box.
415 */ 398 */
416 399
417 public class Box extends Component implements MouseListener 400 public class Box extends Component implements MouseListener {
418 { 401 public Box() {
419 public Box ()
420 {
421 super(); 402 super();
422 addMouseListener(this); 403 addMouseListener(this);
423 } 404 }
424 405
425 public void update (Graphics g) 406 public void update(Graphics g) {
426 { 407 paint(g);
427 paint (g);
428 } 408 }
429 409
430 public void paint (Graphics g) 410 public void paint(Graphics g) {
431 {
432 Dimension d = getSize(); 411 Dimension d = getSize();
433 g.setColor (Color.black); 412 g.setColor(Color.black);
434 g.drawRect (0, 0, d.width - 1, d.height - 1); 413 g.drawRect(0, 0, d.width - 1, d.height - 1);
414 }
415
416 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
417 }
418
419 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
420 }
421
422 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
435 } 423 }
436 424
437 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {} 425 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
438 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {} 426 }
439 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {} 427
440 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {} 428 public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
441
442 public void mouseReleased(java.awt.event.MouseEvent mouseEvent)
443 {
444 TableLayoutConstraints c = layout.getConstraints(this); 429 TableLayoutConstraints c = layout.getConstraints(this);
445 Smiley smiley = new Smiley(); 430 Smiley smiley = new Smiley();
446 Container container = getParent(); 431 Container container = getParent();
@@ -449,32 +434,26 @@ public class RadTool extends Frame implements ActionListener
449 } 434 }
450 } 435 }
451 436
452
453 437
454 /** 438 /**
455 * This inner class is a component that looks like a smiley face. 439 * This inner class is a component that looks like a smiley face.
456 */ 440 */
457 441
458 public class Smiley extends Component implements MouseListener 442 public class Smiley extends Component implements MouseListener {
459 { 443 public Smiley() {
460 public Smiley ()
461 {
462 super(); 444 super();
463 addMouseListener (this); 445 addMouseListener(this);
464 } 446 }
465 447
466 public Dimension getPreferredSize () 448 public Dimension getPreferredSize() {
467 {
468 return new Dimension(64, 64); 449 return new Dimension(64, 64);
469 } 450 }
470 451
471 public void update (Graphics g) 452 public void update(Graphics g) {
472 { 453 paint(g);
473 paint (g);
474 } 454 }
475 455
476 public void paint (Graphics g) 456 public void paint(Graphics g) {
477 {
478 Dimension d = getSize(); 457 Dimension d = getSize();
479 int width_1_8 = d.width >> 3; 458 int width_1_8 = d.width >> 3;
480 int width_7_8 = d.width - width_1_8; 459 int width_7_8 = d.width - width_1_8;
@@ -487,131 +466,132 @@ public class RadTool extends Frame implements ActionListener
487 int height_1_4 = d.height >> 2; 466 int height_1_4 = d.height >> 2;
488 int height_3_4 = d.height - height_1_4; 467 int height_3_4 = d.height - height_1_4;
489 int height_5_8 = height_7_8 - height_1_4; 468 int height_5_8 = height_7_8 - height_1_4;
490 469
491 g.setColor (Color.yellow); 470 g.setColor(Color.yellow);
492 g.fillArc (0, 0, d.width - 1, d.height - 1, 0, 360); 471 g.fillArc(0, 0, d.width - 1, d.height - 1, 0, 360);
493 g.setColor (Color.blue); 472 g.setColor(Color.blue);
494 g.fillArc (width_1_4, height_1_4, width_1_8, height_1_8, 0, 360); 473 g.fillArc(width_1_4, height_1_4, width_1_8, height_1_8, 0, 360);
495 g.fillArc (width_5_8, height_1_4, width_1_8, height_1_8, 0, 360); 474 g.fillArc(width_5_8, height_1_4, width_1_8, height_1_8, 0, 360);
496 g.setColor (Color.red); 475 g.setColor(Color.red);
497 g.fillArc (width_1_4, height_5_8, width_1_2, height_1_4, 180, 180); 476 g.fillArc(width_1_4, height_5_8, width_1_2, height_1_4, 180, 180);
477 }
478
479 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {
480 }
481
482 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {
483 }
484
485 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {
486 }
487
488 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {
498 } 489 }
499 490
500 public void mouseExited(java.awt.event.MouseEvent mouseEvent) {} 491 public void mouseReleased(java.awt.event.MouseEvent mouseEvent) {
501 public void mousePressed(java.awt.event.MouseEvent mouseEvent) {}
502 public void mouseClicked(java.awt.event.MouseEvent mouseEvent) {}
503 public void mouseEntered(java.awt.event.MouseEvent mouseEvent) {}
504
505 public void mouseReleased(java.awt.event.MouseEvent mouseEvent)
506 {
507 // Get constraints applied to this Smiley 492 // Get constraints applied to this Smiley
508 TableLayoutConstraints c = layout.getConstraints(this); 493 TableLayoutConstraints c = layout.getConstraints(this);
509 494
510 // Create controls 495 // Create controls
511 Panel panel = new Panel(); 496 Panel panel = new Panel();
512 497
513 int numRow = layout.getNumRow(); 498 int numRow = layout.getNumRow();
514 int numColumn = layout.getNumColumn(); 499 int numColumn = layout.getNumColumn();
515 500
516 Choice choiceCol1 = new Choice(); 501 Choice choiceCol1 = new Choice();
517 Choice choiceCol2 = new Choice(); 502 Choice choiceCol2 = new Choice();
518 Choice choiceRow1 = new Choice(); 503 Choice choiceRow1 = new Choice();
519 Choice choiceRow2 = new Choice(); 504 Choice choiceRow2 = new Choice();
520 505
521 for (int i = 1; i < numColumn; i++) 506 for (int i = 1; i < numColumn; i++) {
522 { 507 choiceCol1.add("" + i);
523 choiceCol1.add ("" + i); 508 choiceCol2.add("" + i);
524 choiceCol2.add ("" + i);
525 } 509 }
526 510
527 for (int i = 1; i < numRow; i++) 511 for (int i = 1; i < numRow; i++) {
528 { 512 choiceRow1.add("" + i);
529 choiceRow1.add ("" + i); 513 choiceRow2.add("" + i);
530 choiceRow2.add ("" + i);
531 } 514 }
532 515
533 choiceCol1.select (c.col1 - 1); 516 choiceCol1.select(c.col1 - 1);
534 choiceCol2.select (c.col2 - 1); 517 choiceCol2.select(c.col2 - 1);
535 choiceRow1.select (c.row1 - 1); 518 choiceRow1.select(c.row1 - 1);
536 choiceRow2.select (c.row2 - 1); 519 choiceRow2.select(c.row2 - 1);
537 520
538 Choice choiceAlignH = new Choice(); 521 Choice choiceAlignH = new Choice();
539 choiceAlignH.add ("Left"); 522 choiceAlignH.add("Left");
540 choiceAlignH.add ("Center"); 523 choiceAlignH.add("Center");
541 choiceAlignH.add ("Full"); 524 choiceAlignH.add("Full");
542 choiceAlignH.add ("Right"); 525 choiceAlignH.add("Right");
543 choiceAlignH.select (c.hAlign); 526 choiceAlignH.select(c.hAlign);
544 527
545 Choice choiceAlignV = new Choice(); 528 Choice choiceAlignV = new Choice();
546 choiceAlignV.add ("Top"); 529 choiceAlignV.add("Top");
547 choiceAlignV.add ("Center"); 530 choiceAlignV.add("Center");
548 choiceAlignV.add ("Full"); 531 choiceAlignV.add("Full");
549 choiceAlignV.add ("Bottom"); 532 choiceAlignV.add("Bottom");
550 choiceAlignV.select (c.vAlign); 533 choiceAlignV.select(c.vAlign);
551 534
552 JLabel labelCol1 = new JLabel("Column 1"); 535 JLabel labelCol1 = new JLabel("Column 1");
553 JLabel labelCol2 = new JLabel("Column 2"); 536 JLabel labelCol2 = new JLabel("Column 2");
554 JLabel labelRow1 = new JLabel("Row 1"); 537 JLabel labelRow1 = new JLabel("Row 1");
555 JLabel labelRow2 = new JLabel("Row 2"); 538 JLabel labelRow2 = new JLabel("Row 2");
556 JLabel labelAlignH = new JLabel("Horizontal Justification"); 539 JLabel labelAlignH = new JLabel("Horizontal Justification");
557 JLabel labelAlignV = new JLabel("Vertical Justification"); 540 JLabel labelAlignV = new JLabel("Vertical Justification");
558 labelAlignH.setHorizontalAlignment (JLabel.RIGHT); 541 labelAlignH.setHorizontalAlignment(JLabel.RIGHT);
559 labelAlignV.setHorizontalAlignment (JLabel.RIGHT); 542 labelAlignV.setHorizontalAlignment(JLabel.RIGHT);
560 543
561 // Create layout 544 // Create layout
562 double f = TableLayout.FILL; 545 double f = TableLayout.FILL;
563 double p = TableLayout.PREFERRED; 546 double p = TableLayout.PREFERRED;
564 double s = 10; 547 double s = 10;
565 548
566 double size[][] = 549 double size[][] =
567 {{f, p, s, p, s, p, s, p, f}, 550 {{f, p, s, p, s, p, s, p, f},
568 {p, s, p, s, p, s, p, s, p, s, p}}; 551 {p, s, p, s, p, s, p, s, p, s, p}};
569 552
570 TableLayout panelLayout = new TableLayout(size); 553 TableLayout panelLayout = new TableLayout(size);
571 panel.setLayout (panelLayout); 554 panel.setLayout(panelLayout);
572 555
573 // Add controls 556 // Add controls
574 panel.add (labelCol1, "1, 0, R, B"); 557 panel.add(labelCol1, "1, 0, R, B");
575 panel.add (choiceCol1, "3, 0, L, B"); 558 panel.add(choiceCol1, "3, 0, L, B");
576 panel.add (labelRow1, "5, 0, R, B"); 559 panel.add(labelRow1, "5, 0, R, B");
577 panel.add (choiceRow1, "7, 0, L, B"); 560 panel.add(choiceRow1, "7, 0, L, B");
578 panel.add (labelCol2, "1, 2, R, B"); 561 panel.add(labelCol2, "1, 2, R, B");
579 panel.add (choiceCol2, "3, 2, L, B"); 562 panel.add(choiceCol2, "3, 2, L, B");
580 panel.add (labelRow2, "5, 2, R, B"); 563 panel.add(labelRow2, "5, 2, R, B");
581 panel.add (choiceRow2, "7, 2, L, B"); 564 panel.add(choiceRow2, "7, 2, L, B");
582 panel.add (labelAlignH, "0, 4, 3, 4"); 565 panel.add(labelAlignH, "0, 4, 3, 4");
583 panel.add (choiceAlignH, "5, 4, 7, 4"); 566 panel.add(choiceAlignH, "5, 4, 7, 4");
584 panel.add (labelAlignV, "0, 6, 3, 6"); 567 panel.add(labelAlignV, "0, 6, 3, 6");
585 panel.add (choiceAlignV, "5, 6, 7, 6"); 568 panel.add(choiceAlignV, "5, 6, 7, 6");
586 569
587 // Prompt user 570 // Prompt user
588 String button[] = {"Update Smiley", "Remove Smiley", "Cancel"}; 571 String button[] = {"Update Smiley", "Remove Smiley", "Cancel"};
589 572
590 GeneralDialog dialog = new GeneralDialog 573 GeneralDialog dialog = new GeneralDialog
591 (RadTool.this, "Update Smiley", "", button, null, panel); 574 (RadTool.this, "Update Smiley", "", button, null, panel);
592 575
593 String answer = dialog.promptUser(); 576 String answer = dialog.promptUser();
594 577
595 // Update constraints applied to this Smiley 578 // Update constraints applied to this Smiley
596 if (answer.equals("Update Smiley")) 579 if (answer.equals("Update Smiley")) {
597 {
598 // Get columns 580 // Get columns
599 int col1 = choiceCol1.getSelectedIndex() + 1; 581 int col1 = choiceCol1.getSelectedIndex() + 1;
600 int col2 = choiceCol2.getSelectedIndex() + 1; 582 int col2 = choiceCol2.getSelectedIndex() + 1;
601 int row1 = choiceRow1.getSelectedIndex() + 1; 583 int row1 = choiceRow1.getSelectedIndex() + 1;
602 int row2 = choiceRow2.getSelectedIndex() + 1; 584 int row2 = choiceRow2.getSelectedIndex() + 1;
603 585
604 // Make sure col1 < col2 586 // Make sure col1 < col2
605 if (col1 > col2) 587 if (col1 > col2) {
606 {
607 int temp = col1; 588 int temp = col1;
608 col1 = col2; 589 col1 = col2;
609 col2 = temp; 590 col2 = temp;
610 } 591 }
611 592
612 // Make sure row1 < row2 593 // Make sure row1 < row2
613 if (row1 > row2) 594 if (row1 > row2) {
614 {
615 int temp = row1; 595 int temp = row1;
616 row1 = row2; 596 row1 = row2;
617 row2 = temp; 597 row2 = temp;
@@ -624,7 +604,7 @@ public class RadTool extends Frame implements ActionListener
624 c.row2 = row2; 604 c.row2 = row2;
625 c.hAlign = choiceAlignH.getSelectedIndex(); 605 c.hAlign = choiceAlignH.getSelectedIndex();
626 c.vAlign = choiceAlignV.getSelectedIndex(); 606 c.vAlign = choiceAlignV.getSelectedIndex();
627 layout.setConstraints (this, c); 607 layout.setConstraints(this, c);
628 608
629 // Repaint and layout container since layout has changed 609 // Repaint and layout container since layout has changed
630 Container container = getParent(); 610 Container container = getParent();
@@ -632,16 +612,14 @@ public class RadTool extends Frame implements ActionListener
632 container.repaint(); 612 container.repaint();
633 } 613 }
634 // Remove Smiley 614 // Remove Smiley
635 else if (answer.equals("Remove Smiley")) 615 else if (answer.equals("Remove Smiley")) {
636 {
637 Container container = getParent(); 616 Container container = getParent();
638 container.remove (this); 617 container.remove(this);
639 container.doLayout(); 618 container.doLayout();
640 container.repaint(); 619 container.repaint();
641 } 620 }
642 } 621 }
643 } 622 }
644 623
645 624
646
647} 625}
diff --git a/lib/example/Simple.java b/lib/example/Simple.java
index 3ff9fb9..b49f8df 100644
--- a/lib/example/Simple.java
+++ b/lib/example/Simple.java
@@ -1,62 +1,58 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Button;
5import java.awt.*; 5import java.awt.Frame;
6import java.awt.event.*; 6import java.awt.event.WindowAdapter;
7import java.awt.event.WindowEvent;
7import layout.TableLayout; 8import layout.TableLayout;
8 9
9 10
10 11public class Simple {
11public class Simple
12{
13 12
14 13
15 14 public static void main(String args[]) {
16 public static void main (String args[])
17 {
18 // Create a frame 15 // Create a frame
19 Frame frame = new Frame("Example of TableLayout"); 16 Frame frame = new Frame("Example of TableLayout");
20 frame.setBounds (100, 100, 300, 300); 17 frame.setBounds(100, 100, 300, 300);
21 18
22 // Create a TableLayout for the frame 19 // Create a TableLayout for the frame
23 double border = 10; 20 double border = 10;
24 double size[][] = 21 double size[][] =
25 {{border, 0.10, 20, TableLayout.FILL, 20, 0.20, border}, // Columns 22 {{border, 0.10, 20, TableLayout.FILL, 20, 0.20, border}, // Columns
26 {border, 0.20, 20, TableLayout.FILL, 20, 0.20, border}}; // Rows 23 {border, 0.20, 20, TableLayout.FILL, 20, 0.20, border}}; // Rows
27 24
28 frame.setLayout (new TableLayout(size)); 25 frame.setLayout(new TableLayout(size));
29 26
30 // Create some buttons 27 // Create some buttons
31 String label[] = {"Top", "Bottom", "Left", "Right", "Center", "Overlap"}; 28 String label[] =
29 {"Top", "Bottom", "Left", "Right", "Center", "Overlap"};
32 Button button[] = new Button[label.length]; 30 Button button[] = new Button[label.length];
33 31
34 for (int i = 0; i < label.length; i++) 32 for (int i = 0; i < label.length; i++) {
35 button[i] = new Button(label[i]); 33 button[i] = new Button(label[i]);
34 }
36 35
37 // Add buttons 36 // Add buttons
38 frame.add (button[0], "1, 1, 5, 1"); // Top 37 frame.add(button[0], "1, 1, 5, 1"); // Top
39 frame.add (button[1], "1, 5, 5, 5"); // Bottom 38 frame.add(button[1], "1, 5, 5, 5"); // Bottom
40 frame.add (button[2], "1, 3 "); // Left 39 frame.add(button[2], "1, 3 "); // Left
41 frame.add (button[3], "5, 3 "); // Right 40 frame.add(button[3], "5, 3 "); // Right
42 frame.add (button[4], "3, 3, c, c"); // Center 41 frame.add(button[4], "3, 3, c, c"); // Center
43 frame.add (button[5], "3, 3, 3, 5"); // Overlap 42 frame.add(button[5], "3, 3, 3, 5"); // Overlap
44 43
45 // Allow user to close the window to terminate the program 44 // Allow user to close the window to terminate the program
46 frame.addWindowListener 45 frame.addWindowListener
47 (new WindowAdapter() 46 (new WindowAdapter() {
48 { 47 public void windowClosing(WindowEvent e) {
49 public void windowClosing (WindowEvent e) 48 System.exit(0);
50 { 49 }
51 System.exit (0); 50 }
52 }
53 }
54 ); 51 );
55 52
56 // Show frame 53 // Show frame
57 frame.show(); 54 frame.show();
58 } 55 }
59 56
60 57
61
62} \ No newline at end of file 58} \ No newline at end of file
diff --git a/lib/example/TypicalGui.java b/lib/example/TypicalGui.java
index ab80ff1..e44a371 100644
--- a/lib/example/TypicalGui.java
+++ b/lib/example/TypicalGui.java
@@ -1,179 +1,158 @@
1package example; 1package example;
2 2
3 3
4 4import java.awt.Color;
5import java.awt.*; 5import java.awt.Component;
6import java.awt.event.*; 6import java.awt.Container;
7import javax.swing.*; 7import java.awt.Dimension;
8import java.awt.Graphics;
9import java.awt.event.WindowAdapter;
10import java.awt.event.WindowEvent;
11import javax.swing.JButton;
12import javax.swing.JFrame;
13import javax.swing.JMenu;
14import javax.swing.JMenuBar;
15import javax.swing.JMenuItem;
16import javax.swing.JPanel;
17import javax.swing.JTextPane;
8import layout.TableLayout; 18import layout.TableLayout;
9 19
10 20
21public class TypicalGui extends JFrame {
22
23
24 public TypicalGui() {
25 super("A Typical GUI");
11 26
12public class TypicalGui extends JFrame
13{
14
15
16
17 public static void main (String args[])
18 {
19 new TypicalGui();
20 }
21
22
23
24 public TypicalGui ()
25 {
26 super ("A Typical GUI");
27
28 Container pane = getContentPane(); 27 Container pane = getContentPane();
29 28
30 double b = 10; 29 double b = 10;
31 double f = TableLayout.FILL; 30 double f = TableLayout.FILL;
32 double p = TableLayout.PREFERRED; 31 double p = TableLayout.PREFERRED;
33 double size[][] = {{b, f, 5, p, 5, p, b}, {p, b, f, 10, p, b}}; 32 double size[][] = {{b, f, 5, p, 5, p, b}, {p, b, f, 10, p, b}};
34 TableLayout layout = new TableLayout(size); 33 TableLayout layout = new TableLayout(size);
35 pane.setLayout (layout); 34 pane.setLayout(layout);
36 35
37 addMenu(pane); 36 addMenu(pane);
38 addCommandButtons(pane, layout); 37 addCommandButtons(pane, layout);
39 addColorBoxes(pane, layout); 38 addColorBoxes(pane, layout);
40 addTextArea(pane, layout); 39 addTextArea(pane, layout);
41 40
42 allowClosing(); 41 allowClosing();
43 setSize (640, 480); 42 setSize(640, 480);
44 show(); 43 show();
45 } 44 }
46 45
47 46 public static void main(String args[]) {
48 47 new TypicalGui();
49 public void addMenu (Container pane) 48 }
50 { 49
50 public void addMenu(Container pane) {
51 JMenuBar menuBar = new JMenuBar(); 51 JMenuBar menuBar = new JMenuBar();
52 52
53 String menuText[] = {"File", "Edit", "View", "Help"}; 53 String menuText[] = {"File", "Edit", "View", "Help"};
54 String itemText[][] = 54 String itemText[][] =
55 {{"New", "Open", "Save", "Print", "Exit"}, 55 {{"New", "Open", "Save", "Print", "Exit"},
56 {"Cut", "Copy", "Paste"}, 56 {"Cut", "Copy", "Paste"},
57 {"Zoom in", "Zoom out"}, 57 {"Zoom in", "Zoom out"},
58 {"About", "Index", "Search"}}; 58 {"About", "Index", "Search"}};
59 59
60 for (int i = 0; i < menuText.length; i++) 60 for (int i = 0; i < menuText.length; i++) {
61 {
62 JMenu menu = new JMenu(menuText[i]); 61 JMenu menu = new JMenu(menuText[i]);
63 menuBar.add (menu); 62 menuBar.add(menu);
64 63
65 for (int j = 0; j < itemText[i].length; j++) 64 for (int j = 0; j < itemText[i].length; j++) {
66 {
67 JMenuItem item = new JMenuItem(itemText[i][j]); 65 JMenuItem item = new JMenuItem(itemText[i][j]);
68 menu.add (item); 66 menu.add(item);
69 } 67 }
70 } 68 }
71 69
72 pane.add (menuBar, "0, 0, 6, 0"); 70 pane.add(menuBar, "0, 0, 6, 0");
73 } 71 }
74 72
75
76 73
77 public void addCommandButtons (Container pane, TableLayout layout) 74 public void addCommandButtons(Container pane, TableLayout layout) {
78 {
79 JPanel buttonPanel = new JPanel(); 75 JPanel buttonPanel = new JPanel();
80 pane.add (buttonPanel, "1, 4, 5, 4"); 76 pane.add(buttonPanel, "1, 4, 5, 4");
81 77
82 for (int i = 1; i <= 5; i++) 78 for (int i = 1; i <= 5; i++) {
83 {
84 JButton button = new JButton("Button " + i); 79 JButton button = new JButton("Button " + i);
85 buttonPanel.add (button); 80 buttonPanel.add(button);
86 } 81 }
87 } 82 }
88 83
89 84
90 85 public void addColorBoxes(Container pane, TableLayout layout) {
91 public void addColorBoxes (Container pane, TableLayout layout)
92 {
93 Color color[][] = 86 Color color[][] =
94 {{Color.white, Color.black}, 87 {{Color.white, Color.black},
95 {Color.green, Color.blue}, 88 {Color.green, Color.blue},
96 {Color.red, Color.yellow}, 89 {Color.red, Color.yellow},
97 {Color.pink, Color.orange}, 90 {Color.pink, Color.orange},
98 {Color.magenta, Color.cyan}}; 91 {Color.magenta, Color.cyan}};
99 92
100 for (int i = 0; i < color.length; i++) 93 for (int i = 0; i < color.length; i++) {
101 {
102 // Add a row for spacing and a row for the color boxes 94 // Add a row for spacing and a row for the color boxes
103 layout.insertRow (2, TableLayout.PREFERRED); 95 layout.insertRow(2, TableLayout.PREFERRED);
104 layout.insertRow (2, 5); 96 layout.insertRow(2, 5);
105 97
106 // Add color boxes 98 // Add color boxes
107 pane.add (new ColorBox(color[i][0]), "3, 3"); 99 pane.add(new ColorBox(color[i][0]), "3, 3");
108 pane.add (new ColorBox(color[i][1]), "5, 3"); 100 pane.add(new ColorBox(color[i][1]), "5, 3");
109 } 101 }
110 102
111 // Remove the unnecessary leading space 103 // Remove the unnecessary leading space
112 layout.deleteRow (2); 104 layout.deleteRow(2);
113 } 105 }
114 106
115 107
116 108 public void addTextArea(Container pane, TableLayout layout) {
117 public void addTextArea (Container pane, TableLayout layout)
118 {
119 int numRow = layout.getRow().length; 109 int numRow = layout.getRow().length;
120 JTextPane textArea = new JTextPane(); 110 JTextPane textArea = new JTextPane();
121 pane.add (textArea, "1, 2, 1, " + (numRow - 4)); 111 pane.add(textArea, "1, 2, 1, " + (numRow - 4));
122 } 112 }
123 113
124 114
125 115 public void allowClosing() {
126 public void allowClosing ()
127 {
128 addWindowListener 116 addWindowListener
129 (new WindowAdapter() 117 (new WindowAdapter() {
130 { 118 public void windowClosing(WindowEvent e) {
131 public void windowClosing (WindowEvent e) 119 System.exit(0);
132 { 120 }
133 System.exit (0); 121 }
134 }
135 }
136 ); 122 );
137 } 123 }
138 124
139 125
140
141 //************************************************************************** 126 //**************************************************************************
142 //*** Inner classes *** 127 //*** Inner classes ***
143 //************************************************************************** 128 //**************************************************************************
144 129
145 130
146 131 protected class ColorBox extends Component {
147 protected class ColorBox extends Component
148 {
149 protected Color color; 132 protected Color color;
150 133
151 protected ColorBox (Color color) 134 protected ColorBox(Color color) {
152 {
153 this.color = color; 135 this.color = color;
154 } 136 }
155 137
156 public void update (Graphics g) 138 public void update(Graphics g) {
157 { 139 paint(g);
158 paint (g);
159 } 140 }
160 141
161 public void paint (Graphics g) 142 public void paint(Graphics g) {
162 {
163 Dimension d = getSize(); 143 Dimension d = getSize();
164 g.setColor (Color.black); 144 g.setColor(Color.black);
165 g.drawRect (0, 0, d.width - 1, d.height - 1); 145 g.drawRect(0, 0, d.width - 1, d.height - 1);
166 146
167 g.setColor (color); 147 g.setColor(color);
168 g.fillRect (1, 1, d.width - 1, d.height - 1); 148 g.fillRect(1, 1, d.width - 1, d.height - 1);
169 } 149 }
170 150
171 public Dimension getPreferredSize () 151 public Dimension getPreferredSize() {
172 { 152 return new Dimension(40, 20);
173 return new Dimension(40, 20);; 153 ;
174 } 154 }
175 } 155 }
176 156
177 157
178
179} 158}