Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java GUI สอน Java GUI เขียนโปรแกรม GUI ด้วย Java(Swing/AWT/JFC) > Java GUI : Connect and Viewer/Preview iReport (JasperReport)



Clound SSD Virtual Server

Java GUI : Connect and Viewer/Preview iReport (JasperReport)

Java GUI : Connect and Viewer/Preview iReport (JasperReport) หลังจากที่เราได้ Report ที่ถูกสร้างจาก iReport ผ่านการเชื่อม Connection เขียน Query และ Preview จน Report ออกตรงกับความต้องการของเราแล้ว ในตัวอย่างนี้เราจะมาเขียน Java GUI ในการที่จะ Preview ตัว Report ด้วยโปรแกรม Java GUI ซึ่งสิ่งที่เราจำเป็นจะต้องใช้คือ ไฟล์นามสกุล .jrxml ที่ได้จาก iReport ในกรณีที่ Report มีพวก รูปภาพ ก็จำเป็นจะต้องทำการ Copy มาด้วย

iReport Create Design Report Wizard and Preview Report (Step by Step)


Java GUI : Connect and Viewer/Preview iReport

iReport Report JasperViewer การแสดงผล Report จะใช้ Library ของ JasperViewer


ในกรณีที่ Path ไม่ถูกต้อง Report อาจจะไม่ทำงาน ฉะนั้นในส่วนนี้อาจจะต้องใช้ความระมัดระระวังในเรื่องของ Path ด้วย แต่ทั้งนี้ถ้าเราสามารถใช้งาน iReport ได้อย่างคล่องแคล่ว ก็สามารถที่จะแก้ไขรายละเอียดต่าง ๆ ได้จากไฟล์ .jrxml ได้เช่นเดียวกัน

Java GUI : Connect and Viewer/Preview iReport

สิ่งที่จำเป้นจะต้องใช้คือ myReport.jrxml และรูปภาพที่เกี่ยวข้อง ซึ่งไฟล์ .jrxml นี่แหละเป็นหัวใจของ Report ที่จะนำมาใช้กับ Java GUI

Java GUI : Connect and Viewer/Preview iReport

และเมื่อเราเปิดดูก็จะเห้นว่า iReport ถูกสร้างโดยใช้ XML เป็น UI นี่เอง และเราจะเห็นพวกคำสั่ง SQL Query ที่เราได้เขียนและออกแบบไว้ในขั้นตอนการสร้าง Report และที่สำคัญก์คือ พวก Connection ต่าง ๆ ที่ได้สร้างไว้ จะไม่มาด้วยแน่นอน ฉะนั้นในการที่จะ Preview Report ด้วย Java GUI เราจำเป็นจะต้องสร้าง Connection ตัวเดียวกับตัวที่สร้างไว้บน iReport เพราะไฟล์ .jrxml มีหน้าที่แค่อ่าน Connection และ Query แสดงผลตามเงื่อนไขที่ออกแบบและเขียนไว้เท่านั้น

รูปแบบการสร้าง Connection และการ Preview Viewer ตัว iReport

// Create Connection
Class.forName("com.mysql.jdbc.Driver");
connect =  DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
		"?user=root&password=root");

// Report Viewer
String report = new File(".").getCanonicalPath()+ "\\myReport.jrxml";
JasperReport ir = JasperCompileManager.compileReport(report);
JasperPrint ip = JasperFillManager.fillReport(ir, null,connect);
JasperViewer.viewReport(ip);

// Close Connection
connect.close();

รูปแบบคำสั่งการเรียก Viewer ของ iReport ประกอบด้วย Connection และตัว Viewer (ซึ่งตัว Viewer จะมีการเรียกใช้ JAR Library อีก 5-6 ตัว ดูได้จากย่อหน้าถัดไป)

กลับมายังหน้าจอของ Java GUI Project

Java GUI : Connect and Viewer/Preview iReport

ออกแบบหน้าจอ Frame ดังรูป

Java GUI : Connect and Viewer/Preview iReport

