Skip to main content

How to change the component orientation ?

2 replies [Last post]
Joined: 2010-09-23


I'm currently using a JPanel with a GridLayout. Then I'm adding a lot of JCheckBox for sorted items to be checked by the end-user.

These items are added Left to Right first then Top to Bottom.

Most of my users asked for changing this orientation Top-Bottom first then Left to Right.

I look at the ComponentOrientation class, and I haven't seen any way to change this order.
The only country which seems to do this is .... Mongolia (TL). Is there a way to define Locale(Mongolia) just for this JPanel and use this with SetComponentOrientation ? If so, how to code this ?

Or is there a more simple way for doing this (except specifying layouts inside GridLayout).

Thanks a lot for help


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2010-08-05

Just change the way you add the check boxes to the panel.[code]import java.awt.GridLayout;
import javax.swing.*;

public class VerticalGridLayout {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
new VerticalGridLayout().makeUI();

public void makeUI() {
JPanel panel = new JPanel(new GridLayout(4, 4));
JComponent[] components = new JComponent[16];
for (int i = 0; i < 16; i++) {
components[i] = new JButton("Button " + i);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
panel.add(components[i % 4 + j * 4]);
JFrame frame = new JFrame();
frame.setSize(400, 400);

Joined: 2010-09-23

Many thanks Darryl for your answer and sorry for the delay I was quite busy since my original post.

Even if I thought it exists something easier, it helped me a lot.
However, beause the number of items is variable, and the number of lines and columns are not equal, I had to to adapt your sample.
Here is my code. If you have time to have a look, your comments are very welcome.
Thanks again for all.


int nbC = 5, // number of columns
nbL = 20; // number of lines
Vector v = createItemList(cat);
int sz = v.size();
mypanel= new JPanel();
mypane.setBounds(15, 69, 855, 423); // generated by my IDE
mypane].setLayout(new java.awt.GridLayout(nbL,nbC));
javax.swing.JCheckBox ck = null;
for (int j=0; j< nbL ;j++)
for (int k=0; k {
int pos = j+k*nbL; // item index in vector
if (pos else
ck = new javax.swing.JCheckBox("dummy");
mypanel.add(ck );