REMAINDER to signal that any component assigned the GridBagContstraint Con takes up the rest of the space on a row. Similarly, we can set gridheight to REMAINDER so that a component assigned this constraint takes up the remaining vertical space. The last detail associated with GridBagLayout involves assigning the properties to the component. This is done via the setConstraints method in GridBagLayout. Listing 4.4 shows how we do this. Notice that we assign properties for the TextArea, but not for the Labels. Because we re positioning the Labels on the right side of the screen (the default), there is no need to assign constraints. There are more properties you can set with GridBagLayout, but it s beyond the scope of this book. Listing 4.4 Assigning properties to components. add(new Label(”Name”)); gridbag.setConstraints(NameField, Con); add(NameField); // Note that we did not setConstraints for the Label. The GridbagLayout // manager assumes they carry the default constraints. The NameField is // assigned to be the last component on its row via the constraints Con, // then added to the user interface. add(new Label(”Database URL”)); gridbag.setConstraints(DBurl, Con); add(DBurl); gridbag.setConstraints(ConnectBtn, Con); add(ConnectBtn); // Here, we only want the ConnectBtn button on a row, by itself, so we // set the constraints, and add it. add(new Label(”SQL Query”)); gridbag.setConstraints(QueryField, Con); add(QueryField); Label result_label = new Label(”Result”); result_label.setFont(new Font(”Helvetica”, Font.PLAIN, 16)); result_label.setForeground(Color.blue); gridbag.setConstraints(result_label, Con); add(result_label); // Here we add a label on its own line. We also set the colors for it. Con.weighty=1.0; gridbag.setConstraints(OutputField, Con); OutputField.setForeground(Color.white); OutputField.setBackground(Color.black); add(OutputField); // This is what we were talking about before. We want the large OutputField to // take up as much of the remaining space as possible, so we set the // weighty=1 at this point. This sets the field apart from the previously // added components, and gives it more room to exist in. show();
Do you want truly affordable web hosting? With us, what you see is what you get, just click on affordable web hosting services.