Soheil
Soheil

Reputation: 1706

java- FlowLayout from right to left

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?

Upvotes: 2

Views: 6172

Answers (3)

camickr
camickr

Reputation: 324118

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

Upvotes: 7

MadProgrammer
MadProgrammer

Reputation: 347204

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

Upvotes: 4

Adam
Adam

Reputation: 36703

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

Upvotes: 4

Related Questions