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

    Default SplitPanes, how to move panes together?

    This code plots two charts in a splitpane, and by left mouse click and drag the upper line chart moves left/right (and up/down).

    I would like to move both panes together left/right when I click and drag with left mouse on the upper graph: how to accomplish this?

    Thanks

    Susie

    Java FX Code:
    public class XyChartInSplitMove extends Application { 
    SplitPane               splitPane1 = null; 
    BorderPane              pane; 
    BorderPane              pane2; 
    XYChart.Series          series1 = new XYChart.Series(); 
    XYChart.Series          series2 = new XYChart.Series(); 
    
    SimpleDoubleProperty rectinitX = new SimpleDoubleProperty(); 
    SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();  
    
    @Override 
    public void start(Stage stage) { 
    
    final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
    final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 
    
    xAxis.setAnimated(false);
    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); 
    
    pane = new BorderPane(); 
    pane.setCenter(lineChart1); 
    
    splitPane1 = new SplitPane();                                
    splitPane1.setOrientation(Orientation.VERTICAL);
    splitPane1.getItems().addAll(pane);
    splitPane1.setDividerPosition(0, 1);
    
    Platform.runLater(new Runnable() {
         @Override
         public void run() {
             double percSplit;
    
            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);
            }
         }
     });
    //BarChart
    final CategoryAxis xAxis2 = new CategoryAxis();
    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;
    }
    });
    final BarChart<String, Number>BarChart2 = new BarChart<String, Number>(xAxis2, yAxis2);
    
    BarChart2.setAlternativeRowFillVisible(false);
    BarChart2.setLegendVisible(false);
    BarChart2.setAnimated(false);
    
    XYChart.Series series2 = new XYChart.Series();
    
    series2.getData().add(new XYChart.Data("Jan", 1));
    series2.getData().add(new XYChart.Data("Feb", 3));
    series2.getData().add(new XYChart.Data("Mar", 1.5));
    series2.getData().add(new XYChart.Data("Apr", 3));
    series2.getData().add(new XYChart.Data("May", 4.5));
    series2.getData().add(new XYChart.Data("Jun", 5));
    series2.getData().add(new XYChart.Data("Jul", 4));
    series2.getData().add(new XYChart.Data("Aug", 8));
    series2.getData().add(new XYChart.Data("Sep", 16.5));
    series2.getData().add(new XYChart.Data("Oct", 13.9));
    series2.getData().add(new XYChart.Data("Nov", 17));
    series2.getData().add(new XYChart.Data("Dec", 20));
    
    BarChart2.getData().addAll(series2);
    
    Platform.runLater(new Runnable() {
         @Override
         public void run() {
             double percSplit;
             splitPane1.getItems().addAll(BarChart2);
    
            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);         
    
    pane.setOnMouseClicked(mouseHandler); 
    pane.setOnMouseDragged(mouseHandler); 
    pane.setOnMouseEntered(mouseHandler); 
    pane.setOnMouseExited(mouseHandler); 
    pane.setOnMouseMoved(mouseHandler);  
    pane.setOnMouseReleased(mouseHandler); 
    
    stage.show(); 
    } 
    
    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>(){ 
    
    @Override 
    public void handle(MouseEvent mouseEvent) { 
    
        if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED ||    mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED){ 
            LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane.getCenter(); 
    
            NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); 
            NumberAxis xAxis = (NumberAxis) lineChart.getXAxis();  
            double Tgap = xAxis.getWidth()/(xAxis.getUpperBound() - xAxis.getLowerBound()); 
            double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); 
            double newYlower=yAxis.getLowerBound(), newYupper=yAxis.getUpperBound(); 
    
            double xAxisShift = getSceneShift(xAxis);
            double yAxisShift = getSceneShift(yAxis);
    
            double yAxisStep=yAxis.getHeight()/(yAxis.getUpperBound()-yAxis.getLowerBound());
            double CurrentPrice=yAxis.getUpperBound()-((mouseEvent.getY()-yAxisShift)/yAxisStep);
    
            double Delta=0.3;
    
            if(mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED){
                if(rectinitX.get() < mouseEvent.getX()){    
                newXlower=xAxis.getLowerBound()-Delta;
                newXupper=xAxis.getUpperBound()-Delta;
            }
            else if(rectinitX.get() > mouseEvent.getX()){    
                newXlower=xAxis.getLowerBound()+Delta;
                newXupper=xAxis.getUpperBound()+Delta;
            }    
            xAxis.setLowerBound( newXlower ); 
            xAxis.setUpperBound( newXupper ); 
    
            //========== Y-Axis Moving ============================
    
            if(rectinitY.get() < mouseEvent.getY()){    
                newYlower=yAxis.getLowerBound()+Delta/1000;
                newYupper=yAxis.getUpperBound()+Delta/1000;
            }
            else if(rectinitY.get() > mouseEvent.getY()){    
                newYlower=yAxis.getLowerBound()-Delta/1000;
                newYupper=yAxis.getUpperBound()-Delta/1000;
            }
            yAxis.setLowerBound(newYlower);
            yAxis.setUpperBound(newYupper);
            }
            rectinitX.set(mouseEvent.getX()); 
            rectinitY.set(mouseEvent.getY()); 
    
            if(mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED && mouseEvent.getY()>yAxisShift && mouseEvent.getY()<yAxisShift+yAxis.getHeight() && mouseEvent.getX()>xAxisShift && mouseEvent.getX()<xAxisShift+xAxis.getWidth()){
    
            double XX=((mouseEvent.getX() - xAxisShift) / Tgap) + xAxis.getLowerBound();
            double YY=CurrentPrice;
    
            int XLB=(int) xAxis.getLowerBound();
            int XUB=(int) xAxis.getUpperBound();
            }
         } 
    } 
    }; 
    private static double getSceneShift(Node node) { 
    double shift = 0; 
    do {  
        shift += node.getLayoutX();  
        node = node.getParent(); 
    } while (node != null); 
    return shift; 
    }
    private static String getHIstLOstY(XYChart.Series S,int XLowerBound,int XUpperBound) { 
    double ValLOst=1000000;
    double ValHIst=-1000000;
    for(int i=XLowerBound; i<XUpperBound; i++){  
        double P=GetPrice(S,i);
        if(ValHIst<P){
            ValHIst=P;
        }
        if(ValLOst>P){
            ValLOst=P;
        }
    }
    return Double.toString(ValLOst) + "," + Double.toString(ValHIst);
    } 
    private static double GetPrice(XYChart.Series S,int IX) { 
    Object SVal=S.getData().get(IX);
    String Temp=SVal.toString().replaceAll("Data", "");
    Temp=Temp.replace("[", "");
    Temp=Temp.replace("]", "");
    String[] TempArray=Temp.split(",");
    return Double.parseDouble(TempArray[1]);     
    }   
    public static void main(String[] args) { 
    launch(args);  
    } 
    }

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

    Default Re: SplitPanes, how to move panes together?

    Notes:
    When posting code on a forum, do the members the courtesy of removing all unused code. IOW, post a SSCCE.
    In the same vein, about 3 values in a Series are enough to demonstrate this issue, and could reduce the length of this code by 18 lines at least.
    Include imports, whether specific or on-demand. Class names can be ambiguous across the JDK.
    Follow coding conventions. Variable names start with a lowercase letter, and indents should be consistent throughout.

    This fulfills most of your requirement. You need to dig deeper and find a way to span the bar chart's x-axis line more correctly.
    Java FX Code:
    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.beans.property.SimpleDoubleProperty;
    import javafx.collections.ObservableList;
    import javafx.event.EventHandler;
    import javafx.geometry.Orientation;
    import javafx.scene.Group;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.chart.BarChart;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.Chart;
    import javafx.scene.chart.LineChart;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.scene.control.SplitPane;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    public class XYChartInSplitMove extends Application {
    
      SplitPane splitPane1 = null;
      BorderPane pane;
      BorderPane pane2;
      XYChart.Series series1 = new XYChart.Series();
      XYChart.Series series2 = new XYChart.Series();
      SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
      SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
    
      @Override
      public void start(Stage stage) {
    
        final NumberAxis xAxis = new NumberAxis(1, 12, 1);
        final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005);
    
        xAxis.setAnimated(false);
        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);
    
        pane = new BorderPane();
        pane.setCenter(lineChart1);
    
        splitPane1 = new SplitPane();
        splitPane1.setOrientation(Orientation.VERTICAL);
        splitPane1.getItems().addAll(pane);
        splitPane1.setDividerPosition(0, 1);
    
        Platform.runLater(new Runnable() {
    
          @Override
          public void run() {
            double percSplit;
    
            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);
            }
          }
        });
        //BarChart
        final CategoryAxis xAxis2 = new CategoryAxis();
        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;
          }
        });
        final BarChart<String, Number> barChart2 = new BarChart<String, Number>(xAxis2, yAxis2);
    
        barChart2.setAlternativeRowFillVisible(false);
        barChart2.setLegendVisible(false);
        barChart2.setAnimated(false);
    
        series2.getData().add(new XYChart.Data("Jan", 1));
        series2.getData().add(new XYChart.Data("Feb", 3));
        series2.getData().add(new XYChart.Data("Mar", 1.5));
        series2.getData().add(new XYChart.Data("Apr", 3));
        series2.getData().add(new XYChart.Data("May", 4.5));
        series2.getData().add(new XYChart.Data("Jun", 5));
        series2.getData().add(new XYChart.Data("Jul", 4));
        series2.getData().add(new XYChart.Data("Aug", 8));
        series2.getData().add(new XYChart.Data("Sep", 16.5));
        series2.getData().add(new XYChart.Data("Oct", 13.9));
        series2.getData().add(new XYChart.Data("Nov", 17));
        series2.getData().add(new XYChart.Data("Dec", 20));
    
        barChart2.getData().addAll(series2);
        pane2 = new BorderPane();
        pane2.setCenter(barChart2);
    
        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);
    
        pane.setOnMouseClicked(mouseHandler);
        pane.setOnMouseDragged(mouseHandler);
        pane.setOnMouseEntered(mouseHandler);
        pane.setOnMouseExited(mouseHandler);
        pane.setOnMouseMoved(mouseHandler);
        pane.setOnMouseReleased(mouseHandler);
    
        stage.show();
      }
      EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
    
        @Override
        public void handle(MouseEvent mouseEvent) {
    
          if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED || mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) {
            LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane.getCenter();
    
            NumberAxis yAxis = (NumberAxis) lineChart.getYAxis();
            NumberAxis xAxis = (NumberAxis) lineChart.getXAxis();
            double newXlower = xAxis.getLowerBound(), newXupper = xAxis.getUpperBound();
            double newYlower = yAxis.getLowerBound(), newYupper = yAxis.getUpperBound();
            double delta = 0.3;
    
            if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
              if (rectinitX.get() < mouseEvent.getX()) {
                newXlower = xAxis.getLowerBound() - delta;
                newXupper = xAxis.getUpperBound() - delta;
              } else if (rectinitX.get() > mouseEvent.getX()) {
                newXlower = xAxis.getLowerBound() + delta;
                newXupper = xAxis.getUpperBound() + delta;
              }
              xAxis.setLowerBound(newXlower);
              xAxis.setUpperBound(newXupper);
    
              // Y-Axis Moving
    
              if (rectinitY.get() < mouseEvent.getY()) {
                newYlower = yAxis.getLowerBound() + delta / 1000;
                newYupper = yAxis.getUpperBound() + delta / 1000;
              } else if (rectinitY.get() > mouseEvent.getY()) {
                newYlower = yAxis.getLowerBound() - delta / 1000;
                newYupper = yAxis.getUpperBound() - delta / 1000;
              }
              yAxis.setLowerBound(newYlower);
              yAxis.setUpperBound(newYupper);
            }
            rectinitX.set(mouseEvent.getX());
            rectinitY.set(mouseEvent.getY());
    
            BarChart<String, Number> barChart2 = (BarChart<String, Number>) pane2.getCenter();
            double chartWidth = xAxis.getWidth();
            double axisSpan = xAxis.getUpperBound() - xAxis.getLowerBound();
            double displacement = (chartWidth / axisSpan) * (1 - newXlower);
    
            for (Node node : barChart2.getChildrenUnmodifiable()) {
              if (node.getClass().getEnclosingClass() == Chart.class) {
                for (Node node2 : ((Parent) node).getChildrenUnmodifiable()) {
                  if ((node2 == barChart2.getXAxis())) {
                    node2.translateXProperty().set(displacement);
                  }
                  if (node2.getClass().getEnclosingClass() == XYChart.class) {
                    for (Node node3 : ((Parent) node2).getChildrenUnmodifiable()) {
                      if (node3.getClass() == Group.class) {
                        node3.translateXProperty().set(displacement);
                      }
                    }
                  }
                }
              }
            }
          }
        }
      };
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    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: SplitPanes, how to move panes together?

    Thank you very much Darryl Burke,

    your help really appreciated.

    Susie

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

    Default Re: SplitPanes, how to move panes together?

    Sorry, I meant to mention this but forgot -- the code I posted is extremely dependent on the internal implementation of BarChart, and could very possibly break in a future release of JavaFX.

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

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

    Default Re: SplitPanes, how to move panes together?

    Hi DarrylBurke,

    good to know this, thank you.

    Susie


    Quote Originally Posted by DarrylBurke View Post
    Sorry, I meant to mention this but forgot -- the code I posted is extremely dependent on the internal implementation of BarChart, and could very possibly break in a future release of JavaFX.

    db

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

    Default Re: SplitPanes, how to move panes together?

    Hi Darryl Burke,

    I am using your displacement for X axis

    Java FX Code:
    double chartWidthX = xAxis.getWidth();
    double axisSpanX = xAxis.getUpperBound() - xAxis.getLowerBound();
    double displacementX = (chartWidthX / axisSpanX) * ( -xAxis.getLowerBound())
    and it works perfectly.

    I would like to use displacement for Y axis, but it doesn't work, here is my attempt:

    Java FX Code:
    double chartWidthY = yAxis.getWidth();
    double axisSpanY = yAxis.getUpperBound() - yAxis.getLowerBound();                               
    double displacementY = (yAxis.getDisplayPosition(yAxis.getLowerBound()))-((chartWidthY / axisSpanY)*(yAxis.getUpperBound()));
    I am sure I am wrong somewhere, can you help me?

    Thanks

    Susie
    Last edited by susieferrari; 10-19-2012 at 09:09 PM.

Similar Threads

  1. JTab Panes
    By Nidi in forum New To Java
    Replies: 2
    Last Post: 03-02-2011, 05:27 AM
  2. Layered Panes
    By teckno101 in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2009, 12:16 AM
  3. panes, frames and ddm's :|
    By skatefreak in forum New To Java
    Replies: 1
    Last Post: 08-09-2009, 11:21 AM
  4. working with splitpanes
    By masa in forum AWT / Swing
    Replies: 3
    Last Post: 12-19-2008, 07:13 PM
  5. Two content panes in one GUI?
    By Leprechaun in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 05:02 AM

Posting Permissions

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