problem in displaying chart in swing application.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
anamika1977
Posts: 4
Joined: Fri May 09, 2008 5:38 pm

problem in displaying chart in swing application.

Post by anamika1977 » Fri May 09, 2008 5:40 pm

Hi,

I am writing a swing application to show the charts based on some user selection. But when I try to draw chart and display on button click it is not showing can someone help.

public class DataAnalysisTool extends ApplicationFrame {

private static final long serialVersionUID = 1L;

public static void main(String args[]) {
new DataAnalysisTool("Demo");
}

public DataAnalysisTool(final String title) {
super(title);
initComponents();
}

private void initComponents() {

dlmList = new DefaultListModel();

tabbedPane1 = new JTabbedPane();
panel1 = new JPanel();
label1 = new JLabel();
vSpacer1 = new JPanel(null);
scrollPane1 = new JScrollPane();
list1 = new JList(dlmList);
scrollPane2 = new JScrollPane();
panel3 = new JPanel();
label3 = new JLabel();
comboBox1 = new JComboBox();
checkBox1 = new JCheckBox();
checkBox2 = new JCheckBox();
checkBox3 = new JCheckBox();
checkBox4 = new JCheckBox();
label4 = new JLabel();
label5 = new JLabel();
textField1 = new JTextField();
textField2 = new JTextField();
label6 = new JLabel();
button1 = new JButton();
panel2 = new JPanel();
label2 = new JLabel();

//======== this ========
Container contentPane = getContentPane();
contentPane.setLayout(null);

//======== tabbedPane1 ========
{
tabbedPane1.setFont(new Font("Trebuchet MS", Font.BOLD, 12));

//======== panel1 ========
{

// JFormDesigner evaluation mark
/* panel1.setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), panel1.getBorder())); panel1.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
*/
panel1.setLayout(null);

//---- label1 ----
label1.setText("Instruments");
label1.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
panel1.add(label1);
label1.setBounds(15, 7, 80, 20);
panel1.add(vSpacer1);
vSpacer1.setBounds(250, 30, vSpacer1.getPreferredSize().width, 660);

//======== scrollPane1 ========
{
scrollPane1.setViewportView(list1);
fillInstrumentList();
}
panel1.add(scrollPane1);
scrollPane1.setBounds(10, 30, 230, 660);
panel1.add(scrollPane2);
scrollPane2.setBounds(265, 75, 845, 615);

//======== scrollPane2 ========
{

//======== panel3 ========
{
panel3.setLayout(null);

{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel3.getComponentCount(); i++) {
Rectangle bounds = panel3.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 = panel3.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel3.setMinimumSize(preferredSize);
panel3.setPreferredSize(preferredSize);
}
}
scrollPane2.setViewportView(panel3);
}

//---- label3 ----
label3.setText("Price Type :");
label3.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(label3);
label3.setBounds(270, 40, 80, label3.getPreferredSize().height);
panel1.add(comboBox1);
comboBox1.setBounds(345, 35, 160, comboBox1.getPreferredSize().height);

//---- checkBox1 ----
checkBox1.setText("Today's Data");
checkBox1.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(checkBox1);
checkBox1.setBounds(new Rectangle(new Point(540, 10), checkBox1.getPreferredSize()));

//---- checkBox2 ----
checkBox2.setText("Real Time");
checkBox2.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(checkBox2);
checkBox2.setBounds(new Rectangle(new Point(540, 35), checkBox2.getPreferredSize()));

//---- checkBox3 ----
checkBox3.setText("History");
checkBox3.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(checkBox3);
checkBox3.setBounds(new Rectangle(new Point(660, 10), checkBox3.getPreferredSize()));

//---- checkBox4 ----
checkBox4.setText("Add Random");
checkBox4.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(checkBox4);
checkBox4.setBounds(new Rectangle(new Point(660, 35), checkBox4.getPreferredSize()));

//---- label4 ----
label4.setText("From");
label4.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(label4);
label4.setBounds(775, 25, 50, label4.getPreferredSize().height);

//---- label5 ----
label5.setText("To");
label5.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
panel1.add(label5);
label5.setBounds(775, 45, 50, label5.getPreferredSize().height);
panel1.add(textField1);
textField1.setBounds(850, 20, 105, textField1.getPreferredSize().height);
panel1.add(textField2);
textField2.setBounds(850, 45, 105, textField2.getPreferredSize().height);

//---- label6 ----
label6.setText("History");
label6.setFont(new Font("Trebuchet MS", Font.BOLD, 12));
panel1.add(label6);
label6.setBounds(775, 5, 70, label6.getPreferredSize().height);

//---- button1 ----
button1.setText("Draw Graph");
button1.setFont(new Font("Trebuchet MS", Font.BOLD, 11));
panel1.add(button1);
button1.setBounds(new Rectangle(new Point(995, 40), button1.getPreferredSize()));

{ // 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);
}
}
tabbedPane1.addTab("Prices", panel1);


//======== panel2 ========
{
panel2.setLayout(null);
panel2.add(chartpanel);

{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < panel2.getComponentCount(); i++) {
Rectangle bounds = panel2.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 = panel2.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
panel2.setMinimumSize(preferredSize);
panel2.setPreferredSize(preferredSize);
}
}
tabbedPane1.addTab("Trades", panel2);

}
contentPane.add(tabbedPane1);
tabbedPane1.setBounds(5, 15, 1115, 720);

//---- label2 ----
label2.setText("CFD Data Analysis Tool");
label2.setFont(new Font("Trebuchet MS", Font.BOLD, 14));
label2.setBorder(new BevelBorder(BevelBorder.RAISED));
label2.setHorizontalAlignment(SwingConstants.CENTER);
label2.setBackground(new Color(255, 184, 51));
contentPane.add(label2);
label2.setBounds(345, 5, 410, 25);

{ // compute preferred size
Dimension preferredSize = new Dimension();
for(int i = 0; i < contentPane.getComponentCount(); i++) {
Rectangle bounds = contentPane.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 = contentPane.getInsets();
preferredSize.width += insets.right;
preferredSize.height += insets.bottom;
contentPane.setMinimumSize(preferredSize);
contentPane.setPreferredSize(preferredSize);
}
pack();
setLocationRelativeTo(getOwner());
// JFormDesigner - End of component initialization //GEN-END:initComponents

setVisible(true);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

//Search event for the database query
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//x = new TimeSeries("Time", Millisecond.class );
x = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Month.class);
x.add(new Month(2, 2001), 181.80000000000001D);
x.add(new Month(3, 2001), 167.30000000000001D);
x.add(new Month(4, 2001), 153.80000000000001D);
x.add(new Month(5, 2001), 167.59999999999999D);

x.setMaximumItemAge( TIMEWINDOW );

timeseriesCollection = new org.jfree.data.time.TimeSeriesCollection( );
timeseriesCollection.addSeries( x );

chart = ChartFactory.createTimeSeriesChart( "BUNDESREPUBLIC SR.05 BUND-3.500-01/04/16", "Time", "Latency", timeseriesCollection, true, true, false) ;
chart.setBackgroundPaint(Color.BLUE);
XYPlot xyplot = (XYPlot)chart.getPlot();
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
if(xyitemrenderer instanceof XYLineAndShapeRenderer)
{
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(false);
}
DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));


