This trail is available as a book, The JFC Swing Tutorial. Besides having the usual advantages of a book (such as portability and readability) this page tome. This second edition of Java Swing thoroughly covers all the features available in Java 2 SDK and . Programming with Accessibility . other JFC topics, like Java 2D—check out Java 2D by Jonathan Knudsen for that; all the drawing and font .. of you still working with JDK , we've included a PDF containing the . 6 days ago Learn Java Gui Applications A Jfc Swing Tutorial - [Free] Learn Java Gui Swing Tutorial [PDF] [EPUB] Tour Start here for a quick overview of.
|Language:||English, Dutch, German|
|Genre:||Children & Youth|
|ePub File Size:||26.67 MB|
|PDF File Size:||8.13 MB|
|Distribution:||Free* [*Sign up for free]|
Thu, 26 Jul GMT learn java gui applications pdf - WOW! eBook: Free eBooks. Download is a Legal. eBooks Free Download Site. Download, Free The Java Swing Tutorial Download Pdf, Free Pdf The Java Swing java swing tutorial is a part of java foundation classes (jfc) that is used to . learn java gui applications a jfc swing tutorial 8th edition pdf book ebook pdf free and manual reference download learn java gui applications for high school.
A menu contains menu-item JMenuItem. Menu-A contains 2 menu-items Up and Down. Menu-B has 1 menu-item Reset. JOptionPane provides standard pre-built diglog boxes to interact with user for both input and output. To create a dialog box, use one of the static methods JOptionPane.
Each of these methods also comes has a showInternalXxxDialog version, which uses an internal frame to hold the dialog box. Example: Input, Confirm and Message Dialogs 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import javax.
Furthermore, you can use JOptionPane directly under main to prompt user for input, similar to text-based input via Scanner. Example: Prompting User for Input with Validation 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 import java.
The "look" refers to the appearance of the widgets JComponent ; while the "feel" refers to how the widgets behave e. You can choose to use the default Java look and feel, the native system's look and feel Windows, Linux, Mac , or the newer cross-platform Nimbus look and feel. Pluggable look and feel is supported in Swing's components by separating the components into two classes: JComponent in package javax.
Nonetheless, you shall not interact with the UI delegate directly. These look and feel are supported in packages javax. There are a few ways to set the Look and Feel.
Via UIManager. You can either use the static method UIManager. MetalLookAndFeel] javax. NimbusLookAndFeel] javax. MotifLookAndFeel] javax.
WindowsLookAndFeel] javax. Nimbus is a polished cross-platform look and feel introduced in the JDK 1. Nimbus is highly customizable. You can use the Nimbus look and feel as is, or you can skin customize the look with your own brand.
Use command-line option "swing. You can choose the size via: myButton. You can customize the look and feel, which is beyond the scope of this article. All content-panes default to BorderLayout with hgap and vgap of 0. Second add will override the first add. Absolute Positioning without LayoutManager shall be avoided, as it does not adjust well on screens with different resolutions; or when the window is resize. Hints on sizes and alignments You can provide hints on the minimum, preferred and maximum sizes of a component via setMinimumSize , setPreferredSize and setMaximumSize methods.
However, some layout managers ignore these requests, especially the maximum size. You can also do these by extending a subclass and overriding the getXxxSize call-back methods.
The setSize method must be issued at the correct point, or else it will not work as it was overridden by another implicit setSize. BoxLayout honors them but other layout managers may ignore these hints. You can also extend a subclass and override the getAlignmentX and getAlignmentY call-back methods. Methods validate and doLayout If you change a property of a LayoutManager, such as hgap or vgap of GridLayout, you need to issue a doLayout to force the LayoutManager to re-layout the components using the new property.
A container has only one LayoutManager. You need to follow with a validate to ask the container to re-layout the components.
Code Example This example creates 6 buttons, which are arranged in 3x2 and 2x3 GridLayout alternately upon clicking any button. You can also use aContainer. You need to issue a validate call to the container after adding or removing components.
Code Example This example starts with 2 buttons: one to "add" a button and one to "remove" a button. The buttons are arranged in FlowLayout on the content-pane. For demonstration purpose, I remove all the buttons and re-add all the buttons. Since JDK 1. Some new terms were introduced.
You can then position you components using the method setBounds int xTopLeft, int yTopLeft, int width, int height. 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.
The GUI Builder provides helpful inline hints and other visual feedback regarding where components should be placed on your form, automatically snapping components into position along guidelines.
It makes these suggestions based on the positions of the components that have already been placed in the form, while allowing the padding to remain flexible such that different target look and feels render properly at runtime. Visual Feedback The GUI Builder also provides visual feedback regarding component anchoring and chaining relationships.
These indicators enable you to quickly identify the various positioning relationships and component pinning behavior that affect the way your GUI will both appear and behave at runtime. This speeds the GUI design process, enabling you to quickly create professional-looking visual interfaces that work. Thanks to the IDE's Free Design paradigm, you no longer have to struggle with layout managers to control the size and position of the components within your containers.
All you need to do is drag and drop the components you need to your GUI form as shown in the illustrations that follow. Note: Refer to the Adding individual and multiple components. Many interface designers consider this a "best practice" technique, however, for the purposes of this tutorial you can simply peek at how our completed form should look by jumping ahead to the Previewing your GUI section.
Since we've already added a JFrame as our form's top-level container, the next step is to add a couple of JPanels which will enable us to cluster the components of our UI using titled borders. Refer to the following illustrations and notice the IDE's "drag and drop" behavior when accomplishing this. 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. Click in the form to place the JPanel in this location. After releasing the mouse button, small indicators appear to show the component's anchoring relationships and a corresponding JPanel node is displayed in the Navigator window, as shown in the following illustration.
Next, we need to resize the JPanel to make room for the components we'll place within it a little later, but let's take a minute to point out another of the GUI Builder's visualization features first. In order to do this we need to deselect the JPanel we just added. Because we haven't added a title border yet, the panel disappears.
Notice, however, that when you pass the cursor over the JPanel, its edges change to light gray so that its position can be clearly seen. You need only to click anywhere within the component to reselect it and cause the resize handles and anchoring indicators to reappear. The small square resize handles reappear around the component's perimeter. Click and hold the resize handle on the right edge of the JPanel and drag until the dotted alignment guideline appears near the form's edge.
Release the resize handle to resize the component. The JPanel component is extended to span between the container's left and right margins in accordance with the recommended offset, as shown in the following illustration. Now that we've added a panel to contain our UI's Name information, we need to repeat the process to add another directly below the first for the E-mail information.
Referring to the following illustrations, repeat the previous two tasks, paying attention to the GUI Builder's suggested positioning. Notice that the suggested vertical spacing between the two JPanels is much narrower than that at the edges. Once you have added the second JPanel, resize it such that it fills the form's remaining vertical space. Because we want to visually distinguish the functions in the upper and lower sections of our GUI, we need to add a border and title to each JPanel.
First we'll accomplish this using the Properties window and then we'll try it using the pop-up menu. In the Properties window, click the ellipsis button In the Properties pane below, enter Name for the Title property. Click the ellipsis Click OK to exit the dialogs. Select the bottom JPanel and repeat steps 2 through 5, but this time right-click the JPanel and access the Properties window using the pop-up menu.
Enter E-mail for the Title property.
Titled borders are added to both JPanel components. Adding Individual Components to the Form Now we need to start adding the components that will present the actual contact information in our contact list. In this task we'll add four JTextFields that will display the contact information and the JLabels that will describe them.
While accomplishing this, notice the horizontal and vertical guidelines that the GUI Builder displays, suggesting the preferred component spacing as defined by your operating system's look and feel. This ensures that your GUI is automatically rendered respecting the target operating system's look and feel at runtime.
Trail: Creating a GUI With JFC/Swing
Move the cursor over the Name JPanel we added earlier. When the guidelines appear indicating that the JLabel is positioned in the top left corner of the JPanel with a small margin at the top and left edges, click to place the label. The JLabel is added to the form and a corresponding node representing the component is added to the Inspector window.
Before going further, we need to edit the display text of the JLabel we just added.
Other UI-Related Trails
Though you can edit component display text at any point, the easiest way is to do this as you add them. Type First Name: and press Enter. The JLabel's new name is displayed and the component's width adjusts as a result of the edit. Move the cursor immediately to the right of the First Name: JLabel we just added. When the horizontal guideline appears indicating that the JTextField's baseline is aligned with that of the JLabel and the spacing between the two components is suggested with a vertical guideline, click to position the JTextField.
The JTextField snaps into position in the form aligned with the JLabel's baseline, as shown in the following illustration. Notice that the JLabel shifted downward slightly in order to align with the taller text field's baseline. As usual, a node representing the component is added to the Navigator window. Before proceeding further, we need to add an additional JLabel and JTextField immediately to the right of those we just added, as shown in the following illustration.
When the vertical alignment guidelines appear suggesting the margin between the text field and right edge of the JPanel, release the mouse button to resize the JTextField. The JTextField's right edge snaps into alignment with the JPanel's recommended edge margin, as shown in the following illustration. We'll drag and drop the components while pressing the Shift key, to quickly add them to the form.
While accomplishing this, again notice that the GUI Builder displays horizontal and vertical guidelines suggesting the preferred component spacing. To add multiple JLabels to the form: In the Palette window, select the Label component from the Swing Controls category by clicking and releasing the mouse button.
Move the cursor over the form directly below the First Name: JLabel we added earlier. When the guidelines appear indicating that the new JLabel's left edge is aligned with that of the JLabel above and a small margin exists between them, shift-click to place the first JLabel.
Trail: Creating a GUI With JFC/Swing
While still pressing the Shift key, place another JLabel immediately to the right of the first. Make certain to release the Shift key prior to positioning the second JLabel. If you forget to release the Shift key prior to positioning the last JLabel, simply press the Escape key. The JLabels are added to the form creating a second row, as shown in the following illustration. Nodes representing each component are added to the Navigator window.
Before moving on, we need to edit the JLabels' name so that we'll be able to see the effect of the alignments we'll set later. Type Title: and press Enter. Repeat steps 1 and 2, entering Nickname: for the second JLabel's name property.
The JLabels' new names are displayed in the form and are shifted as a result of their edited widths, as shown in the following illustration. Inserting Components Note: Refer to the Inserting components. Often it is necessary to add a component between components that are already placed in a form. Whenever you add a component between two existing components, the GUI Builder automatically shifts them to make room for the new component.
To demonstrate this, we'll insert a JTextField between the JLabels we added previously, as shown in the following two illustrations. Move the cursor over the Title: and Nickname: JLabels on the second row such that the JTextField overlaps both and is aligned to their baselines.
If you encounter difficulty positioning the new text field, you can snap it to the left guideline of the Nickname JLabel as shown in the first image below. The rightmost JLabel shifts toward the right of the JTextField to accommodate the suggested horizontal offset. We still need to add one additional JTextField to the form that will display each contact's nickname on the right side of the form. Move the cursor to the right of the Nickname label and click to place the text field.
When the vertical alignment guidelines appear suggesting the margin between the text field and JPanel edges, release the mouse button to resize the JTextField. Press Ctrl-S to save the file. Next, we'll take a more in depth look at the GUI Builder's alignment features as we work with the various other components we need for our application. Component Alignment Note: Refer to the Aligning and anchoring components. Every time you add a component to a form, the GUI Builder effectively aligns them, as evidenced by the alignment guidelines that appear.
It is sometimes necessary, however, to specify different relationships between groups of components as well. Now we'll align the two columns of JLabels so that their right edges line up. Click the Align Right in Column button in the toolbar. The JLabels' positions shift such that the right edges of their display text are aligned. The anchoring relationships are updated, indicating that the components have been grouped.
Before we're finished with the JTextFields we added earlier, we need to make sure that the two JTextFields we inserted between the JLabels are set to resize correctly. Unlike the two JTextFields that we stretched to the right edge of our form, inserted components' resizeability behavior isn't automatically set. The JTextFields are set to resize horizontally at runtime. The alignment guidelines and anchoring indicators are updated, providing visual feedback of the component relationships.
To set components to be the same size: Control-click all four of the JTextFields in the form to select them. The JTextFields are all set to the same width and indicators are added to the top edge of each, providing visual feedback of the component relationships. Now we need to add another JLabel describing the JComboBox that will enable users to select the format of the information our ContactEditor application will display.
When the guideline appears indicating that the new JLabel's right edge is aligned with the right edges of the component group above the two JLabels , click to position the component. The JLabel snaps into a right-aligned position with the column of JLabels above, as shown in the following illustration.
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. Baseline Alignment 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.
To align the baselines of components: In the Palette window, select the Combo Box component from the Swing Controls category. Move the cursor immediately to the right of the JLabel we just added. When the horizontal guideline appears indicating that the JComboBox's baseline is aligned with the baseline of the text in the JLabel and the spacing between the two components is suggested with a vertical guideline, click to position the combo box. 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. Drag the resize handle on the JComboBox's right edge toward the right until the alignment guidelines appear suggesting the preferred offset between the JComboBox and JPanel edges. 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. 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.Many interface designers consider this a "best practice" technique, however, for the purposes of this tutorial you can simply peek at how our completed form should look by jumping ahead to the Previewing your GUI section.
Select the bottom JPanel and repeat steps 2 through 5, but this time right-click the JPanel and access the Properties window using the pop-up menu. In this section we'll add a few JRadioButtons below a JLabel that will allow users to customize the way the application displays data.
It also tells you how to use borders and icons. Whenever you add a component between two existing components, the GUI Builder automatically shifts them to make room for the new component.
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. When the vertical alignment guidelines appear suggesting the margin between the text field and JPanel edges, release the mouse button to resize the JTextField.
Code Example This example creates 6 buttons, which are arranged in 3x2 and 2x3 GridLayout alternately upon clicking any button.