クリックした MenuItem に応じて JPanels を表示する JFrame があります。それは機能しますが、JPanel がフレームに追加されて表示されたら、メソッドを呼び出す必要があります (そのパネル内で JFreeChart を使用していてchartPanel.repaint()
、JPanel が表示されているときに呼び出す必要があるため):
this.getContentPane().add( myjpanel, BorderLayout.CENTER ); //this = JFrame
this.validate();
myjpanel.methodCalledOnceDisplayed();
大丈夫そうですか?myjpanel
表示されているのは本当ですか?そうではないようです:
public void methodCalledOnceDisplayed() {
chartPanel.repaint()
}
これは機能していません ( chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0)
IndexOutOfBoundsException をスローしています)。これは、再描画が呼び出されたときに JPanel が表示されなかったことを意味します。次のことをテストしました。
public void methodCalledOnceDisplayed() {
JOptionPane.showMessageDialog(null,"You should see myjpanel now");
chartPanel.repaint()
}
今では動作し、myjpanel
アラートの背後にあることがわかります。予想どおり、chartPanel が再描画され、例外は発生しません。
EDIT : SSCCE (jfreechart と jcommon が必要: http://www.jfree.org/jfreechart/download.html )
import java.awt.BorderLayout; java.awt.EventQueue をインポートします。 java.awt.Font をインポートします。 import javax.swing.JButton; javax.swing.JFrame をインポートします。 javax.swing.JLabel をインポートします。 import javax.swing.JOptionPane; javax.swing.JPanel をインポートします。 import javax.swing.border.EmptyBorder; org.jfree.chart.ChartMouseEvent をインポートします。 org.jfree.chart.ChartMouseListener をインポートします。 org.jfree.chart.JFreeChart をインポートします。 org.jfree.chart.plot.CombinedDomainXYPlot をインポートします。 org.jfree.chart.plot.PlotOrientation をインポートします。 org.jfree.chart.plot.XYPlot をインポートします。 org.jfree.chart.ChartPanel をインポートします。 org.jfree.data.time.TimeSeries をインポートします。 org.jfree.data.time.TimeSeriesCollection をインポートします。 org.jfree.data.xy.XYDataset をインポートします。 import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Window extends JFrame { プライベート JPanel contentPane; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { 試す { ウィンドウ フレーム = new Window(); frame.setVisible(真); キャッチ(例外e){ e.printStackTrace(); } } }); } 公開ウィンドウ() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 700, 500); contentPane = new JPanel(); contentPane.setBorder(新しい EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(新しい BorderLayout(0, 0)); setContentPane(コンテンツペイン); JButton clickme = new JButton("クリックしてください"); clickme.addActionListener(新しいActionListener() { public void actionPerformed(ActionEvent arg0) { contentPane.removeAll(); MyJPanel mypanel = new MyJPanel(); contentPane.add( mypanel, BorderLayout.CENTER ); 検証(); mypanel.methodCalledOnceDisplayed(); } }); contentPane.add( clickme, BorderLayout.NORTH ); JPanel の例 = new JPanel(); example.add( new JLabel("JPanel の例") ); contentPane.add( 例、BorderLayout.CENTER ); } } class MyJPanel extends JPanel implement ChartMouseListener { プライベート ChartPanel chartPanel; プライベート JFreeChart チャート; プライベート XYPlot subplotTop; プライベート XYPlot subplotBottom; private CombinedDomainXYPlot プロット。 public MyJPanel() { this.add( new JLabel("この JPanel にはグラフが含まれています") ); createCombinedChart(); chartPanel = new ChartPanel(チャート); chartPanel.addChartMouseListener(this); this.add(チャートパネル); } プライベートボイド createCombinedChart() { plot = new CombinedDomainXYPlot(); plot.setGap(30); createSubplots(); plot.add(subplotTop, 4); plot.add(subplotBottom, 1); plot.setOrientation(PlotOrientation.VERTICAL); chart = new JFreeChart("Title", new Font("Arial", Font.BOLD,20), plot, true); } プライベートボイド createSubplots() { subplotTop = new XYPlot(); subplotBottom = new XYPlot(); subplotTop.setDataset(emptyDataset("Empty 1")); subplotBottom.setDataset(emptyDataset("Empty 2")); } プライベート XYDataset emptyDataset(文字列のタイトル) { TimeSeries ts = 新しい TimeSeries(タイトル); TimeSeriesCollection tsc = new TimeSeriesCollection(); tsc.addSeries(ts); tsc を返します。 } @オーバーライド public void chartMouseMoved(ChartMouseEvent e) { System.out.println("マウスが動きました!"); } @オーバーライド public void chartMouseClicked(ChartMouseEvent arg0) {} public void methodCalledOnceDisplayed() { JOptionPane.showMessageDialog(null,"Magic!"); // この行をコメントして、コンソールを表示してみてください chartPanel.repaint(); //これでグラフ領域を取得できます this.chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(0).getDataArea(); this.chartPanel.getChartRenderingInfo().getPlotInfo().getSubplotInfo(1).getDataArea(); } }
JOptionPane を使用した場合と使用しない場合で何が起こるかを確認してください。