Java Programming - Can't make the chart visible!

Hi everyone ,

I 've been trying to understand JFreeChart and i have an applet with user interface and in the applet i have a JPanel name panel1... I init the dataset and in a function like DrawChart i called the dataset and try to draw the chart , like :

publicvoid DrawChart(){

initDataSet();

// create the chart...

JFreeChart chart = ChartFactory.createBarChart(

"Bar Chart",// chart title

"Category",// domain axis label

"Value",// range axis label

dataset,// data

PlotOrientation.VERTICAL,// orientation

true,// include legend

true,// tooltips?

false// URLs?

);

chartPanel =new ChartPanel(chart);

chartPanel.setPreferredSize(new Dimension(panel1.getHeight(),panel1.getWidth()));

panel1.add(chartPanel);

JOptionPane.showMessageDialog( null,""+panel1.getComponentCount());

panel1.validate();

panel1.repaint();

}

and in my initComponents() function i call the DrawChart and as having an appleti don't have main andi don't have frame- instead i use jpanel- ... Anything i've done i couldn't make the chart visible...

Can anyone help me?

Can't we use jpanel to contain jfreechart?

[1971 byte] By [janniserya] at [2007-11-26 23:30:27]
# 1
Maybe you should add that panel to your applet at some point...
CeciNEstPasUnProgrammeura at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...
# 2

of course i did it. I have a initComponents method which add panels , labels on applet. Like :

private void initComponents() {

title = new JLabel();

closePanel = new JButton();

panel1 = new JPanel();

//======== this ========

setLayout(null);

//- title -

title.setText("screen - 1");

title.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));

title.setHorizontalAlignment(SwingConstants.CENTER);

title.setHorizontalTextPosition(SwingConstants.CENTER);

title.setFont(new Font("Arial", Font.BOLD, 16));

title.setForeground(new Color(0, 0, 153));

add(title);

title.setBounds(15, 45, 720, 35);

//- closePanel -

closePanel.setIcon(UIManager.getIcon("InternalFrame.closeIcon"));

closePanel.setPreferredSize(new Dimension(16, 16));

closePanel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

closePanelActionPerformed(e);

}

});

add(closePanel);

closePanel.setBounds(720, 0, 16, 16);

//======== panel1 ========

{

panel1.setBorder(UIManager.getBorder("InternalFrame.optionDialogBorder"));

panel1.setLayout(null);

{ // compute preferred size

Dimension preferredSize = new Dimension();

for(int i = 0; i < panel1.getComponentCount(); i++) {

Rectangle bounds = panel1.getComponent(i).getBounds();

preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);

preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);

}

Insets insets = panel1.getInsets();

preferredSize.width += insets.right;

preferredSize.height += insets.bottom;

panel1.setMinimumSize(preferredSize);

panel1.setPreferredSize(preferredSize);

}

}

add(panel1);

panel1.setBounds(20, 135, 715, 425);

{ // compute preferred size

Dimension preferredSize = new Dimension();

for(int i = 0; i < getComponentCount(); i++) {

Rectangle bounds = getComponent(i).getBounds();

preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);

preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);

}

Insets insets = getInsets();

preferredSize.width += insets.right;

preferredSize.height += insets.bottom;

setMinimumSize(preferredSize);

setPreferredSize(preferredSize);

}

DrawChart();

}

janniserya at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...
# 3

In

chartPanel.setPreferredSize(new Dimension(panel1.getHeight(),panel1.getWidth()));

if 'panel1' has not been set visible at this point then maybe you are telling the chart panel to be of zero width and height.

Since you have set 'panel1' to have a null layout then you have to set the size of your components.

sabre150a at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...
# 4

ok , then i changed it to :

chartPanel.setPreferredSize(new Dimension(400,400));

but still has no visual :( Also i tried to trace the code with :

chartPanel = new ChartPanel(chart);

<b>JOptionPane.showMessageDialog( null, "chart added....");</b>

chartPanel.setPreferredSize(new Dimension(panel1.getHeight(),panel1.getWidth()));

panel1.add(chartPanel);

<b>JOptionPane.showMessageDialog( null, ""+panel1.getComponentCount());</b>

panel1.validate();

panel1.repaint();

it returns "chart added" and "1" which i expect but the chart didn't displayed :(

janniserya at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...
# 5
I use JFreeChart in a lot of what I do and never have this sort of problem. You need to create a self contained example that illustrates the problem and then post it.P.S. I bet that in creating the self contained example you solve the problem anyway.
sabre150a at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...
# 6
i change the layout and use setOpaque(true); for panel1. It's now working fine... Sorrry , my bad :(
janniserya at 2007-7-10 14:41:33 > top of Java-index,Java Essentials,Java Programming...