Kamis, 08 Maret 2012

Event Handling

Event Handling
Event berguna untuk menangani interaksi user dengan program, misalnya user memilih
sebuah menu dalam aplikasi MIDlet.Untuk menangani event perlu
mengimplementasikan interface CommandListener dan atau ItemListener.
CommandListener berfungsi untuk menangani jika user memilih Command tertentu
sedangkan ItemListener berfungsi untuk menangani jika user mengubah nilai seperti
misalnya mengubah pilihan pada ChoiceGroup.
Untuk memberikan gambaran sebuah event bekerja, perhatikan contoh berikut.
import javax.microedition.MIDlet.*;
import javax.microedition.lcdui.*;
public class NotHelloWorld2 extends MIDlet implements CommandListener{
private Command cmdExit;
private Display display;
public NotHelloWorld2()
{
display = Display.getDisplay(this);
cmdExit = new Command(“Exit”, Command.SCREEN,2);
}
public void startApp()
{
TextBox t = new TextBox(“Not “,”Not Hello World”,256,0);
t.addCommand(cmdExit);
t.setCommandListener(this);
display.setCurrent(t);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command cmd,Displayable disp)
{
if(cmd==cmdExit)
{
destroyApp(false);
notifyDestroyed();
}
}
}
Pemrograman aplikasi wireless dengan Java/J2ME 24
Pada contoh kedua ini, kita tambahkan sebuah Command untuk keluar dari aplikasi
yang telah kita buat pada contoh 1. Ketika program pertama kali dipanggil, maka state
berada pada Aktif, disini sebuah TextBox dan Command dibuat. Kemudian program
menunggu respon dari user dengan mengimplementasikan CommandListener. Ketika
user menekan Command “cmdExit” , maka program memanggil
metodh(destroyApp) untuk membunuh MIDlet.
Sekian dan Terima Kasih . . . .

Konsep Dasar Stream I/O

Tipe-Tipe Stream yang Umum Digunakan :
stream karakter dan byte. Kita hanya mengulang perbedaan mendasar antara
keduanya. Stream byte adalah abstraksi file atau alat untuk data biner sedangkan
stream karakter adalah untuk karakter Unicode.
Class InputStream adalah abstraksi class root untuk semua input stream byte sedangkan
class OutputStream adalah class root abstraksi dari semua output stream byte. Untuk
stream karakter, superclasss yang sesuai dari semua class-class secara berturut-turut
adalah class Reader dan the Writer. Kedua class-class ini adalah abstraksi class-class
untuk membaca dan menulis stream karakter.
Stream juga dikategorikan berdasarkan apakah mereka digunakan untuk membaca atau
menulis stream.
Contoh Stream/IO :
import java.io.*;
class CopyFile {
void copy(String input, String output) {
FileReader reader;
FileWriter writer;
int data;
try {
reader = new FileReader(input);
writer = new FileWriter(output);
while ((data = reader.read()) != -1) {
writer.write(data);
}
reader.close();
writer.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
public static void main(String args[]) {
String inputFile = args[0];
String outputFile = args[1];
CopyFile cf = new CopyFile();
cf.copy(inputFile, outputFile);
}
}

Simple project with SWT and Swing :)

            Program SWT:
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
 
public class HelloWorld 
{
   public static void main (String[] args) 
  {
      Display display = new Display();
      Shell shell = new Shell(display);
      Label label = new Label(shell, SWT.NONE);
      label.setText("Hello World");
      label.pack();
      shell.pack();
      shell.open();
      while (!shell.isDisposed()) 
      {
         if (!display.readAndDispatch()) display.sleep();
      }
      display.dispose();
   }
}
            Program Swing:
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
 
public class SwingExample implements Runnable {
    @Override
    public void run() {
        // Create the window
        JFrame f = new JFrame ("Hello, World!");
        // Sets the behaviour for when the window is closed
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // add a label and a button
        f.getContentPane().add(new JLabel("Hello, world!"));
        f.getContentPane().add(new JButton("Press me!"));
        // arrange the components inside the window
        f.pack();
        //By default, the window is not visible. Make it visible.
        f.setVisible(true);
    }
 
    public static void main(String[] args) {
        SwingExample se = new SwingExample();
        // Schedules the application to be run at the correct time in the event queue.
        SwingUtilities.invokeLater(se);
    }
}       

SWT, AWT, Java Swing

