I have 9 jbuttons added to jpanel and the panel added to jscrollpane and it added to jframe.
http://www.pic1.iran-forum.ir/images/up9/95426323683658592564.jpg
when i change frame orientation by:
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
the panel move to right and size of buttons got fixed and wouldn`t fill the panel but you see in the image below that scrolbar is fill all width of panel
http://www.pic1.iran-forum.ir/images/up9/60975202722295688553.jpg
(i used gridbaglayout for adding buttons and borderlayout.center for add scrollpane).
is that a bug in java or ?
EDIT:
its the simplest view . Does it help?
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class MyFrame extends JFrame{
private JButton[] arrayButton = new JButton[9];
private JButton btnLeft = new JButton("<");
private JButton btnRight = new JButton(">");
private JScrollPane scpButtons = new JScrollPane();
public MyFrame() {
for (int i = 0; i < arrayButton.length; i++)
arrayButton[i] = new JButton("btn");
JPanel pnlButton = initPnlButton();
scpButtons.setViewportView(pnlButton);
setLayout(new BorderLayout());
add(scpButtons, BorderLayout.CENTER);
// comment it and see the result
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
pack();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setVisible(true);
}
private JPanel initPnlButton() {
JPanel pnlButton = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, 10,
1, new Insets(0, 0, 0, 0), 0, 0);
int ind = 0;
int row = 3;
int column = 4;
for (int i = 0; i < row; i++) {
for (int j = 1; j < column; j++) {
gbc.gridx = j;
gbc.gridy = i;
pnlButton.add(arrayButton[ind++], gbc);
}
}
gbc.weightx = 0;
gbc.gridheight = 3;
gbc.gridx = 0;
gbc.gridy = 0;
pnlButton.add(btnLeft, gbc);
gbc.gridx = 4;
gbc.gridy = 0;
pnlButton.add(btnRight, gbc);
pnlButton.setPreferredSize(new Dimension(1000, 700));
return pnlButton;
}
public static void main(String[] args) {
new MyFrame();
}
}
after playing with lots of properties and moving up and down the statements i found the answer.
if you put
after
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);everything will be okay and no need to
setSize(getWidth() + 1, getHeight() + 1);so funny it got a whle day from me 🙂 does c# or other langs have bugs like this?
if you let
SwingUtilitiesbloc to run , befor resizing the window everything are yet LtR.}