FlowLayout
adds new component to the right of the last component. I mean it arranges components from left to right(>>>>), but I need the arrangement from right to left(<<<<). is it possible?
Asked
Active
Viewed 5,729 times
2
-
Have you reviewed [Laying Out Components Within a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html)? – dbank Mar 11 '15 at 20:46
-
You want the components aligned to the right or you want them ordered right to left? – MadProgrammer Mar 11 '15 at 20:47
-
@MadProgrammer ordered right to left – Soheil Mar 11 '15 at 20:50
-
What determines the order? Are you adding them dynamically? – MadProgrammer Mar 11 '15 at 20:51
-
@MadProgrammer yeah I'm adding dynamically – Soheil Mar 11 '15 at 20:52
-
Then you can put them into an ArrayList, and each time you need to add a new component, remove all components, re-add them in reverse order, and then revalidate and repaint the container. – Hovercraft Full Of Eels Mar 11 '15 at 20:53
3 Answers
7
Add the components to the beginning of the panel:
panel.add(component, 0);
Or, set the component orientation, then you add the buttons normally:
panel.setLayout( new FlowLayout(FlowLayout.RIGHT) );
panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
panel.add( new JButton("1") );
panel.add( new JButton("2") );
panel.add( new JButton("3") );
panel.add( new JButton("4") );

camickr
- 321,443
- 19
- 166
- 288
3
FlowLayout does not provide this facility, only various justification options. Could you just add the components in reverse order?
JFrame frame = new JFrame();
frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
frame.add(new JButton("3"));
frame.add(new JButton("2"));
frame.add(new JButton("1"));
frame.setSize(200, 200);
frame.setVisible(true);

Adam
- 35,919
- 9
- 100
- 137
-
nice but suppose i need to add 6 jbuttons, then the first 3 ones go to second line...this is the problem :( – Soheil Mar 11 '15 at 20:53
-
3
FlowLayout
simply honours the position (or z-order) that the components where added. You can specify the position a component should be added using a verity of add
methods provided by the JContainer
, for FlowLayout
which takes no constraints, you can simply use add(Component, int)
, for example...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JavaApplication252 {
public static void main(String[] args) {
new JavaApplication252();
}
public JavaApplication252() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JPanel mainPane = new JPanel();
JButton btn = new JButton("Add");
btn.addActionListener(new ActionListener() {
int count;
@Override
public void actionPerformed(ActionEvent e) {
mainPane.add(new JLabel(Integer.toString(count++)), 0);
mainPane.revalidate();
mainPane.repaint();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPane);
frame.add(btn, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
The only other choice you would be to create your own Layout Manager (probably based on FlowLayout
) which laid the components out in reverse order

MadProgrammer
- 343,457
- 22
- 230
- 366
-
very good, but suppose i add more and more components(more than one line), then this doesnt work... – Soheil Mar 11 '15 at 21:01
-
1FlowLayout doesn't wrap well. Consider using the [WrapLayout](https://tips4java.wordpress.com/2008/11/06/wrap-layout/) which is an extended version of FlowLayout to do just that – MadProgrammer Mar 11 '15 at 21:04
-
I added WrapLayout but i cant get how to use it to meet my dreams! could u please explain more? – Soheil Mar 11 '15 at 21:19