JFreeChart
ChartPanel chartpanel = new ChartPanel(chart);
chartpanel.setDomainZoomable(true);
jPanel4.add(chartpanel, BorderLayout.CENTER);
Chart with JFrame
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
package
AAA;
import
java.awt.BorderLayout;
import
org.jfree.chart.ChartFactory;
import
org.jfree.chart.ChartPanel;
import
org.jfree.chart.JFreeChart;
import
org.jfree.chart.plot.PlotOrientation;
import
org.jfree.data.xy.XYSeries;
import
org.jfree.data.xy.XYSeriesCollection;
/**
*
* @author Satya< satyajohnny@live.com>
*/
public
class JchartExaple extends javax.swing.JFrame {
/**
* Creates new form JchartExaple
*/
public JchartExaple() {
initComponents();
}
/**
* This method is called from within the
constructor to initialize the form.
* WARNING: Do NOT modify this code. The content
of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold
defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel9 = new javax.swing.JLabel();
reg = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
pid = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
pname = new javax.swing.JTextField();
jLabel10 = new javax.swing.JLabel();
age = new javax.swing.JTextField();
gender = new javax.swing.JTextField();
jPanel4 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMaximumSize(new
java.awt.Dimension(800, 558));
setMinimumSize(new
java.awt.Dimension(800, 558));
getContentPane().setLayout(null);
jLabel9.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel9.setText("Mrd Num");
getContentPane().add(jLabel9);
jLabel9.setBounds(20, 50, 90, 20);
reg.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
reg.setToolTipText("Press
Space/Enter to search for the list");
reg.addMouseListener(new
java.awt.event.MouseAdapter() {
public void
mouseClicked(java.awt.event.MouseEvent evt) {
regMouseClicked(evt);
}
});
reg.addKeyListener(new
java.awt.event.KeyAdapter() {
public void
keyPressed(java.awt.event.KeyEvent evt) {
regKeyPressed(evt);
}
});
getContentPane().add(reg);
reg.setBounds(110, 50, 140, 26);
jLabel3.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel3.setText("Patient
Id");
getContentPane().add(jLabel3);
jLabel3.setBounds(20, 80, 90, 20);
pid.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
getContentPane().add(pid);
pid.setBounds(110, 80, 140, 26);
jLabel4.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel4.setText("Name");
getContentPane().add(jLabel4);
jLabel4.setBounds(20, 110, 90, 20);
pname.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
getContentPane().add(pname);
pname.setBounds(110, 110, 140, 26);
jLabel10.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
jLabel10.setText("Age
&Gender");
getContentPane().add(jLabel10);
jLabel10.setBounds(20, 140, 100, 20);
age.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
getContentPane().add(age);
age.setBounds(110, 140, 50, 26);
gender.setFont(new
java.awt.Font("Segoe UI", 0, 14)); // NOI18N
getContentPane().add(gender);
gender.setBounds(170, 140, 80, 26);
jPanel4.setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 102, 51), 4));
javax.swing.GroupLayout jPanel4Layout =
new javax.swing.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 702, Short.MAX_VALUE)
);
jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 332, Short.MAX_VALUE)
);
getContentPane().add(jPanel4);
jPanel4.setBounds(30, 200, 710, 340);
jButton1.setText("Chart");
jButton1.addActionListener(new
java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(320, 110, 59, 23);
pack();
}// </editor-fold>
private void
regMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void
regKeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void
jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ChartMrthod(); // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold
defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6)
is not available, stay with the default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for
(javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName()))
{
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JchartExaple.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JchartExaple.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JchartExaple.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JchartExaple.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new
Runnable() {
public void run() {
new
JchartExaple().setVisible(true);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JTextField age;
public static javax.swing.JTextField
gender;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel4;
public static javax.swing.JTextField pid;
public static javax.swing.JTextField pname;
public static javax.swing.JTextField reg;
// End of variables declaration
public
void ChartMrthod()
{
try {
XYSeries series = new XYSeries("asdf");
for (int i = 0; i < 100; i++)
series.add(i, Math.random());
XYSeriesCollection dataset = new
XYSeriesCollection(series);
JFreeChart chart =
ChartFactory.createXYLineChart(null, null, null, dataset, PlotOrientation.HORIZONTAL,
true, true, true);
ChartPanel chartpanel = new
ChartPanel(chart);
chartpanel.setDomainZoomable(true);
jPanel4.setLayout(new BorderLayout());
jPanel4.add(chartpanel,
BorderLayout.NORTH);
this.add(jPanel4);
this.pack();
this.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}