- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Distribute extra horizontal and vertical space in a GridBagLayout with Java
To distribute the extra horizontal and vertical space, use the fields weightx and weighty. The following is an example to distribute extra horizontal and vertical space −
Example
package my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Demo Frame"); JPanel panel = new JPanel(); GridBagLayout layout = new GridBagLayout(); panel.setLayout(layout); GridBagConstraints gbc = new GridBagConstraints(); JLabel label = new JLabel("Rank: "); JTextArea text = new JTextArea(); text.setText("Add rank here..."); layout.columnWeights = new double[]{0.0f, 0.0f, 2.0f}; layout.rowWeights = new double[]{0.0f, 1.0f}; gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 2; gbc.weighty = 2; layout.setConstraints(label, gbc); panel.add(label); gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 2; gbc.weighty = 2; layout.setConstraints(text, gbc); panel.add(text); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(550, 400); frame.setVisible(true); } }
Output
- Related Articles
- Horizontal and Vertical Center Alignment with Flexbox in CSS3
- Rearrange positive and negative numbers with constant extra space in C++
- Vertical Text, with Horizontal Letters in CSS
- Reverse Individual Words With O(1) Extra Space
- Java Program to display both horizontal and vertical grid lines in a JTable
- Set the location of component in a GridBagLayout with Java
- How to center a JLabel in a JPanel with GridBagLayout in Java?
- How to disallow resizing component with GridBagLayout in Java
- CSS Central, Horizontal and Vertical Alignment
- Difference between Horizontal and Vertical Relationships
- Vertical and Horizontal Scrollbars on Tkinter Widget
- Difference between vertical integration and horizontal integration
- Difference between Horizontal Equity and Vertical Equity
- Difference Between Horizontal Nystagmus and Vertical Nystagmus
- Split Space Delimited String and Trim Extra Commas and Spaces in JavaScript?

Advertisements