JPanel added but not displayed “in time”

前端 未结 3 981
囚心锁ツ
囚心锁ツ 2020-12-02 02:17

I have a JFrame that displays JPanels depending on the MenuItem you click. It works, but now I need to call a method once a JPanel is added to the frame and it is being show

3条回答
  •  一生所求
    2020-12-02 02:56

    It seems it is solved thanks to invokeLater:

    public void methodCalledOnceDisplayed() {
            SwingUtilities.invokeLater( new Runnable() {
                @Override
                public void run() {
                    chartPanel.repaint();
                    chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea();
                    chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(1).getDataArea();
                }
            });
        }

    Now there is no IndexOutOfBoundsException: Index: 0, Size: 0

    An explanation of why is this happening would be great

提交回复
热议问题