Horrerblade
Horrerblade

Reputation: 168

JavaFX comboBox wont let me chose another option after i select one

Hey guys I am trying to get this combobox working, but after I select an option it changes to a graph (Like i want it to) but it wont let me chose another graph option. Does anyone know how to select another graph? Here is my code:

public class Grapher extends JApplet {

    private static final int JFXPANEL_WIDTH_INT = 800;
    private static final int JFXPANEL_HEIGHT_INT = 800;
    private static JFXPanel fxContainer;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception e) {
                }

                JFrame frame = new JFrame("Graph Viewer");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                JApplet applet = new Grapher();
                applet.init();

                frame.setContentPane(applet.getContentPane());

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                applet.start();
            }
        });
    }

    @Override
    public void init() {
        fxContainer = new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(JFXPANEL_WIDTH_INT, JFXPANEL_HEIGHT_INT));
        add(fxContainer, BorderLayout.CENTER);
        // create JavaFX scene
        Platform.runLater(new Runnable() {

            @Override
            public void run() {
                createScene();
            }
        });
    }

    private void createScene() {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25,25,25,50));

        final  Label gType = new Label ("Type of Graph: ");

        Scene scene = new Scene(grid, 1000, 800);
        fxContainer.setScene(scene);
        //Multi select Combo box Code
        ObservableList<String> options = FXCollections.observableArrayList(
            "OSNR_Eff (dB) vs Frequency",
            "OSNR (dB) vs Frequency",
            "Non-Linear OSNR Penalty (dB) vs Frequency",        
            "Tx Pwr (dBm) vs Frequency",
            "BOL/EOL Span Loss vs. Frequency",
            "Chromatic Dispersion (ps/nm) vs. Frequency",
            "EOL Worst-Channel OSNR_Eff Per Span",
            "Tx Pwr, Opt Tx Pwr, & P_max vs Frequency Per Span",
            "EOL Final OSNR_Eff (dB) vs Frequency",  
            "Tx Pwr vs. Opt Tx Pwr Config 0",
            "Tx Pwr vs. Opt Tx Pwr Config 1",
            "Tx Pwr vs. Opt Tx Pwr Config 2",
            "Tx Pwr vs. Opt Tx Pwr Config 3",
            "Tx Pwr vs. Opt Tx Pwr Config 4",
            "Tx Pwr vs. Opt Tx Pwr Config 5",
            "Tx Pwr vs. Opt Tx Pwr Config 6",
            "Tx Pwr vs. Opt Tx Pwr Config 7"         
        );
        final ComboBox comboBox = new ComboBox(options);

        comboBox.setVisibleRowCount(4);
        comboBox.setValue(null);
        //Event Handeling for the ComboBox


        comboBox.setOnAction((event) -> {
            //Graph Code

            //set x and y axis
            final NumberAxis xAxis = new NumberAxis(); 
            final NumberAxis yAxis = new NumberAxis();

            //Get x/y axis labels
            String xLabel;
            String yLabel;
            String gTitle = (String) comboBox.getValue();

            //save x/y axis label
            if (gTitle.contains("Tx Pwr vs. Opt Tx Pwr Config" )){
               xLabel = "Frequency (THz)";
               yLabel = "Tx Pwr (dBm)";
            }
            else if (gTitle.contains("OSNR_Eff (dB) vs Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "OSNR_Eff (dB)";
            }
            else if (gTitle.contains("OSNR (dB) vs Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "OSNR (dB)";
            }
            else if (gTitle.contains("Non-Linear OSNR Penalty (dB) vs Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "Non-Linear Penalty (dB)";
            }
            else if (gTitle.contains("Tx Pwr (dBm) vs Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "Tx Pwr (dBm)";
            }
            else if (gTitle.contains("BOL/EOL Span Loss vs. Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "BOL/EOL Span Loss (dB)";
            }
            else if (gTitle.contains("Chromatic Dispersion (ps/nm) vs. Frequency")){
               xLabel = "Frequency (THz)";
               yLabel = "Chromatic Dispersion (ps/nm)";
            }
            else if (gTitle.contains("EOL Worst-Channel OSNR_Eff Per Span")){
               xLabel = "Span Index";
               yLabel = "OSNR_Eff (dB)";
            }
            else if (gTitle.contains("Tx Pwr, Opt Tx Pwr, & P_max vs Frequency Per Span")){
               xLabel = "Span Index";
               yLabel = "Pwr (dBm)";
            }
            else{ // for "EOL Final OSNR_Eff (dB) vs Frequency" graph
                xLabel = "Frequency (THz)";
                yLabel = "OSNR_Eff (dB)";
            }

            //Set x/y Axis Label
            xAxis.setLabel(xLabel);
            yAxis.setLabel(yLabel);

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

            //defining a series
            XYChart.Series series = new XYChart.Series();
            series.setName("My portfolio");
            //populating the series with data
            series.getData().add(new XYChart.Data(1, 23));
            series.getData().add(new XYChart.Data(2, 14));
            series.getData().add(new XYChart.Data(3, 15));
            series.getData().add(new XYChart.Data(4, 24));
            series.getData().add(new XYChart.Data(5, 34));
            series.getData().add(new XYChart.Data(6, 36));
//            series.getData().add(new XYChart.Data(7, 22));
//            series.getData().add(new XYChart.Data(8, 45));
//            series.getData().add(new XYChart.Data(9, 43));
//            series.getData().add(new XYChart.Data(10, 17));
//            series.getData().add(new XYChart.Data(11, 29));
//            series.getData().add(new XYChart.Data(12, 25));
            Scene scene1 = new Scene(lineChart,800,600);
            lineChart.getData().add(series);
            fxContainer.setScene(scene1);

        });

        grid.add(gType, 0,0,1,1); //Adds label
        grid.add(comboBox, 1, 0); //Adds ComboBox


        fxContainer.show();


    }

}

Upvotes: 0

Views: 67

Answers (1)

RickyNevada
RickyNevada

Reputation: 68

The problem is that you're creating a new scene, adding the chart to it, and then setting the JFXPanel's scene to that one, so the existing scene with the ComboBox is getting overwritten.

Basically, you'll want to add the linechart to the same scene that the combobox is using. You can add the linechart to the GridPane. Here's an example, you can mess around with the layout.

......
//Set x/y Axis Label
xAxis.setLabel(xLabel);
yAxis.setLabel(yLabel);
//THIS CHECKS TO SEE IF A CHART IS ADDED AND REMOVES IT TO PREVENT
//DUPLICATING THE CHART
if(grid.getChildren().size() == 3){
    grid.getChildren().remove(2);

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

//defining a series
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
//populating the series with data
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
grid.getChildren().add(lineChart);  //Add linechart to grid
lineChart.getData().add(series);


});

Upvotes: 1

Related Questions