2

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?

dbank
  • 1,173
  • 1
  • 17
  • 29
Soheil
  • 1,676
  • 7
  • 34
  • 65

3 Answers3

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);

Example

Adam
  • 35,919
  • 9
  • 100
  • 137
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...

Layout Order

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
  • 1
    FlowLayout 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