Category: Programming


Berkenalan dengan Java GUI

Java GUI? Kenapa tidak. Mudah? Ya.

Mari kita berkenalan dengan Java GUI. Di postingan ini, akan didemonstrasikan dasar-dasar pemrograman GUI dengan menggunakan Java. Mari kita mulai dengan membuat Frame. Pertama buat class dengan nama Main seperti di bawah ini.

public class Main {

	public static void main(String[] args) {
		new Main();
	}

}

Lalu sekarang modifikasi class Main, supaya meng-extends class JFrame. Tambahkan juga constructor class Main seperti di bawah ini. (Dan jangan lupa import class javax.swing.JFrame).

import javax.swing.JFrame;

public class Main extends JFrame {

	public Main(){
		super("Test Java GUI");
		setSize(300, 200);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new Main();
	}

}

Setelah itu, Run Program. Apa yang terjadi? Muncul Frame dengan ukuran 300 x 200 pixels, pada posisi pojok kiri atas.

JFrame Example

JFrame Example

Apa sih itu JFrame? JFrame itu ya komponen GUI yang bentuknya seperti yg tadi di-Run. Lalu apa gunanya memanggil method super(“Test Java GUI”) pada constructor? Ya, tulisan itu muncul di Frame tadi. Karena class Main merupakan turunan dari class JFrame, maka pemanggilan method super() pada contructor di atas berarti memanggil constructor dari class JFrame. Begitu juga dengan pemanggilan method setSize(), setDefaultCloseOperation(), dan setVisible(), itu semua adalah method yg diturunkan dari class parent. Fungsi method setSize() saya rasa cukup jelas, itu untuk men-setting size dari Frame. setVisible() juga sudah cukup jelas, yaitu supaya Frame tsb dapat dilihat (visible), by default adalah false. Jadi  kita harus setVisible dengan nilai true. Lalu kalau setDefaultCloseOperation() untuk apa? Method ini untuk menentukan apa yg harus dilakukan program apabila Frame tadi di-close. Dalam contoh ini, program harus exit (terminate) apabila Frame di-close. Kalau tidak disetting, nanti saat Frame di-close.. program akan tetap running. Tentu kita tidak menginginkan hal itu. Kita ingin Program berhenti setelah Frame tadi di-close.

Mengapa program muncul di pojok kiri atas? Bisa tidak program muncul di tengah-tengah layar? Akan dibahas di postingan berikutnya.

Upload File in Spring

Spring has built-in multipart support to handle fileuploads in web applications. The design for the multipart support is done with pluggable MultipartResolver objects. This object available on Spring library, defined in the org.springframework.web.multipart.MultipartFile package. Out of the box, Spring provides MultipartResolvers for use with Commons FileUpload (http://jakarta.apache.org/commons/fileupload) and COS FileUpload (http://www.servlets.com/cos). In this post, it shows you how to handle file upload in Spring MVC web application.

By default, no multipart handling will be done by Spring. You will have to enable it yourself by adding a multipart resolver to the web application’s context. After you have done that, each request will be inspected to see if it contains a multipart. If no multipart is found, the request will continue as expected. However, if a multipart is found in the request, the MultipartResolver that has been declared in your context will be used. After that, the multipart attribute in your request will be treated like any other attribute.

Using the MultipartResolver

The CommonsMultipartResolver is a common MultipartResolver implementation, which use the Apache commons upload library to handle the file upload in a form. To use CommonsMultipartResolver to handle the file upload, you need to get the commons-fileupload.jar andcommons-io.jar libraries.

The following example shows how to use the CommonsMultipartResolver:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

This is an example using the CosMultipartResolver:

<bean id="multipartResolver" class="org.springframework.web.multipart.cos.CosMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

Continue reading

Write to File in Java

In this tutorial, I will show you how to write java program to write to a file. We will use the class FileWriter and BufferedWriter to write to a file.

Why we use FileWriter?

The FileWriter is a class used for writing character files. FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream. The constructors of FileWriter class assume that the default character encoding and the default byte-buffer size are acceptable.

Note that some platforms, in particular, allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. Well in this situation, the constructors in this class will fail if the file involved is already open.

Then, why we use BufferedWriter too?

The BufferedWriter class is used to write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

The buffer size may be specified, or the default size may be accepted. In this tutorial, we don’t need to specify the buffer size.

Ok, now here is the code of java program to write text to a file:

import java.io.*;

public class FileWrite{
 public static void main(String args[]){
  try{
   // Create file
   FileWriter fstream = new FileWriter("out.txt");
   BufferedWriter out = new BufferedWriter(fstream);
   // Write to file
   out.write("Hello Java");
   //Close the output stream
   out.close();
  }catch (Exception e){ //Catch exception if any
   System.err.println("Error: " + e.getMessage());
  }
 }
}

Selama ini gw selalu pake jpos, dan jpos itu dokumentasinya berbayar (kalo ga salah 50 USD harganya). Ga pernah kepikiran bikin parser sendiri.

Sampai akhirnya ada yg request buat parser dalam bahasa pascal. Gw sempetin diantara sela-sela kerjaan.. akhirnya jadi pula.

Berikut ini saya share source code untuk parsing iso8583 dalam bahasa pascal. Akan tetapi, source code berikut ini hanyalah basic algorithm, dan hanya untuk message iso 8583 64 bit. Untuk yg 128 / 192 bit harus diubah lagi packagernya. (males gw ngubahnya, abis kepanjangan wkwkwk).

Oiya, program ini membaca iso string yg disimpan dalam file test.txt. Jadi buat dulu file bernama test.txt yang berisi iso 8583 string yang akan diparsing. Ini contoh iso 8583 yg akan diparsing:

01002220050028C010041100000619163620193776021046316278790012300001=15031011000000000000000003ABCD0123TESTINGABCD0123F713B5B0ED3D0A61006000003

Untuk source code parsernya, silakan dilihat-lihat: Continue reading

Pattern Check in Java

Today I face one problem. My job is Java Programmer, and I take responsibility for EDC Server. The problem is data that sent by EDC is not valid, I means.. it contains non-ASCII character.

Based on document specification EDC-EDC Server, EDC must sent data in ASCII character. This character will be forward to switching which is dont know what is non-ASCII character.

Since the data that will be send to switching its only contains alphanumeric (inluce space) and ‘=’, so I try this method:

private boolean checkFirst(String isoString){
	boolean retVal = true;
	if(!Pattern.matches("[a-zA-Z0-9= ]+", isoString)) {
		retVal = false;
	}
	return retVal;
}

And.. it works! Simple, and I dont need to check every single character on message. (dont forget to import java.util.regex.Pattern). And the problem now is solved :mrgreen:

Follow

Get every new post delivered to your Inbox.

Join 1,515 other followers