skip to Main Content

I want to rotate a vertical slider in javafx so that I start with 0 from the top.

I tried this:

.slider {
    -fx-rotate: 180;
}

But then i got this:

enter image description here

I would like to leave it like that, but unfortunately I haven’t been able to turn the numbers the right way around.

How can i rotate the numbers too? Or is there generally another way to turn the slider upside down?

Thanks for every answer!

2

Answers


  1. I would look at the label formatter as jewselsea points out, and yes it can be tricky.

    Maybe this can help you:

    package org.example;
    import javafx.application.Application;
    import javafx.geometry.Orientation;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Slider;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class App extends Application {
    
        @Override
        public void start(Stage stage) {
    
            Slider slider = new Slider();
            slider.setOrientation(Orientation.VERTICAL);
            slider.setPrefHeight(500);
            slider.setMin(0);
            slider.setMax(6);
            slider.setShowTickLabels(true);
            slider.setShowTickMarks(true);
            slider.setMajorTickUnit(1);
            slider.setLabelFormatter(new StringConverter() {});
    
            slider.valueProperty().addListener(((observable, oldValue, newValue) -> {
    
                System.out.println(Invers.return0ToX(newValue.intValue()));
    
            }));
    
            VBox vBox = new VBox();
            vBox.setAlignment(Pos.CENTER);
            vBox.getChildren().add(slider);
    
            BorderPane borderPane = new BorderPane();
            borderPane.setCenter(vBox);
    
            var scene = new Scene(borderPane, 300, 600);
    
            stage.setScene(scene);
            stage.show();
    
        }
    
        public static void main(String[] args) {
            launch();
        }
    
    }
    

    With the StringConverter class as this:

    package org.example;
    
    public abstract class StringConverter extends javafx.util.StringConverter<Double> {
    
        @Override
        public String toString(Double value) {
    
            return Integer.toString(Invers.return0ToX(value.intValue()));
    
        }
    
        @Override
        public Double fromString(String string) {
    
            return null;
    
        }
    
    }
    

    And this class for inverting the values:

    package org.example;
    import java.util.Arrays;
    import java.util.Collections;
    
        public class Invers {
        
            static Integer[] arrayWith0ToX = new Integer[7];
        
            public static int return0ToX(int input) {
        
                for (int x = 0; x <= arrayWith0ToX.length - 1; x++) {
        
                    arrayWith0ToX[x] = x;
        
                }
        
                Collections.reverse(Arrays.asList(arrayWith0ToX));
        
                return arrayWith0ToX[input];
        
            }
        
        }
    

    Above will give you this:

    enter image description here

    Edit above to suit your needs, but I’m not sure this is want you want, you need to describe in more detail what you want for me to be more precise.

    Login or Signup to reply.
  2. Try setting the setLabelFormatter.

    import javafx.application.Application;
    import javafx.geometry.Orientation;
    import javafx.scene.Scene;
    import javafx.scene.control.Slider;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    import javafx.util.StringConverter;
     
    public class App extends Application {
     
        @Override
        public void start(Stage stage)
        {
     
            // creating group
            StackPane root = new StackPane();
            Scene scene = new Scene(root, 600, 400);
     
            // set Scene to the stage
            stage.setScene(scene);
     
            // set title for the frame
            stage.setTitle("Slider Sample");
     
            // create slider
            Slider slider = new Slider();
            slider.setMin(-10);
            slider.setMax(0);
            slider.setValue(5);
            slider.setShowTickLabels(true);
            slider.setShowTickMarks(true);
            slider.setMajorTickUnit(1);
            slider.setBlockIncrement(1);
            slider.setOrientation(Orientation.VERTICAL);
            slider.setLabelFormatter(new StringConverter<Double>(){
    
            @Override
            public String toString(Double object) {
                if(object < 0)
                {
                    Double postive = object * -1;
                    return postive.toString();
                }
                
                return object.toString();
            }
    
            @Override
            public Double fromString(String string) {
                throw new UnsupportedOperationException("Not supported yet."); 
            }
    
        });
            // add slider to the frame
            root.getChildren().add(slider);
     
            stage.show();
        }
     
        // Main Method
        public static void main(String[] args)
        {
     
            // launch the application
            launch(args);
        }
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search