JavaFX 2 XYChart.Series和setOnMouseEntered [英] JavaFX 2 XYChart.Series and setOnMouseEntered

查看:188
本文介绍了JavaFX 2 XYChart.Series和setOnMouseEntered的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将XYChart.Series的实例设置为对setOnMouseEntered进行操作?在我看来,使其工作的一个先决条件是实现EventTarget界面。对于JavaFX XYChart.Series,我想在光标触摸黄线(XYChart.Series的实例)时突出显示以下数据系列:
http://docs.oracle.com/javafx/2.0/charts/img/line-series.png
< br>为了更精确,我想做以下,但是例如XYChart.Series不是Button:

Is it possible to set instance of XYChart.Series to act on setOnMouseEntered? It seems to me that one precondition to make it work would be to implement EventTarget interface. As for JavaFX XYChart.Series I would like to highlight the following data series when cursor touches the yellow line (instance of XYChart.Series):
http://docs.oracle.com/javafx/2.0/charts/img/line-series.png
To be more precise I would like to do the following but for instance of XYChart.Series not Button:

public class Temp {
    /*code removed*/
    Button btn = new Button("Button to touch");
    btn.setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent e) {
            System.out.println("Cursor just touched the button!");
        }
    });
    /*code removed*/
}


推荐答案

查找相应的节点,并添加你想要的事件处理程序。

Lookup the appropriate nodes and add the event handlers you want to them.

这是一个例子。

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.chart.*;
import javafx.scene.effect.Glow;
import javafx.scene.input.MouseEvent;
import javafx.scene.shape.Path;
import javafx.stage.Stage;

public class LineChartSample extends Application {
  @Override public void start(Stage stage) {
    //create the chart
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);
    lineChart.setTitle("Stock Monitoring, 2010");
    XYChart.Series series = new XYChart.Series();
    series.setName("My portfolio");
    series.getData().addAll(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);

    // show the scene.
    Scene scene = new Scene(lineChart, 800, 600);
    stage.setScene(scene);
    stage.show();

    // make the first series in the chart glow when you mouse over it.
    Node n = lineChart.lookup(".chart-series-line.series0");
    if (n != null && n instanceof Path) {
      final Path path = (Path) n;
      final Glow glow = new Glow(.8);
      path.setEffect(null);
      path.setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent e) {
          path.setEffect(glow);
        }
      });
      path.setOnMouseExited(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent e) {
          path.setEffect(null);
        }
      });
    }
  }
  public static void main(String[] args) { launch(args); }
}

这篇关于JavaFX 2 XYChart.Series和setOnMouseEntered的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