chartpanel = new ChartPanel(chart);
chartpanel.setPreferredSize(new Dimension(500, 270));
chartpanel.setMouseZoomable(true, false);

panel3.add(chartpanel);
panel3.validate();
tabbedPane1.validate();
panel3.repaint();
tabbedPane1.repaint();
//contentPane.validate();
}
});

}

private JTabbedPane tabbedPane1;
private JPanel panel1;
private JLabel label1;
private JPanel vSpacer1;
private JScrollPane scrollPane1;
private JList list1;
private JScrollPane scrollPane2;
private JLabel label3;
private JPanel panel3;
private JComboBox comboBox1;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private JCheckBox checkBox3;
private JCheckBox checkBox4;
private JLabel label4;
private JLabel label5;
private JTextField textField1;
private JTextField textField2;
private JLabel label6;
private JButton button1;
private JPanel panel2;
private JLabel label2;

DefaultListModel dlmList;

private JFreeChart chart;
private TimeSeriesCollection timeseriesCollection;
private TimeSeries x = null;
private ChartPanel chartpanel;
final static long TIMEWINDOW = 60 * 60 * 1000;

}

Taqua
JFreeReport Project Leader
Posts: 698
Joined: Fri Mar 14, 2003 3:34 pm
Contact:

Post by Taqua » Fri May 09, 2008 7:08 pm

I'd say: Learn how to use LayoutManagers.

Your code removes all layout managers from all panels and then you do not set a size (using setSize(..) as setPreferredSize is a hint for the layout manager you already killed).

Else, if you insist on not using a layout manager, then at least compute the layout properly :)

Have fun,
said Thomas

Valentino_FBI
Posts: 5
Joined: Mon May 19, 2008 1:34 pm
Location: Bulgaria
Contact:

Hi

Post by Valentino_FBI » Mon May 19, 2008 1:51 pm

I have used LayoutManagers but the problem is still here!

ChartPanel bar_panel = new ChartPanel(mychart);
Chart_Panel.add(bar_panel);
Chart_Panel.repaint();
- this doesn't work
//getFrame().pack(); - this does work , but the main frame/window/ is resizing each time

Chart_Panel has BoxLayoutManager. Why repaint() method doesn't work?

Locked