Java ChoiceDialog reorders default buttons

Contrary to expectations, the default button order in ChoiceDialog is “Cancel” on the left and “OK” on the right.

In this method, I create a dialogue that lists the Sex of the cattle (Cow, Heifer, Steer, etc.). Everything works fine using the default dialog box, except that the order of the buttons is the reverse of every other custom dialog box I created.

I figured the Java Design and Design Guide requires a dialog with OK on the left and Cancel on the right.

In any case, as the code below shows, I tried to set my own buttons. Changing the order in which they are added does not matter.

However, I may have the order that I am looking for if ButtonData.OTHER, but then I do not get the desired result (selected sex).

All I really want to do is change the order of the buttons and get the desired result.

public static final String getChoice_Sex()
{
    String sex = "";
    List<String> list_Sexs = LM_Constant.getList_Sexs();

    ChoiceDialog<String> dialog = new ChoiceDialog<>(list_Sexs.get(0), list_Sexs);
    dialog.setTitle("Sex");
    dialog.setHeaderText("Please choose one.");
    dialog.setContentText("From this list:");

    //ButtonType button_OK = new ButtonType("OK", ButtonData.OK_DONE); 
    //ButtonType button_OK = new ButtonType("OK", ButtonData.OTHER); 
    //ButtonType button_Cancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

    //dialog.getDialogPane().getButtonTypes().clear();
    //dialog.getDialogPane().getButtonTypes().add(button_Cancel);
    //dialog.getDialogPane().getButtonTypes().add(button_OK);
    //dialog.getDialogPane().getButtonTypes().add(button_Cancel);

    Optional<String> result = dialog.showAndWait();        
    if (result.isPresent())
        sex = result.get();

    return sex;
}
+4
source share
1 answer

The order of the buttons in the dialog box (and dialog boxes) can be configured as follows:

  • Create a subclass ChoiceDialog(e.g. CattleGenderDialog)
  • Create a subclass DialogPane(e.g. ButtonOrderPane).
  • Set the dialog box CattleGenderDialogto use ButtonOrderPane.
  • Have the ButtonOrderPaneback buttons in your preferred order.

Once class relationships are understood, the code is trivial.

CattleGenderDialog Class

public class CattleGenderDialog extends ChoiceDialog {
  public CattleGenderDialog( final Window owner ) {
    initOwner( owner );

    // This is the key line to override button order.
    setDialogPane( new ButtonOrderPane() );

    // This line will return the button order pane.
    final DialogPane dialogPane = getDialogPane();
    dialogPane.setContent( pane );
    dialogPane.getButtonTypes().addAll( ... );
  }
}

ButtonOrderPane Class

import static javafx.scene.control.ButtonBar.BUTTON_ORDER_WINDOWS;

public class ButtonOrderPane extends DialogPane {

  // This is where the revised button order is defined.
  @Override
  protected Node createButtonBar() {
    final ButtonBar node = (ButtonBar)super.createButtonBar();
    node.setButtonOrder( getButtonOrder() );
    return node;
  }

  private String getButtonOrder() {
    return getSetting( "dialog.alert.button.order.windows", BUTTON_ORDER_WINDOWS );
  }

  private String getSetting( final String key, final String defaultValue ) {
    return getSettings().getSetting( key, defaultValue );
  }

  // How you load the settings is up to you; I prefer decoupled code.
  private Settings getSettings() {
    return Services.load( Settings.class );
  }
}

Settings

:

dialog.alert.button.order.mac=L_HE+U+FBIX_NCYOA_R
dialog.alert.button.order.linux=L_HE+UNYACBXIO_R
dialog.alert.button.order.windows=L_E+U+FBXI_YNOCAH_R

, . (, Mac Linux.)

+1

Source: https://habr.com/ru/post/1660224/


All Articles