Here is the screenshot of the sample AWT window, which we will create in this example: In our example, we have a Multi-Select List box control in the upper left portion of the AWT Frame. When the user clicks the Get Fruits button, the AWT TextArea control will show all the selected list items in it. The Clear Output button will delete the text displayed in the AWT TextArea Control. This example will help you learn how to add strings on AWT List Control and how to retrieve all the items selected by the user. Now, we will proceed with the example.
The item marked as 1 is the AWT TextArea control. We will add this control to the center of AWT Frame and you can see it occupies 80% of the space. Towards the bottom we add three more AWT Controls. First one is the TextField (Marked as 2) and after that we add an AWT Button (Marked as 3). Finally, we add a Checkbox Control (Marked as 4) and we use this check box to insert text in the TextArea control. We can type a line of text in the TextField and click on the Add Text button. This will add a single line of text in the AWT TextArea Control. Each time we click Add Text button, a new line of text is added to the TextArea Component. When the check box not checked, we are in text selection mode. User can move their mouse and select any text and delete it or add text manually by changing the cursor position through the mouse. When the Check box is in checked state, we are in Text-Insert mode. We can type a word or line of text in the text field and then click the mouse cursor at a different location on the TextArea Control to insert it. This way we can insert same text at different location quickly.
Top portion of the Frame window has the AWT Choice Control. There are two Text fields in the Frame. The first one takes a string and adds it to the Choice Control via Add button click. The next text field is used to display the selected text of the Choice Control. Moreover, the Remove button click will use this text field to delete an item in the Choice.
Here, the GridBagLayout comprises of 16 cells. In x axis, we have 4 slots and in y axis we have 4 slots. These together form a grid of 16 cells. The gridx and gridy properties of GridBagConstraints represents these slots. In the above picture you can also see placement of the 9 buttons and how they vary in size. The properties gridheight & gridwidth tell how many cells a control must cover. The gridwidth property spawns a control horizontally and gridheight property spawns it vertically. In this example, we will create 9 buttons and add it to the AWT Frame window, which is managed by GridBagLayout. Along the way, we will learn about the gridx, gridy, gridwidth and gridheight properties of the GridBagConstraints.
We had seen many Layouts in past examples on Java AWT. The GridBagLayout is the most flexible layout in the Java AWT packages. But this comes with the cost of complexity held in the setup of GridBagLayout. You can imagine GridBagLayout as GridLayout with change in cell sizes. Here, we can add controls in relation with the existing one. The GridBagLayout makes use of the specific settings for each controls which can be set using the GridBagConstraints class. Before adding the controls to the GridBagLayout class, we need to set these component specific setup rules. The bag layout will make use of these setup factors to arrange the components in the parent container.