Java GUI : iReport Create Parameters and Pass Variable |
Java GUI : iReport Create Parameters and Pass Variable บทความนี้เป็นสุดยอดเทคนิคการเขียน Java GUI กับ iReport วิธีหนึ่งที่น่าสนใจมาก เพราะหัวใจลองการออก Report ก็คือการให้ Report สามารถทำการ Query แสดง ข้อมูลให้ได้ตรงกับความต้องการ เช่น กรณีที่ต้องการ Report ของลูกค้าก็จะต้องมีการส่งรหัสของลุกค้าที่ต้องการไป Filter ข้อมูลใน iReport จากนั้น iReport จะนำรหัสนั้น ๆ ไปเลือกข้อมูลที่ต้องการมาแสดงผลในหน้า Report
Java GUI : iReport Create Parameters and Pass Variable
ซึ่งวิธีนี้เราจะต้องใช้การสร้าง Parameters ในฝั่งของ iReport จากนั้นใน Java GUI ก็จะทำการส่งค่า Parameters ให้กับ iReport เมื่อ iReport ได้ค่าเหล่านั้นก็จะนำไป Query ข้อมูลมาแสดงผล ซึ่งรูปแบบการส่ง Parameters มีดังนี้
// Connection
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
// Parameters
Map param = new HashMap();
param.put("p1", "val");
param.put("p2", "val");
// Report Viewer
JasperReport ir = JasperCompileManager.compileReport(report);
JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
JasperViewer.viewReport(ip);
// Connection Close
connect.close();
Step 1 สร้าง Parameters ไว้รับค่า บน iReport
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-02.jpg?v=1001)
กลับมายังหน้า Report บน iReport
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-03.jpg?v=1001)
ในส่วนของ Report Inspector จะมี Parameters
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-04.jpg?v=1001)
ให้คลิกขวาเลือก Add Parameters
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-05.jpg?v=1001)
ได้ Parametrs ชื่อว่า parameters 1
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-06.jpg?v=1001)
ใน parameters 1 จะมี Property
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-07.jpg?v=1001)
ให้เปลี่ยนชื่อเป็น sCoutryCode และเลือกชนิดเป็น String
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-08.jpg?v=1001)
จากนั้นให้คลิกที่ Query รูป Icon กลืม ๆ ดังรูป
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-09.jpg?v=1001)
ซึ่งตอนนี้จะเป็น Query คำสั่งของ SQL
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-10.jpg?v=1001)
ให้เขียน Query เพิ่ม โดยเราจะ WHERE ตาม CountryCode ของลูกค้า
WHERE CountryCode = $P{sCountryCode}
สามารถเขียน Query ต่าง ๆ ได้เหมือนกับการเขียน SQL กรณีที่มีมากกว่า 1 ตัวก็ให้ใช้ AND ได้ตามปกติ และเพียงเท่านี้ก็ถือว่าเป็นการเสร้จสิ้นขั้นตอนการสร้าง Parameters
![](/images/adv.jpg)
Step 2 ทำการ Preview ทดสอบการทำงานของ Query
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-11.jpg?v=1001)
เลือก Preview
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-12.jpg?v=1001)
จะเห็นว่าเมื่อ Preview ผ่าน iReport โปรแกรมจะมี Dialog เพื่อให้เราทำการ Input ตัว Parameters
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-13.jpg?v=1001)
ใส่รหัส CountryCode ที่ต้องการ
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-14.jpg?v=1001)
จากนั้นตัว Report ก็จะ Filter และ Query ข้อมูลเฉพาะตามที่เราเขียน Query ไว้
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-14.jpg?v=1001)
จาก Code จะเห็นว่าใน JasperPrint จะมี Argument ให้เราทำการ Assign ค่า Parameters ให้กับ iReport
Step 3 การ Preview และ Viewer ตัว iReport บน Java GUI
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-15.jpg?v=1001)
การโยนค่า Parameters ให้กับ iReport จาก Code ของ Java
Code ทั้งหมด
MyForm.java
package com.java.myapp;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;
public class MyForm extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 431, 286);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// Button Report
JButton btnOpenReport = new JButton("Open Report");
btnOpenReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection connect = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Application path
String report = null;
try {
report = new File(".").getCanonicalPath()+ "\\myReport.jrxml";
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// Parameters
Map param = new HashMap();
param.put("sCountryCode", "US");
// Report Viewer
JasperReport ir = JasperCompileManager.compileReport(report);
JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
JFrame frame = new JFrame();
frame.setTitle("ThaiCreate.Com Customer Report");
frame.setBounds(100, 100, 800,600);
frame.getContentPane().add(new JRViewer(ip));
frame.setVisible(true);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnOpenReport.setBounds(137, 98, 146, 23);
getContentPane().add(btnOpenReport);
setResizable (false);
}
}
Output
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-16.jpg?v=1001)
คลิกที่ Button ของ Open Report
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-17.jpg?v=1001)
Map param = new HashMap();
param.put("sCountryCode", "US");
Report ก็จะแสดงตาม Parameters ที่เรากำหนดขึ้น โดยในตัวอย่างนี้จะให้ Query ตาม CountryCode ของลูกค้า
หรือจะออกแบบเป็น Text Fields เพื่อรับค่าจาก Input
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-18.jpg?v=1001)
package com.java.myapp;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
public class MyForm extends JFrame {
private JTextField txtCountryCode;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
MyForm frame = new MyForm();
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public MyForm() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 431, 286);
setTitle("ThaiCreate.Com Java GUI Tutorial");
getContentPane().setLayout(null);
// Label Country Code
JLabel lblCountryCode = new JLabel("Country Code");
lblCountryCode.setHorizontalAlignment(SwingConstants.CENTER);
lblCountryCode.setBounds(123, 31, 160, 14);
getContentPane().add(lblCountryCode);
// Text Field CountryCode
txtCountryCode = new JTextField();
txtCountryCode.setHorizontalAlignment(SwingConstants.CENTER);
txtCountryCode.setBounds(161, 56, 86, 20);
getContentPane().add(txtCountryCode);
txtCountryCode.setColumns(10);
setResizable (false);
// Button Report
JButton btnOpenReport = new JButton("Open Report");
btnOpenReport.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Connection connect = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Application path
String report = null;
try {
report = new File(".").getCanonicalPath()+ "\\myReport.jrxml";
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// Parameters
Map param = new HashMap();
param.put("sCountryCode", txtCountryCode.getText());
// Report Viewer
JasperReport ir = JasperCompileManager.compileReport(report);
JasperPrint ip = JasperFillManager.fillReport(ir, param,connect);
JFrame frame = new JFrame();
frame.setTitle("ThaiCreate.Com Customer Report");
frame.setBounds(100, 100, 800,600);
frame.getContentPane().add(new JRViewer(ip));
frame.setVisible(true);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnOpenReport.setBounds(137, 98, 146, 23);
getContentPane().add(btnOpenReport);
}
}
Output
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-18.jpg?v=1001)
แสดง Input กรอกรหัส CountryCode
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-19.jpg?v=1001)
แสดง Report ตามเงื่อนไขที่เรากำหนดขึ้น
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-20.jpg?v=1001)
ทดสอบเปลี่ยนเป็น TH
![iReport Create Parameters and Pass Variable iReport Create Parameters and Pass Variable](https://www.thaicreate.com/upload/tutorial/java-gui-ireport-parameters-21.jpg?v=1001)
แสดง Report ตามเงื่อนไขที่ส่งจาก Parameters
Property & Method (Others Related) |
|
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท
|
|
|
By : |
ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ) |
|
Score Rating : |
![](/images/resource/startrue.gif) ![](/images/resource/startrue.gif) ![](/images/resource/startrue.gif) |
|
|
Create/Update Date : |
2013-09-12 11:43:20 /
2017-03-27 21:36:44 |
|
Download : |
No files |
|
Sponsored Links / Related |
|
|
|
|
|
|
|