Search code examples
javaswingalignmentlayout-managerboxlayout

AlignmentX and Y of a JComponent without a BoxLayout


I've seen some people using the setAlignmentX() and setAlignmentY() methods without a BoxLayout (e.g. when using a BorderLayout and other kinds of layouts).

Is this correct or does it do something at all? I haven't found any documentation explaining the use of alignmentX and alignmentY in JComponents but I have always thought it only affects BoxLayouts.


Solution

  • As @camickr stated in the comments, alignmentX and alignmentY are attributes of a JComponent, and a layout manager can choose to support/respect them or not.

    From the native layout managers, only BoxLayout does support this attributes, so if you try to use them on other layouts (e.g. FlowLayout, BorderLayout...) it won't do anything.

    Of course you could create your custom layout manager and choose to respect these attributes or not.