The GUI Builder updates the alignment status lines indicating the component's spacing and anchoring relationships. As in the previous examples, double-click the JLabel to select its display text and then enter Display Format: for the display name. Notice that when the JLabel snaps into position, the other components shift to accommodate the longer display text.
JFC Swing Tutorial, The: A Guide to Constructing GUIs
Whenever you add or move components that include text JLabels, JTextFields, and so forth , the IDE suggests alignments which are based on the baselines of the text in the components. When we inserted the JTextField earlier, for example, its baseline was automatically aligned to the adjacent JLabels. Now we'll add the combo box that will enable users to select the format of the information that our ContactEditor application will display. Notice once again the baseline alignment guidelines that appear to assist us with the positioning.
The component snaps into a position aligned with the baseline of the text in the JLabel to its left, as shown in the following illustration. The GUI Builder displays status lines indicating the component's spacing and anchoring relationships. As shown in the following illustration, the JComboBox's right edge snaps into alignment with the JPanel's recommended edge margin and the component's width is automatically set to resize with the form.
Editing component models is beyond the scope of this tutorial, so for the time being we'll leave the JComboBox's placeholder item list as it is. We've got off to a good start building our ContactEditor GUI, but let's take a minute to recap what we've learned while we add a few more of the components our interface requires.
It is important to understand, however, that another integral part of component placement is anchoring. Though we haven't discussed it yet, you've already taken advantage of this feature without realizing it. As mentioned previously, whenever you add a component to a form, the IDE suggests the target look and feel's preferred positioning with guidelines. Once placed, new components are also anchored to the nearest container edge or component to ensure that component relationships are maintained at runtime.
In this section, we'll concentrate on accomplishing the tasks in a more streamlined fashion while pointing out the work the GUI builder is doing behind the scenes. The GUI Builder enables you to lay out your forms quickly and easily by streamlining typical workflow gestures. Whenever you add a component to a form, the GUI Builder automatically snaps them into the preferred positions and sets the necessary chaining relationships so you can concentrate on designing your forms rather than struggling with complicated implementation details.
The JLabel snaps into the preferred position in the form, anchored to the top and left edges of the enclosing JPanel. Just as before, a corresponding node representing the component is added to the Navigator window. Its corresponding node is also added to the Inspector window.
The JTextField's right edge snaps to the alignment guideline indicating the preferred margins. Now we need to add the JList that will display our ContactEditor's entire contact list. The JList snaps into the position designated by the alignment guidelines and its corresponding node is displayed in the Inspector window. Notice also that the form expands to accommodate the newly added JList. Since JLists are used to display long lists of data, they typically require the addition of a JScrollPane.
Note: Refer to the Resizing and indenting components. It is often beneficial to set several related components, such as buttons in modal dialogues, to be the same size for visual consistency. To demonstrate this we'll add four JButtons to our ContactEditor form that will allow us to add, edit, and remove individual entries from our contact list, as shown in the following illustrations.
Afterwards, we'll set the four buttons to be the same size so they can be easily recognized as offering related functionality. The JButton components snap into the positions designated by the alignment guidelines. The width of the buttons changes to accommodate the new names. Now that the buttons are positioned where we want them, we'll set the four buttons to be the same size for visual consistency as well as to clarify that they are related functionally.
- The JFC Swing tutorial : a guide to constructing GUIs | UTS Library.
- Probability, Statistics, and Random Processes for Electrical Engineers - Instructors Solution Manual.
- Account Options.
- The JFC Swing Tutorial: A Guide to Constructing GUIs, Second Edition.
- Walrath & Campione, JFC Swing Tutorial, The: A Guide to Constructing GUIs | Pearson!
The JButtons are set to the same size as the button with the longest name. Often it is necessary to cluster multiple components under another component such that it is clear they belong to a group of related functions. One typical case, for example, is placing several related checkboxes below a common label. The GUI Builder enables you to accomplish indenting easily by providing special guidelines suggesting the preferred offset for your operating system's look and feel.
In this section we'll add a few JRadioButtons below a JLabel that will allow users to customize the way the application displays data. Refer to the following illustrations while accomplishing this or click the View Demo link following the procedure to view an interactive demonstration.
Now we need to add the three JRadioButtons to a ButtonGroup to enable the expected toggle behavior in which only one radio button can be selected at a time. This will, in turn, ensure that our ContactEditor application's contact information will be displayed in the mail format of our choosing.
We've managed to rough out our ContactEditor application's GUI, but there are still a few things remaining to do. In this section, we'll take a look at a couple of other typical layout tasks that the GUI Builder streamlines.
My Shopping Bag
Now we need to add the buttons that will enable users to confirm the information they enter for an individual contact and add it to the contact list or cancel, leaving the database unchanged. In this step, we'll add the two required buttons and then edit them so that they appear the same size in our form even though their display text are different lengths. The JButton components appear in the form and their corresponding nodes are displayed in the Navigator window. The JButton components' code is also added to the form's source file which is visible in the Editor's Source view.
Each of the JButtons are set to the same size as the button with the longest name. The last thing we need to do is delete the placeholder text in the various components. Note that while removing placeholder text after roughing out a form can be a helpful technique in avoiding problems with component alignments and anchoring relationships, most developers typically remove this text in the process of laying out their forms.
As you go through the form, select and delete the placeholder text for each of the JTextFields. Now that you have successfully built the ContactEditor GUI, you can try your interface to see the results. The form opens in its own window, allowing you to test it prior to building and running. In order for the interfaces you create with the GUI Builder to work outside of the IDE, the application must be compiled against classes for the GroupLayout layout manager and also have those classes available at runtime. If you develop the application to run on Java SE 5, your application needs to use the Swing Layout Extensions library.
When you deploy the application, you need to include the Swing Layout Extensions library with the application.
The JFC Swing Tutorial: A Guide to Constructing GUIs / Edition 2
This means that you can deploy the application to run on systems with Java SE 6 installed and you do not need to package your application with the Swing Layout Extensions library. To run your application, right-click the project name and select Run in the context menu. In the Run Project dialog select the main class name my. Your application is up and running. Ensure that the manifest.
The Java™ Series - JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition [Book]
For information on adding functionality to the GUIs that you create, see:. This tutorial takes about 30 minutes to complete. Creating a Project Because all Java development in the IDE takes place within projects, we first need to create a new ContactEditor project within which to store sources and other project files.
Click Next. Enter ContactEditor in the Project Name field and specify the project location. Click Finish. Enter my. The toolbar's Source button enables you to view a class's source code, the Design button allows you to view a graphical view of the GUI components, the History button allows you to access the local history of changes of the file. The additional toolbar buttons provide convenient access to common commands, such as choosing between Selection and Connection modes, aligning components, setting component auto-resizing behavior, and previewing forms. Provides a representation of all the components, both visual and non-visual, in your application as a tree hierarchy.
The Navigator also provides visual feedback about what component in the tree is currently being edited in the GUI Builder as well as allows you to organize components in the available panels.
https://lihapuvuzeci.tk In addition, you can create, remove, and rearrange the categories displayed in the Palette using the customizer. Properties Window. Automatic Component Positioning Snapping As you add components to a form, the GUI Builder provides visual feedback that assists in positioning components based on your operating system's look and feel. Visual Feedback The GUI Builder also provides visual feedback regarding component anchoring and chaining relationships.
To add a JPanel: In the Palette window, select the Panel component from the Swing Containers category by clicking and releasing the mouse button. Move the cursor to the upper left corner of the form in the GUI Builder. When the component is located near the container's top and left edges, horizontal and vertical alignment guidelines appear indicating the preferred margins.
- Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing).
- The Time Machine and The War of the Worlds (Bantam Classic reissue)?
- Designing a Swing GUI in NetBeans IDE - Tutorial.
- Lesson: Using Swing Components!
- The JFC Swing Tutorial: A Guide to Constructing GUIs;