Horizontal placement of buttons in BoxLayout depending on OS

248 views Asked by At

I am placing [OK] and [Cancel] buttons horizontally in a Box (which has BoxLayout). Later I add this Box to BorderLayout.PAGE_END in content pane of JDialog. This works perfect in Windows, so [OK] is to left of [Cancel] - just like I have added.

When I test in Linux, the placement of buttons is same as Windows - [OK] to left of [Cancel]. This, again, follows from code.

However, in Linux (Ubuntu), the default placement for buttons is [Cancel] to left of [OK]. This contrasts with my code and placement of buttons.

So, my question is : Whether Java has some kind of constant - say OS_PLACEMENT, which can be set somewhere which will make [Cancel] button appear to left of [OK] in Linux? I am asking this because I know Java supports constants for RTL and LTR layout. Thus, I thought, there might be some constant(s) related to this kind of placement.

I wish to mention that I do set L&F of my Swing app to System L&F. Following is my first line in main()

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

3

There are 3 answers

1
Steve McLeod On

No, Java will not do this automatically for you.

Consider using the free "JGoodies Form" library. It has a class called "ButtonBarBuilder" which will lay out buttons in the correct order according to the user's OS.

Otherwise you must manually inspect the value of System.getProperty("os.name") and lay out the buttons according to the value.

1
María Arias de Reyna Domínguez On

When you say Linux (Ubuntu) you mean Gnome, right? The button placement by default (OS) of Gnome (Gtk) is placing Cancel button to the left.

You can try to change the Look and Feel of Swing : How to Set the Look and Feel

0
jfpoilpret On

Some LayoutManagers automatically do this for you, for example DesignGridLayout and MigLayout detect the OS the application is running on, and based on it, will choose the right position for OK and Cancel (and also other specific buttons).

For example, with DesignGridLayout you would do:

DesignGridLayout layout = new DesignGridLayout(myDialog);
layout.row().bar().add(okButton, Tag.OK).add(cancelButton, Tag.CANCEL);