Which layout is best in swing




















The width of the buttons changes to accommodate the new names. 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. Refer to the following illustrations while accomplishing this or click the View Demo link following the procedure to view an interactive demonstration.

Make certain the label is left aligned with the JList above. Move the cursor below the JLabel that we just added. Shift-click to place the first radio button. Move the cursor to the right of the first JRadioButton. Shift-click to place the second and third JRadioButtons, being careful to respect the suggested component spacing. Make certain to release the Shift key prior to positioning the last JRadioButton. Set the display text for each JRadioButton. 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.

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. Move the cursor over the form below the E-mail JPanel. Enter OK for the left button and Cancel for right one. Notice that the width of the buttons changes to accommodate the new names.

The JButton components appear in the form and their corresponding nodes are displayed in the Navigator window. 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.

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. The dist folder might also contain a lib folder, which you would also need to include. 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. Apache NetBeans. Latest release. 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.

Enter ContactEditor in the Project Name field and specify the project location. Click Finish. Enter my. For advanced developers, the Palette Manager is available that enables you to add custom components from JARs, libraries, or other projects to the Palette. Visual Feedback The GUI Builder also provides visual feedback regarding component anchoring and chaining relationships.

Refer to the Adding individual and multiple components. Release the resize handle to resize the component. In the Properties pane below, enter Name for the Title property.

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 the Palette window, select the Label component from the Swing Controls category.

Type First Name: and press Enter. Double-click the first JLabel to select its display text. Type Title: and press Enter. Inserting Components Refer to the Inserting components. In the Palette window, select the Text Field component from the Swing category. Press Ctrl-S to save the file. Moving Forward Alignment is one of the most fundamental aspects of creating professional-looking GUIs.

Component Alignment Refer to the Aligning and anchoring components. Control-click all four of the JTextFields in the form to select them. In the Palette window, select the Label component from the Swing category. 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.

Adding, Aligning, and Anchoring The GUI Builder enables you to lay out your forms quickly and easily by streamlining typical workflow gestures. In the Palette window, select the List component from the Swing Controls category.

Component Sizing Refer to the Resizing and indenting components. In the Palette window, select the Button component from the Swing Controls category. A BorderLayout places components in up to five areas: top, bottom, left, right, and center. All extra space is placed in the center area. Tool bars that are created using JToolBar must be created within a BorderLayout container, if you want to be able to drag and drop the bars away from their starting positions.

For further details, see How to Use BorderLayout. The BoxLayout class puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.

For further details, see How to Use BoxLayout. The CardLayout class lets you implement an area that contains different components at different times. A CardLayout is often controlled by a combo box, with the state of the combo box determining which panel group of components the CardLayout displays. An alternative to using CardLayout is using a tabbed pane , which provides similar functionality but with a pre-defined GUI.

For further details, see How to Use CardLayout. FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row, starting a new row if its container is not sufficiently wide. The container is divided into equally sized rectangles. One component is placed in each rectangle. The example shows a skeleton of a simple calculator tool. We put nineteen buttons and one label into the manager. Notice that each button is of the same size. Here we set the grid layout manager for the panel component.

The layout manager takes four parameters. The number of rows, the number of columns and the horizontal and vertical gaps between components. BorderLayout is a simple layout manager that can be handy in certain layouts. It has a serious limitiation — it sets the gaps between its children in pixels, thus creating rigid layouts. This leads to non-portable UI, and therefore, its usage is not recommended. BorderLayout divides the space into five regions: north, west, south, east, and centre. Each region can have only one component.

If we need to put more components into a region, we have to put a panel there with a manager of our choice. The components in N, W, S, E regions get their preferred size. The component in the centre takes up the whole space left. It does not look good if child components are too close to each other. We must put some space among them. Each component in Swing toolkit can have borders around its edges. To create a border, we either create a new instance of an EmptyBorder class or we use a BorderFactory.

Here we placed the top panel into the bottom panel component. More precisely, we placed it into the center area of its BorderLayout manager. Here we created a 20px border around the bottom panel.

The border values are as follows: top, left, bottom and right. Note that creating fixed insets spaces is not portable. The example shows a typical application skeleton. We show a vertical and horizontal toolbars, a statusbar, and a central component a text area. BorderLayout is the default layout manager for the JFrame container. So we do not have to set it explicitly.

In order to put some empty space around the button, we have to use EmptyBorder. This adds some fixed space to the top and bottom of the button. When we add fixed spaces, the UI is not portable. A 3 px space may look OK on a x screen but it is inappropriate on a x px screen. CardLayout is a simple layout manager that treats each component as a card.

The container is a stack of these cards. Only one component is visible at a time; the rest is hidden. The first component added to the container is visible by default when the container is initially displayed. This manager has a limited practical use. It can be used to create a wizard or a tabbed pane.

The following example uses a CardLayout manager to create a gallery of images. We use four images of the Krasna Horka castle before the fire in We create the main panel component. We set its colour to dark gray. We put 5px around the panel so that its children are not too close to the border of the window. Clicking on the Previous button the previous method of the manager is called.

It flips to the previous card of the specified container. We add the main panel to the center area of the border layout of the frame component. If we do not specify explicitly where we place the component, it is added to the center area. Finally, the panel with the buttons is placed into the south area of the BorderLayout manager.



0コメント

  • 1000 / 1000