Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default How to extend horizontal line on subpane(s)?

    This code plots two XYChart lines in a Splitpane, and draw an horizontal line only in the upper pane.

    How to extend horizontal line on subpane(s)?-img.png

    I would like to have this horizontal line also in lower subpane(s) when mouse pointer is moved to the lower pane.

    How to accomplish this?

    Thanks

    Here is the code

    Java FX Code:
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.collections.ObservableList;
    import javafx.event.EventHandler;
    import javafx.geometry.Orientation;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.chart.*;
    import javafx.scene.control.SplitPane;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Line;
    import javafx.scene.shape.LineBuilder;
    import javafx.stage.Stage;
    
    
    public class XyChartHorizontalLine extends Application {
    
    SplitPane                splitPane1 = null; 
    BorderPane              pane1; 
    BorderPane              pane2; 
    Line                        LH;
    XYChart.Series         series1 = new XYChart.Series(); 
    XYChart.Series         series2 = new XYChart.Series(); 
    
    @Override
    public void start(Stage stage) {      
    
    stage.setTitle("Lines plot"); 
    
    final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
    final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 
    
    xAxis.setAnimated(false);
    xAxis.setScaleX(0);
    yAxis.setAnimated(false);
    
    yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { 
    
    @Override 
    public String toString(Number object) { 
        return String.format("%7.5f", object); 
       } 
    }); 
    
    final LineChart<Number, Number> lineChart1 = new LineChart<Number, Number>(xAxis,  yAxis); 
    
    lineChart1.setCreateSymbols(false); 
    lineChart1.setAlternativeRowFillVisible(false); 
    lineChart1.setAnimated(false); 
    lineChart1.setLegendVisible(false);
    
    series1.getData().add(new XYChart.Data(1, 0.53185)); 
    series1.getData().add(new XYChart.Data(2, 0.532235)); 
    series1.getData().add(new XYChart.Data(3, 0.53234)); 
    series1.getData().add(new XYChart.Data(4, 0.538765)); 
    series1.getData().add(new XYChart.Data(5, 0.53442)); 
    series1.getData().add(new XYChart.Data(6, 0.534658)); 
    series1.getData().add(new XYChart.Data(7, 0.53023)); 
    series1.getData().add(new XYChart.Data(8, 0.53001)); 
    series1.getData().add(new XYChart.Data(9, 0.53589)); 
    series1.getData().add(new XYChart.Data(10, 0.53476)); 
    series1.getData().add(new XYChart.Data(11, 0.530123)); 
    series1.getData().add(new XYChart.Data(12, 0.531035)); 
    
    lineChart1.getData().addAll(series1);         
    
    pane1 = new BorderPane(); 
    pane1.setCenter(lineChart1); 
    
    splitPane1 = new SplitPane();                                
    splitPane1.setOrientation(Orientation.VERTICAL);
    splitPane1.getItems().addAll(pane1);
    splitPane1.setDividerPosition(0, 1);
    
    final NumberAxis xAxis2 = new NumberAxis(1, 12, 1);
    final NumberAxis yAxis2 = new NumberAxis();
    
    yAxis2.setTickUnit(1);
    yAxis2.setPrefWidth(35);
    yAxis2.setMinorTickCount(10);
    
    yAxis2.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis2){
        @Override
        public String toString(Number object){
            String label;
            label = String.format("%7.2f", object.floatValue());
            return label;
        }
    });
    
    LineChart<Number, Number>lineChart2 = new LineChart<Number, Number>(xAxis2, yAxis2);
    
    lineChart2.setAlternativeRowFillVisible(false);
    lineChart2.setLegendVisible(false);
    lineChart2.setAnimated(false);
    lineChart2.setCreateSymbols(false);
    
    //XYChart.Series series2 = new XYChart.Series();
    
    series2.getData().add(new XYChart.Data(1, 1));
    series2.getData().add(new XYChart.Data(2, 3));
    series2.getData().add(new XYChart.Data(3, 1.5));
    series2.getData().add(new XYChart.Data(4, 3));
    series2.getData().add(new XYChart.Data(5, 4.5));
    series2.getData().add(new XYChart.Data(6, 5));
    series2.getData().add(new XYChart.Data(7, 4));
    series2.getData().add(new XYChart.Data(8, 8));
    series2.getData().add(new XYChart.Data(9, 16.5));
    series2.getData().add(new XYChart.Data(10, 13.9));
    series2.getData().add(new XYChart.Data(11, 17));
    series2.getData().add(new XYChart.Data(12, 20));
    
    lineChart2.getData().addAll(series2);
    
    pane2 = new BorderPane(); 
    pane2.setCenter(lineChart2);
    
    Platform.runLater(new Runnable() {
     @Override
     public void run() {
         double percSplit;
    
         splitPane1.getItems().addAll(pane2);
    
        ObservableList<SplitPane.Divider> splitDiv =  splitPane1.getDividers();
    
        percSplit = 1/(double)(splitDiv.size()+1);
        
        for (int i = 0; i< splitDiv.size(); i++) {                        
            splitPane1.setDividerPosition(i, percSplit);
            percSplit += 1/(double)(splitDiv.size()+1);
            }
         }
     });
    
    Scene scene = new Scene(splitPane1, 800, 600); 
    stage.setScene(scene);         
    
    pane1.setOnMouseMoved(mouseHandler); 
    
    LH=LineBuilder.create()
        .startX(0)
        .startY(0)
        .endX(10)
        .endY(.535)
        .strokeWidth(1)
        .stroke(Color.BLACK)
        .build();
     
    pane1.getChildren().add(LH);
    
    stage.show();
    } 
    
    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 
    
    @Override 
    public void handle(MouseEvent mouseEvent) {
    
    if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { 
        LineChart<Number, Number> lineChart = (LineChart<Number, Number>)     pane1.getCenter(); 
    
        NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); 
        NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); 
    
        double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); 
    
        double xAxisShift = getSceneShift(xAxis);
        double yAxisShift = getSceneShift(yAxis);
    
        if(mouseEvent.getX()>xAxisShift && mouseEvent.getX()<xAxisShift+xAxis.getWidth()){
    
        LH.setStartX(xAxisShift);
        LH.setStartY(mouseEvent.getY());
        LH.setEndX(xAxisShift+xAxis.getWidth());
        LH.setEndY(mouseEvent.getY());
        }
      } 
    }
    }; 
    
    private static double getSceneShift(Node node) { 
    double shift = 0; 
    
    do {  
      shift += node.getLayoutX();  
      node = node.getParent(); 
    } 
    while (node != null); 
      return shift; 
    }   
    
    
    public static void main(String[] args) { 
    launch(args);  
      } 
    }
    Last edited by susieferrari; 09-27-2012 at 04:16 PM. Reason: Code indenting and added vertical whitespace for better readability

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: How to extend horizontal line on subpane(s)?

    Inconsistent indenting and (lack of) vertical whitespace makes that code difficult to read.
    Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: How to extend horizontal line on subpane(s)?

    I have been able to find the solution, question is now solved.

Similar Threads

  1. Replies: 3
    Last Post: 01-10-2012, 10:33 AM
  2. Help with horizontal output
    By IdrinkJava in forum New To Java
    Replies: 6
    Last Post: 01-28-2011, 05:00 PM
  3. Print Horizontal Bars
    By xstrandedx7688 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 04:52 AM
  4. JFileChooser horizontal scrollbar problem
    By nenadm in forum AWT / Swing
    Replies: 4
    Last Post: 11-12-2008, 11:16 AM
  5. Replies: 0
    Last Post: 07-02-2008, 08:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •