JavaFX在折线图上添加上下文菜单

时间:2013-05-06 15:30:20

标签: javafx-2

我想在右线点击我的折线图时添加一个上下文菜单。 它基本上用于重新调整图表的大小。我有什么办法可以实现这个目标吗?

1 个答案:

答案 0 :(得分:4)

这是一个代码片段,用于在右键单击折线图时在折线图上显示调整大小的上下文菜单。

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());
    }  
  }
});

一些完整的示例代码:

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); }
}