Skip to main content

How to change the component orientation ?

2 replies [Last post]
gegemartinelli
Offline
Joined: 2010-09-23
Points: 0

Hello,

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

Gégé

Reply viewing options

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

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() {

@Override
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.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}[/code]
db

gegemartinelli
Offline
Joined: 2010-09-23
Points: 0

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.

Gégé

[code]
int nbC = 5, // number of columns
nbL = 20; // number of lines
Vector v = createItemList(cat);
int sz = v.size();
mypanel= new JPanel();
mypane].setName(cat);
mypane.setBounds(15, 69, 855, 423); // generated by my IDE
mypane].setLayout(new java.awt.GridLayout(nbL,nbC));
mypanel.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
mypanel.setBorder(compound);
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");
ck.setVisible(false);
}
mypanel.add(ck );
}
}
[/code]