JavaFX Adding Context menu on Line Chart

2019-07-21 07:22发布

I want to add a context menu on right click of my Line Chart. Its basically for re-sizing the chart.Is there any way i can achieve this?

标签: javafx-2
1条回答
成全新的幸福
2楼-- · 2019-07-21 08:01

Here is a code snippet to bring up a resizing context menu on a line chart when the line chart is right clicked.

final MenuItem resizeItem = new MenuItem("Resize");
resizeItem.setOnAction(new EventHandler<ActionEvent>() {
  @Override public void handle(ActionEvent event) {
    System.out.println("Resize requested");
  }
});

final ContextMenu menu = new ContextMenu(
  resizeItem
);

lineChart.setOnMouseClicked(new EventHandler<MouseEvent>() {
  @Override public void handle(MouseEvent event) {
    if (MouseButton.SECONDARY.equals(event.getButton())) {
      menu.show(stage, event.getScreenX(), event.getScreenY());
    }  
  }
});

Some complete sample code:

linechartwithcontext

import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.stage.Stage;

public class LineChartWithContextMenu extends Application {   
    @Override public void start(final Stage stage) {
        stage.setTitle("Line Chart Sample");

        //defining the axes
        final NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Number of Month");
        final NumberAxis yAxis = new NumberAxis();

        //creating the chart
        final LineChart<Number,Number> lineChart = 
                new LineChart<>(xAxis,yAxis);

        lineChart.setTitle("Stock Monitoring, 2010");

        //defining a series
        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");

        //populating the series with data
        series.getData().setAll(
          new XYChart.Data(1, 23),
          new XYChart.Data(2, 14),
          new XYChart.Data(3, 15),
          new XYChart.Data(4, 24),
          new XYChart.Data(5, 34),
          new XYChart.Data(6, 36),
          new XYChart.Data(7, 22),
          new XYChart.Data(8, 45),
          new XYChart.Data(9, 43),
          new XYChart.Data(10, 17),
          new XYChart.Data(11, 29),
          new XYChart.Data(12, 25)
        );

        lineChart.getData().add(series);

        //adding a context menu item to the chart
        final MenuItem resizeItem = new MenuItem("Resize");
        resizeItem.setOnAction(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent event) {
            System.out.println("Resize requested");
          }
        });

        final ContextMenu menu = new ContextMenu(
          resizeItem
        );

        lineChart.setOnMouseClicked(new EventHandler<MouseEvent>() {
          @Override public void handle(MouseEvent event) {
            if (MouseButton.SECONDARY.equals(event.getButton())) {
              menu.show(stage, event.getScreenX(), event.getScreenY());
            }  
          }
        });

        stage.setScene(
          new Scene(
            lineChart,800,600
          )
        );
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}
查看更多
登录 后发表回答