- 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 relative X and Y Coordinates in Java?
To add components with relative X and Y coordinates, you need to set both the gridx and gridy components −
GridBagConstraints constraints = new GridBagConstraints(); constraints.gridy = GridBagConstraints.RELATIVE; constraints.gridx = GridBagConstraints.RELATIVE;
The following is an example to add components with relative X and Y Coordinates −
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 = 1; constraints.gridy = GridBagConstraints.RELATIVE; panel.add(new JButton("1st row 1st column"), constraints); panel.add(new JButton("2nd row"), constraints); panel.add(new JButton("3rd row"), constraints); constraints.gridx = GridBagConstraints.RELATIVE; panel.add(new JButton("1st row 2nd column"), constraints); panel.add(new JButton("1st row 3rd column"), constraints); frame.add(panel); frame.setSize(550, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to add components with a Relative X Position in Java
- How to add components with a relative Y position in Java?\n
- How to get the Tkinter widget's current x and y coordinates?
- How to find the coordinates of the cursor relative to the screen with JavaScript?
- How to set orientation and split components along x-axis in Java?
- How to deal with reusable components in Selenium Java?
- Java Program to set Orientation and split components along y-axis
- How to add a shared x-label and y-label to a plot created with Pandas' plot? (Matplotlib)
- Add: $20 x^{2} y^{2} z and -10 x^{2} y^{2} z$
- How to add the coordinates in a Polygon using FabricJS?
- How to left align components vertically using BoxLayout with Java?
- Java ResultSet relative() method with example
- Determine graphically the coordinates of the vertices of a triangle, the equations of whose sides are:$y=x, y=2x$ and $y+x=6$
- How to plot bar graphs with same X coordinates side by side in Matplotlib?
- How to separate Components in a Row or Column with Box in Java

Advertisements