positioning an added component (JCheckBox) on a ChartPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
dbritton
Posts: 15
Joined: Fri Jun 06, 2003 12:47 am
Contact:

positioning an added component (JCheckBox) on a ChartPanel

Post by dbritton » Fri Oct 05, 2007 4:52 pm

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.

smasters
Posts: 1
Joined: Wed Oct 24, 2007 10:25 pm

Post by smasters » Thu Oct 25, 2007 4:29 am

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);
    }
}

amaglio.c
Posts: 1
Joined: Fri Jun 27, 2008 3:29 pm

Re: positioning an added component (JCheckBox) on a ChartPan

Post by amaglio.c » Mon Jun 30, 2008 1:51 pm

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

Locked