- 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
How to add components with a Relative X Position in Java
To add components with a relative X position, use the GridBagConstraints.RELATIVE constant. Set this to gridx field −
GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = GridBagConstraints.RELATIVE;
The following is an example to add components with a relative X 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.gridy = 0; panel.add(new JButton("First row"), constraints); constraints.gridx = GridBagConstraints.RELATIVE; constraints.gridy = 1; panel.add(new JButton("2nd row 1st column"), constraints); panel.add(new JButton("2nd row 2nd column"), constraints); frame.add(panel); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to add components with a relative Y position in Java?\n
- How to add components with relative X and Y Coordinates in Java?
- CSS position: relative;
- How to add elements to AbstractSequentialList class at a specific position in Java?
- How to set div position relative to another div in JavaScript?
- How to deal with reusable components in Selenium Java?
- How to return the position of a document relative to the collection in MongoDB?
- How to set orientation and split components along x-axis in Java?
- How to separate Components in a Row or Column with Box in Java
- Python Tkinter – How to position a topLevel() widget relative to the root window?
- How to set position relative to top of a canvas circle using Fabric.js?
- How to add separator in a ToolBar with Java?
- How to add a Tab in JTabbedPane with Java?
- How to work with border layout position options in Java?
- How to return the position of the element relative to floating objects in JavaScript?

Advertisements