positioning an added component (JCheckBox) on a ChartPanel
positioning an added component (JCheckBox) on a ChartPanel
I am adding a checkbox to a ChartPanel. It gets added but it is positioned over the title of the chart. I see tha the ChartPanel is using a flowlayout. I've tried adding the component to the layout to no avail. Any hints to positiion the checkbox (upper right) would be appreciated.
I solved this problem by extending ChartPanel then overriding its paint(Graphic) method, moving the combo box to the lower left position.
Code: Select all
public class MyChartPanel extends ChartPanel
{
private JComboBox _chartComponent;
public MyChartPanel(JFreeChart chart)
{
super(chart);
addChartComponent();
}
public MyChartPanel(JFreeChart chart, boolean useBuffer)
{
super(chart, useBuffer);
addChartComponent();
}
public MyChartPanel(JFreeChart chart, boolean properties, boolean save,
boolean print, boolean zoom, boolean tooltips)
{
super(chart, properties, save, print, zoom, tooltips);
addChartComponent();
}
public MyChartPanel(JFreeChart chart, int width, int height,
int minimumDrawWidth, int minimumDrawHeight,
int maximumDrawWidth, int maximumDrawHeight,
boolean useBuffer, boolean properties, boolean save,
boolean print, boolean zoom, boolean tooltips)
{
super(chart, width, height, minimumDrawWidth, minimumDrawHeight, maximumDrawWidth,
maximumDrawHeight, useBuffer, properties, save, print, zoom, tooltips);
addChartComponent();
}
private void addChartComponent()
{
_chartComponent = new JComboBox(
new Object[] {"Cost vs Fill Rate", "Red Sox vs Rockies"} );
_chartComponent.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
add(_chartComponent);
}
public void paint(Graphics g)
{
int chartHeight = this.getHeight();
// Move the component to the bottom/left portion of the graph
java.awt.Rectangle bounds = _chartComponent.getBounds();
bounds.x = 5;
bounds.y = chartHeight - bounds.height - 5;
_chartComponent.setBounds(bounds);
super.paint(g);
}
}
Re: positioning an added component (JCheckBox) on a ChartPan
I need a similar thing: adding a Button on a ChartPanel. My problem is how to know the bounds of the real graph area (excluding domain/range axis and so) in order to put the button inside the graph.
Thank for the answers.
Carlo
Thank for the answers.
Carlo