สร้างปุ่ม JButton สำหรับเปิด Report Viewer

Java GUI : Connect and Viewer/Preview iReport

สร้าง Event ให้กับ Button

Java GUI : Connect and Viewer/Preview iReport

และในบทความนี้ใช้ MySQL Database ให้ทำการ Add JAR Library ของ MySQL Conenctor เข้ามาด้วย

และที่สำคัญ JAR Library ของ JasperReport ซึ่งเป็นตัว Viewer สามารถ Copy ได้จากโฟเดอร์ที่ได้ทำการติดตั้ง iReport

Java GUI : Connect and Viewer/Preview iReport

C:\Program Files\Jaspersoft\iReport-5.2.0\ireport\modules\ext

ขึ้นอยู่กับ Version ของ iReport ด้วย แต่หลัก ๆ จะคล้าย ๆ กัน

Java GUI : Connect and Viewer/Preview iReport

โดยเลือกเอาเฉพาะไฟล์ดังนี้
  • commons-beanutils-1.8.2.jar
  • commons-collections-3.2.1.jar
  • commons-digester-2.1.jar
  • commons-logging-1.1.jar
  • groovy-all-2.0.1.jar
  • iText-2.1.7.js2.jar
  • jasperreports-5.2.0.jar

ขาดตัวใดตัวหนึ่ง Report อาจจะไม่ทำงาน และให้ดูแล้วแต่ Version ด้วย ชื่ออาจจะแตกต่างกันไป และ import library เหล่านี้มาใช้งานด้วย

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.view.JasperViewer;


จากนั้นเขียน 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 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.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 {
					
					// Report Viewer
					JasperReport ir = JasperCompileManager.compileReport(report);
					JasperPrint ip = JasperFillManager.fillReport(ir, null,connect);
					JasperViewer.viewReport(ip);
					
				} 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);
		
	}
}

Code อาจจะยาวหน่อย แต่อันที่จริงประเด็นสำคัญอยู่ที่ Connection และ JasperReport Viewer เท่านั้น

Ouput

Java GUI : Connect and Viewer/Preview iReport

ทดสอบการทำงานของ Report คลิกที่ Open Report

Java GUI : Connect and Viewer/Preview iReport

รอซะครู่ โปรแกรมจะแสดงตัว Viewer ซึ่งจะเป็น Dialog เพื่อ Preview ตัว Report

สรุป การ View ตัว iReport ด้วย Java GUI ก็มีเพียงขั้นตอนง่าย ๆ เท่านี้ เพราะการที่จะให้ Report ออกมาสวยและตรงกับความต้องการ เราจะต้องไปทุ่มเทกับการศึกษาและออกแบบ Design บน iReport ซะมากกว่า และในการ Preview Report ด้วย Java สิ่งหนึ่งที่ขาดไม่ได้ ก็คือการส่งค่า Parameters จาก Java GUI ไปยัง iReport เพื่อทำการเลือกเงื่อนไขต่าง ๆ ตามที่ iReport กำหนดขึ้น และเช่นเดียวกันใน iReport จะต้องสร้าง Parameters รองรับด้วย สามารถอ่านได้จากหัวข้อถัดไป

บทความถัดไปที่แนะนำให้อ่าน


กรณีที่ใช้ร่วมกับ Database อื่น ๆ สามารถดูวิธีการใช้ Connector และ Connection String ได้ที่นี่







   
Share

Property & Method (Others Related)

iReport คืออะไร วิธีติดตั้ง iReport Designer for JasperReports (Step by Step)
Create iReport JasperReports and Database Connection
iReport Create Design Report Wizard and Preview Report (Step by Step)
Java GUI : Viewer/Preview iReport on JFrame/JPanel
Java GUI : iReport Create Parameters and Pass Variable

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-09-12 11:42:43 / 2013-09-12 14:56:36
  Download : No files
 Sponsored Links / Related

 
iReport คืออะไร วิธีติดตั้ง iReport Designer for JasperReports (Step by Step)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่