Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Radio Button Label on Left

edward17Aug 23 2012 — edited Aug 23 2012
How does on get the JavaFX Radio Button to display the label on the LEFT


"My Label" X insetad of X "My label" (where X is the button)

Comments

David Grieve-Oracle
http://docs.oracle.com/javafx/2/api/javafx/scene/control/Labeled.html#setContentDisplay(javafx.scene.control.ContentDisplay)
edward17
Cryptic API reference not helpful.
957628
radioButton.setContentDisplay(ContentDisplay.LEFT);
jsmith
ContentDisplay seems like a good solution - it just doesn't work for RadioButtons.
Most of the radio buttons with different content display settings in the example below end up being laid out identically.
package javafxsamples;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.RadioButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class RadioContentDisplayFailure extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage stage) throws Exception {
    VBox radios = new VBox(15);
    for (ContentDisplay display: ContentDisplay.values()) {
      RadioButton radio = new RadioButton("radio-text: " + display);
      radio.setStyle("-fx-background-color: coral; -fx-border-color: red;");
      radio.setContentDisplay(display);
      radios.getChildren().add(radio);
    }
    radios.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    stage.setScene(new Scene(radios));
    
    stage.show();
  }
}
edward17
The FXML version of this solution is ignored and the code version (as indicated) results in an error:

WARNING: com.sun.javafx.css.StyleHelper calculateValue caught: java.lang.IllegalArgumentException: Parsed value is not an Effect

Edited by: edward17 on Aug 23, 2012 12:51 PM
jsmith
The FXML version of this solution is ignored
Nobody provided as FXML solution for this, I guess you wrote one and it worked as my code sample did (i.e. the ContentDisplay setting for a radio button does nothing), which is what I would have expected given the code sample output.
the code version (as indicated) results in an error . . . .css.StyleHelper . . . Parsed value is not an Effect
The code sample I provided does not have any reported error (it just shows that ContentDisplay does not really affect the layout of a RadioButton).

I believe the stylehelper error you reported is related to some other css you use in your application which you have not published here and is unrelated to radio button layout.
David Grieve-Oracle
Yeah. I should know better. setContentDisplay has to do with the graphic, not the radio button itself. Here's a hack.
     @Override
    public void start(Stage primaryStage) {

        RadioButton rb = new RadioButton("RadioButton");        
        StackPane root = new StackPane();
        root.getChildren().add(rb);
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
       
        Node radio = rb.lookup(".radio");
        Node text = rb.lookup(".text");
        double rx = radio.getBoundsInParent().getMinX();
        double tx = text.getBoundsInParent().getMaxX();
        radio.setLayoutX(tx);
        text.setLayoutX(rx);
}
shakir.gusaroff
Here is another example (but not smart):
  
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;


public class RadioBtns extends Application {

    @Override
    public void start(Stage primaryStage) {

        ToggleGroup grp = new ToggleGroup();
        VBox vb = new VBox(5);
        vb.setPrefWidth(50);
        
        RadioButton rbHome = new RadioButton();
        rbHome.setToggleGroup(grp);
        rbHome.setSelected(true);
        Label lHome = new Label("Home ");
        HBox hbHome = new HBox();
        hbHome.setAlignment(Pos.CENTER_RIGHT);
        hbHome.getChildren().addAll(lHome, rbHome);



        RadioButton rbCal = new RadioButton();
        rbCal.setToggleGroup(grp);
        Label lCal = new Label("Calendar ");
        HBox hbCal = new HBox();
        hbCal.setAlignment(Pos.CENTER_RIGHT);
        hbCal.getChildren().addAll(lCal, rbCal);


        RadioButton rbContact = new RadioButton();
        rbContact.setToggleGroup(grp);
        Label lContact = new Label("Contact ");
        HBox hbContact = new HBox();
        hbContact.setAlignment(Pos.CENTER_RIGHT);
        hbContact.getChildren().addAll(lContact, rbContact);
       
        vb.getChildren().addAll(hbHome, hbCal, hbContact);

        Scene scene = new Scene(vb, 300, 250);

        primaryStage.setTitle("Radio buttons");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
jsmith
Thanks for posting the hack David.

I created http://javafx-jira.kenai.com/browse/RT-24461 "Positioning of a radio button dot relative to it's label" to track whether a more elegant solution should be added to the core api or not.
edward17
Thanks Shakir, I agree is kludgy but I used your solution in the interim
1 - 10
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 20 2012
Added on Aug 23 2012
10 comments
1,348 views