简体   繁体   中英

How to remove the panel from the panel it attached to in Java Swing?

I have met a serious problem with my Java swing.

This is how I initialize my chart, everything seems fine now, xyChartPanel is declared as a JPanel in the field, I initialize it with the xyChart I just created. When this step is done, I am okay to see the chart (painted to xyChartPanel ) centered to the JPanel I am writing code on, see add(xyChartPanel, BorderLayout.CENTER); .

private void initXYChart() {
        // Create Chart
        xyChart = new XYChartBuilder().width(800).height(800).xAxisTitle(xColName).yAxisTitle("Y").build();

        // Customize Chart
        xyChart.getStyler().setLegendPosition(LegendPosition.InsideNE);
        xyChart.getStyler().setAxisTitlesVisible(true);
        xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);

        double[] yCoordArray = new double[xCoordArray.length];

        // Loop through the series
        for (int i = 0; i < yCoordinates.size(); i++) {
            List<Double> yCoordOneSeries = yCoordinates.get(i);
            // Convert list to array
            for (int j = 0; j < yCoordArray.length; j++) {
                yCoordArray[j] = yCoordOneSeries.get(j);
            }
            xyChart.addSeries(yColNames.get(i), xCoordArray, yCoordArray);
        }

        xyChartPanel = new XChartPanel<>(xyChart);
        add(xyChartPanel, BorderLayout.CENTER);

        xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);

        add(xyChartPanel, BorderLayout.CENTER);
    }

Now the problem comes, I don't want my chart to be unchanged all the time, actually I want to change the style of my chart responded to my action on the radio buttons.

I just wrote the updateChartPanelStyle(JRadioButton styleButton) method that takes

private void updateChartPanelStyle(JRadioButton styleButton) {
        String style = styleButton.getText();
        if (styleButton.isSelected()) {
            System.out.println(style);
            switch (style) {
            case "Line":
                xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
                break;
            case "Area":
                xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Area);
                break;
            case "Scatter":
                xyChart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Scatter);
            }

            xyChartPanel = new XChartPanel<>(xyChart);
            add(xyChartPanel, BorderLayout.CENTER);
        }
    }

See in this method, I changed the style of xyChart I initialized in the last function, and reinitialize the xyChartPanel , then add the updated xyChartPanel to the working panel. Interestingly, I didn't see any change in my GUI. I thought this might be a problem with my xyChart whose style could not be changed afterward. But this is not really the case.

Even if I "removed" xyChartPanel with this.remove(xyChartPanel); , the GUI doesn't seems to be changed.

This is really weird, what should I do now?

Every time you add/remove components to swing dynamically, you need to call revalidate(); and then repaint(); on your JPanel (or JFrame if you're adding it straight to that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM