2

I am coding on a little histogram with JavaFX. I used BarChart as the node and added the XYChart series to BarChart. My code worked quite good until I found this:

Picture of category labels grouped at the origin

The labels are overlapping each other.

Then when I manually resize the window:

Picture of category labels correctly positioned after resizing window

So, my question is how can I make it to adjust itself without I doing it manually?

Here is a brief view of my code:

    //  Make an axis for x and one for y. Then, create a chart for the histogram by using x and y axis.
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String,Number>histogramChart = new BarChart<String,Number>(xAxis,yAxis);
    BorderPane myBorderPane = new BorderPane();   //  Use a border pane for the histogram and the button. 
    Button btEnter = new Button("ENTER");
    TextField tfDirectory = new TextField();
    Scanner input;
    XYChart.Series seriesOfAlphabet = new XYChart.Series();
    HBox hbxDirectoryAndButton = new HBox();
    //  A final string array for the arphabet
    final static String[] alphabets= {"A","B","C","D","E","F"
                                     ,"G","H","I","J","K","L"
                                     ,"M","N","O","P","Q","R"
                                     ,"S","T","U","V","W","X"
                                     ,"Y","Z"};

    //  Main to launch the application
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        //  Set the title of the stage
        primaryStage.setTitle("Histogram Of Occurences of Letters by Luckas(YingHong Huang");
        //  Set the title of the chart
        histogramChart.setTitle("Histogram");
        xAxis.setLabel("Alphets");          // Set label of the xAxis
        yAxis.setLabel("Occurences");       // Set the label of yAxis
        tfDirectory.setLayoutY(20);         //  Set the height of the text field for the file input
        tfDirectory.setLayoutX(100);        //  Set the length of the text field for the file input
        seriesOfAlphabet.setName("Occurences");
        btEnter.setOnAction(e->{
            openFile();
        });    
        histogramChart.getData().add(seriesOfAlphabet);
        hbxDirectoryAndButton.getChildren().addAll(tfDirectory,btEnter);     
        hbxDirectoryAndButton.setSpacing(10);
        myBorderPane.setCenter(histogramChart);
        myBorderPane.setBottom(hbxDirectoryAndButton);
        myBorderPane.setPadding(new Insets(5,5,5,5));
        Scene histogramScene = new Scene(myBorderPane,1500,700);
        primaryStage.setScene(histogramScene);
        primaryStage.show();
    }
Slaw
  • 37,820
  • 8
  • 53
  • 80
Lu___
  • 25
  • 6

1 Answers1

2

I came across the same problem today. I used Javafx Scene Builder and unselected "Animated" in Properties solved the problem. Hope it helps.

2zki333
  • 36
  • 2