             Standard Widget Toolkit (SWT) is a graphical widget toolkit for use with the Java platform. It was originally developed by IBM and is now maintained by the Eclipse Foundation in tandem with the Eclipse IDE. It is an alternative to the Abstract Window Toolkit (AWT) and Swing Java GUI toolkits provided by Sun Microsystems as part of the Java Platform, Standard Edition. To display GUI elements, the SWT implementation accesses the native GUI libraries of the operating system using JNI (Java Native Interface) in a manner that is similar to those programs written using operating system-specific APIs. Programs that call SWT are portable, but the implementation of the toolkit, despite part of it being written in Java, is unique for each platform.
The toolkit is licensed under the Eclipse Public License, an open source license approved by the Open Source Initiative.

             Abstract Window Toolkit (AWT) is Java's original platform-independent windowing, graphics, and user-interface widget toolkit. The AWT is now part of the Java Foundation Classes (JFC) — the standard API for providing a graphical user interface (GUI) for a Java program.
AWT is also the GUI toolkit for a number of Java ME profiles. For example, Connected Device Configuration profiles require Java runtimes on mobile telephones to support AWT.

            Swing is the primary Java GUI widget toolkit. It is part of Oracle's Java Foundation Classes (JFC) — an API for providing a graphical user interface (GUI) for Java programs.
Swing was developed to provide a more sophisticated set of GUI components than the earlier Abstract Window Toolkit (AWT). Swing provides a native look and feel that emulates the look and feel of several platforms, and also supports a pluggable look and feel that allows applications to have a look and feel unrelated to the underlying platform. It has more powerful and flexible components than AWT. In addition to familiar components such as buttons, check box and labels, Swing provides several advanced components such as tabbed panel, scroll panes, trees, tables and lists.
Unlike AWT components, Swing components are not implemented by platform-specific code. Instead they are written entirely in Java and therefore are platform-independent. The term "lightweight" is used to describe such an element. The Hierarchy:

       


              Jadi, kalo penjelasan singkatnya, AWT itu toolkit GUI standar independen yang dipunyai java, misalnya textarea, button, checkbox, label, textfield. Si AWT ini juga bisa buat MobileApps dengan profil Java tertentu. Kalo SWT itu hampir sama kaya AWT, tapi dengan pengembang yang berbeda, terus kalo SWT itu Open Source, kalo AWT itu nggak, terus juga Libraries beda,kalo ini pake JNI, dan juga hanya bisa untuk suatu spesifik program dan Swing itu versi paling canggih dan kompleks dari JFC, jadi tinggal tambah pengertian SWT dan QWT, trus tambahin kompleks dan canggih :D
Contoh program ada di posting selanjutnya :)

Java Foundation Classes (JFC)

            The Java Foundation Classes (JFC) are a graphical framework for building portable title "Java (programming language)" Java-based graphical user interfaces (GUIs). JFC consists of the Abstract Window Toolkit (AWT), Swing and Java 2D. Together, they provide a consistent user interface for Java programs, regardless whether the underlying user interface system is Windows, Mac OS X or Linux.

           
             AWT existed before JFC. AWT was heavily criticized for being little more than a wrapper around the native graphical capabilities of the host platform. That meant that the standard widgets in the AWT relied on those capabilities of the native widgets, requiring the developer to also be aware of the differences between host platforms.
An alternative graphics library called the Internet Foundation Classes was developed in more platform-independent code by Netscape.
At the same time, another graphics library, called Application Foundation Classes (AFC), was developed independently by Microsoft. It was made to be easier to extend the graphic components, but was primarily aimed for use with the Microsoft Java Virtual Machine.
On April 2, 1997, Sun Microsystems and Netscape announced their intention to combine IFC with other technologies to form the "Java Foundation Classes". The "Java Foundation Classes" were later renamed "Swing", adding the capability for a pluggable look and feel of the widgets. This allowed Swing programs to maintain a platform-independent code base, but mimic the look of a native application. The release of JFC made IFC obsolete, and dropped interest for Microsoft's AFC.

            Jadi, intinya si JFC ini adalah framework GUI punya JAVA yg terdiri dari Abstract Window Toolkit (AWT), Swing, Java 2D, dan Standard Window Toolkit (SWT), dan itu nggak tergantung sama OS, entah linux, windows atau MAC. Tentang SWT, AWT dan Swing akan ada di posting selanjutnya :D