 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add components with a relative Y position in Java?\\n
To add components with a relative Y position, use the GridBagConstraints.RELATIVE constant. Set this to gridy field −
GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE;
The following is an example to add components with a relative Y position in Java −
Example
package my; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class SwingDemo {    public static void main(String[] args) {       JFrame frame = new JFrame();       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       JPanel panel = new JPanel();       panel.setLayout(new GridBagLayout());       GridBagConstraints constraints = new GridBagConstraints();       constraints.gridx = 0;       panel.add(new JButton("1st row"), constraints);       constraints.gridx = 1;       constraints.gridy = GridBagConstraints.RELATIVE;       panel.add(new JButton("1st row 2nd column"), constraints);       panel.add(new JButton("2nd row 2nd column"), constraints);       frame.add(panel);       frame.setSize(550, 300);       frame.setVisible(true);    } }  Output

Advertisements
 