How To Draw A Resizable Grid In Java Dynamically
Note: This lesson covers writing layout lawmaking by mitt, which tin be challenging. If you are non interested in learning all the details of layout management, you might adopt to apply the GroupLayout layout manager combined with a builder tool to lay out your GUI. One such architect tool is the NetBeans IDE. Otherwise, if you want to code past hand and do not want to employ GroupLayout, then GridBagLayout is recommended as the next about flexible and powerful layout manager.
If you are interested in using JavaFX to create your GUI, run across Working With Layouts in JavaFX.
The following figure represents a snapshot of an awarding that uses the GridLayout class.
Click the Launch button to run GridLayoutDemo using Java™ Web Offset (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.
The complete code of this demo is in the GridLayoutDemo.java file.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the jail cell size so that the cells are as large equally possible, given the space available to the container.
The code snippet beneath creates the GridLayout object and the components it manages.
GridLayout experimentLayout = new GridLayout(0,2); ... compsToExperiment.setLayout(experimentLayout); compsToExperiment.add together(new JButton("Push button 1")); compsToExperiment.add together(new JButton("Push button 2")); compsToExperiment.add(new JButton("Button iii")); compsToExperiment.add(new JButton("Long-Named Button 4")); compsToExperiment.add(new JButton("5")); The constructor of the GridLayout class creates an instance that has two columns and as many rows equally necessary.
Utilise combo boxes to set up how much vertical or horizontal padding is put effectually the components. Then click the Apply gaps button. The post-obit lawmaking snippet shows how your selection is processed past using the setVgap and setHgap methods of the GridLayout class:
applyButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ //Get the horizontal gap value String horGap = (Cord)horGapComboBox.getSelectedItem(); //Get the vertical gap value Cord verGap = (String)verGapComboBox.getSelectedItem(); //Set up the horizontal gap value experimentLayout.setHgap(Integer.parseInt(horGap)); //Set up the vertical gap value experimentLayout.setVgap(Integer.parseInt(verGap)); //Set upwards the layout of the buttons experimentLayout.layoutContainer(compsToExperiment); } }); The GridLayout API
The following table lists constructors of the GridLayout form that specify the number of rows and columns.
| Constructor | Purpose |
|---|---|
GridLayout(int rows, int cols) | Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size. One, simply not both, of rows and cols can exist zero, which means that any number of objects can be placed in a row or in a column. |
GridLayout(int rows, int cols, int hgap, int vgap) | Creates a grid layout with the specified number of rows and columns. In add-on, the horizontal and vertical gaps are fix to the specified values. Horizontal gaps are places between each of columns. Vertical gaps are placed betwixt each of the rows. |
The GridLayout class has two constructors:
Examples that Use GridLayout
The following table lists code examples that use the GridLayout class and provides links to related sections.
| Example | Where Described | Notes |
|---|---|---|
GridLayoutDemo | This page | Uses a two-cavalcade filigree. |
ComboBoxDemo2 | How to Use Combo Boxes | One of many examples that employ a 1x1 grid to make a component as large as possible. |
LabelDemo | How to Use Labels | Uses a 3-row grid. |
Source: https://docs.oracle.com/javase/tutorial/uiswing/layout/grid.html
Posted by: blackmermexamo.blogspot.com

0 Response to "How To Draw A Resizable Grid In Java Dynamically"
Post a Comment