Search code examples
javaswingflowlayout

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?


Solution

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