I just found that when i add a ChartPanel to a JFrame, the ChartPanel will resize to fit the frame as the JFrame dragged larger or smaller. However, when i add the ChartPanel to a JPanel, the JPanel's size( width and height ) changes to fit the JFrame as the JFrame resized. But the ChartPanel just keeps it's dimensions, leaving the enlarged JPanel a lot of empty space. How to make ChartPanel resize with it's container in GUI?
Here is my sample codes :
- This case the chart resize with the JFrame
ChartPanel chartPanel = new ChartPanel(createWhateverChart()); JFrame frame = new JFrame(); frame.add(chartPanel);
- In the case, i add the ChartPanel to JPanel first, for ease of management and update the chart but the ChartPanel doesn't resize with the JPanel/JFrame.
ChartPanel chartPanel = new ChartPanel(createWhateverChart()); JPanel panel = new JPanel("Who cares"); panel.add(chartPanel, BorderLayout.CENTER); JFrame frame = new JFrame(); frame.add(panel);