summaryrefslogtreecommitdiffstats
path: root/lib/example/GridBagVersusTable.java
blob: 0ed367a90f76873b7dddb5231676af77c2b9be99 (plain) (blame)
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
package example;


import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import layout.TableLayout;


public class GridBagVersusTable {


    protected static void makeButton
        (Frame frame, String name, GridBagLayout gridbag,
         GridBagConstraints c) {
        JButton button = new JButton(name);
        gridbag.setConstraints(button, c);
        frame.add(button);
    }


    protected static Frame showGridBagWindow() {
        // Create layout and contraints object
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        // Create frame
        Frame frame = new Frame("GridBagLayout");
        frame.setFont(new Font("Helvetica", Font.PLAIN, 14));
        frame.setLayout(gridbag);

        // Create buttons, add buttons, and apply constraints
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        makeButton(frame, "Button1", gridbag, c);
        makeButton(frame, "Button2", gridbag, c);
        makeButton(frame, "Button3", gridbag, c);

        c.gridwidth = GridBagConstraints.REMAINDER;  //end of row
        makeButton(frame, "Button4", gridbag, c);

        c.weightx = 0.0;                             //reset to the default
        makeButton(frame, "Button5", gridbag, c);   //another row

        c.gridwidth = GridBagConstraints.RELATIVE;   //next-to-last in row
        makeButton(frame, "Button6", gridbag, c);

        c.gridwidth = GridBagConstraints.REMAINDER;  //end of row
        makeButton(frame, "Button7", gridbag, c);

        c.gridwidth = 1;                            //reset to the default
        c.gridheight = 2;
        c.weighty = 1.0;
        makeButton(frame, "Button8", gridbag, c);

        c.weighty = 0.0;                            //reset to the default
        c.gridwidth = GridBagConstraints.REMAINDER; //end of row
        c.gridheight = 1;                           //reset to the default
        makeButton(frame, "Button9", gridbag, c);
        makeButton(frame, "Button10", gridbag, c);

        // Show frame
        frame.pack();
        frame.setLocation(0, 10);
        frame.show();

        return frame;
    }


    protected static Frame showTableWindow() {
        // Create frame
        Frame frame = new Frame("TableLayout");
        frame.setFont(new Font("Helvetica", Font.PLAIN, 14));

        // Set layout
        double f = TableLayout.FILL;
        double p = TableLayout.PREFERRED;
        double size[][] = {{f, f, f, f}, {p, p, p, p, f}};

        TableLayout layout = new TableLayout(size);
        frame.setLayout(layout);

        // Create buttons labeled Button1 to Button10
        int numButton = 10;
        JButton button[] = new JButton[numButton + 1];

        for (int i = 1; i <= numButton; i++) {
            button[i] = new JButton("Button" + i);
        }

        // Add buttons
        frame.add(button[1], "0, 0");
        frame.add(button[2], "1, 0");
        frame.add(button[3], "2, 0");
        frame.add(button[4], "3, 0");
        frame.add(button[5], "0, 1, 3, 1");
        frame.add(button[6], "0, 2, 2, 2");
        frame.add(button[7], "3, 2, 3, 2");
        frame.add(button[8], "0, 3, 0, 4");
        frame.add(button[9], "1, 3, 3, 3");
        frame.add(button[10], "1, 4, 3, 4");

        // Show frame
        frame.pack();
        frame.setLocation(400, 10);
        frame.show();

        return frame;
    }


    public static void main(String args[]) {
        WindowListener listener =
            (new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            }
            );

        Frame frame = showGridBagWindow();
        frame.addWindowListener(listener);

        frame = showTableWindow();
        frame.addWindowListener(listener);
    }
}