GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, April 20, 2011

Java - Packaging

The firstly build the nice directory, for example:
C:\project
    |- bin
    |- src
        |- com
            |- lucia
                |- Boo.java

Then compile your java file using:
C:\project javac -d bin src\com\lucia\Boo.java

The above command will make a binary class file inside your bin directory plus the package.

C:\project> javac -d bin -sourcepath src src\com\lucia\Main.java
C:\project> java -cp bin com.lucia.Main
See more on: http://www.ibm.com/developerworks/library/j-classpath-windows/
Share/Bookmark

Monday, December 27, 2010

Iterating Method In Java

For a given array, to iterate over the whole elements in the array, use:

String[] names = new String[]{"lady gaga","barack obama","larry page"};
for(String s: names){
    System.out.println(s);
}

output:
lady gaga
barack obama
larry page
     
Share/Bookmark

Wednesday, December 22, 2010

Swing JRadioButton

To use this component, it's needed to use ButtonGroup:

import java.awt.*:
import javax.swing.*;

public Main{

   public static void main(String[] args){

        JFrame frame = new JFrame("Radio Button");

        JRadioButton rbApel = new JRadioButton("Apel");
        JRadioButton rbNanas = new JRadioButton("Nanas");
        JRadioButton rbJambu = new JRadioButton("Jambu"):

        ButtonGroup group = new ButtonGroup();
        group.add(rbApel);
        group.add(rbNanas);
        group.add(rbJambu);

        frame.add(rbApel);
        frame.add(rbNanas);
        frame.add(rbJambu);

        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
   }
}
Share/Bookmark

Swing JCheckBox

import javax.swing.*;
import java.awt.*;


public Main{

    public static void main(String[] args){

        JFrame frame = new JFrame("CheckBox Application");

        JCheckBox cbJambu = new JCheckBox("Jambu");
        JCheckBox cbNanas = new JCheckBox("Nanas");
        JCheckBox cbApel = new JCheckBox("Apel");

        frame.add(cbJambu);
        frame.add(cbNanas);
        frame.add(cbApel);

        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOPeration(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Share/Bookmark

Swing JEditorPane

This component can be made to load a text string or other. For file you can use: file:\\d:\\data.txt

import javax.swing.*:

public class Main{

    public static void main(String[] args){

        JFrame frame = new JFrame("JEditorPane Application");

        JEditorPane editPane = new JEditorPane();

        try{
            editPane.setPage("http://google.com");
        }catch(Exception ex){
            ex.printStackTrace();
        }

        frame.add(editPane);
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
   }
}
Share/Bookmark

Swing JTabbedPane

Here's a code on how to show about JTabbedPane: Layout for tabbed pane is BorderLayout by default.

import javax.swing.*;

public class Main{

    public static void main(String[] args){

        JFrame frame = new JFrame("Tab Pane Application");

        JTabbedPane tabPane = new JTabbedPane();

        tabPane.addTab("General", new JButton("Exit"));
        tabPane.addTab("Configuration");
        tabPane.addTab("Advance");

        frame.add(tabPane);
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOporation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}
Share/Bookmark

Tuesday, December 21, 2010

Swing - Load File To JTextArea

Here's a code on how to load a text file to a JTextArea Component:

import java.io.*;
import javax.swing.*;

public class Main{

    public static void main(String[] args){

        JFrame frame = new JFrame("JTextArea Application");
        JTextArea textArea = new JTextArea();

        String text = "";
        try{
            FileReader fr = new FileReader("D://data.txt");
            BufferedReader reader = new BufferedReader(fr);
            String textLine = "";
            while(textLine!=null){
                textLine = reader.readLine();
                text = text + textLine + "\n";
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }

        textArea.setText(text);

        frame.add(new JScrollPane(textArea));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Share/Bookmark

Swing - Table Model

This is a code on how to use table with model:

import javax.swing.*;
import javax.swing.table.*;

public class Main{

    public static void main(String[] args){

        new Main();
   }

    public Main(){

        frame = new JFrame("Table Model Application");

        table = new JTable(new Model());

        frame.add(new JScrollPane(table));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

class Model extends AbstractTableModel{

    public Model(){

        cells = new String[][]{
            new String[]{"1", "Lady Gaga", "New York"},
            new String[]{"2", "Luna Maya", "Denpasar"},
            new String[]{"3", "Aura Kasih", "Jakarta"},
            new String[]{"4", "Bill Gates", "Redmond"}
        };
    }

    public int getRowCount(){
        return cells.length;
    }
    public int getColumnCount(){

        return cells[0].length;
    }

    public Object getValueAt(int r, int c){

        return cells[r][c];
    }

    public getColumnName(int c){

        String[] column = new String[]{"No", "Name",  "City"};
        return column[c];
    }

    private String[][] cells;
}

          
              
Share/Bookmark

Monday, December 20, 2010

Swing - Simple JTable

A code for using JTable in Swing:

import javax.swing.*;
import java.awt.*;

public class Main{

    public static void main(String[] args){

        new Main();
    }

    public Main(){

        frame = new JFrame("JTable Application");

        String[][] cells = {
            {"1", "Lady Gaga", "New York"},
            {"2", "Aura Kasih", "Jakarta'],
            {"3", "Luna Maya", "Denpasar"}
        };
        String[] column = {"No", "Name", "City"};

        table = new JTable(cells, column);

        frame.add(new JScrollPane(table));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private JFrame frame;
    private JTable table;
}

Share/Bookmark

Swing - JTree

Here's a code on how to use JTree:

import javax.swing.*;
import javax.swing.tree.*;


public class Main{

    public static void main(String[] args){

        new Main();
    }

    public Main(){

        frame = new JFrame("JTree Application");

        root = new DefaultMutableTreeNode("World");

        DefaultMutableTreeNode usa = new 
                           DefaultMutableTreeNode("USA");
        root.add(usa);
        DefaultMutableTreeNode id = new   
                           DefaultMutableTreeNode("Indonesia");
        root.add(id);

        DefaultMutableTreeNode ny = new 
                           DefaultMutableTreeNode("New York");
        usa.add(ny);
        DefaultMutableTreeNode ca = new 
                           DefaultMutableTreeNode("California");
        usa.add(ca);

        DefaultMutableTreeNode jkt = new 
                           DefaultMutableTreeNode("Jakarta");
        id.add(jkt);
        DefaultMutableTreeNode dp = new 
                           DefaultMutableTreeNode("Denpasar");
        id.add(dp);

        model = new DefaultTreeModel(root);
        tData = new JTree(model);

        frame.add(new JScrollPane(tData));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }

    private JFrame frame;
    private JTree tData;  
    private DefaultTreeModel model;
    private DefaultMutableTreeNode root;

Share/Bookmark

Swing - JList Event

Here's a code on how to use JList with event:

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;


public class Main{

    public static void main(String[] args){

        new Main();
    }
  
    public Main(){

        frame = new JFrame("JList Application");

        model = new DefaultListModel();
        model.addElement("Jakarta");
        model.addElement("Denpasar");
        model.addElement("Manila");
        model.addElement("Sydney");

        lCities = new JList(lCities);
        lCities.addListSelectionListener(new
                                     ListSelectionListener(){
            public void valueChanged(ListSelectionEvent lse){
                if(lse.getValueIsAdjusting){
                    Object obj = lCities.getSelectedValue();
                    JOptionPane.showMessageDialog(frame,
                                               obj.toString);
            }
        });

        frame.add(new JScrollPane(lCities));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
   
    private JFrame frame;
    private Jlist lCities;
    private DefaultListModel model;
}
Share/Bookmark

Swing - JList Using Model

Here's a code on how to use JList with model;

import javax.swing.*;
import java.awt.*;


public class Main{

    public static void main(String[] args){

        new Main();
    }
  
    public Main(){

        frame = new JFrame("JList Application");

        model = new DefaultListModel();
        model.addElement("Jakarta");
        model.addElement("Denpasar");
        model.addElement("Manila");
        model.addElement("Sydney");

        lCities = new JList(lCities);

        frame.add(new JScrollPane(lCities));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
   
    private JFrame frame;
    private Jlist lCities;
    private DefaultListModel model;
}
Share/Bookmark

Sunday, December 19, 2010

Swing - JComboBox Event

To implement event in JComboBox, use:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Main{

    public static void main(String[] args){


        new Main();
    }

    public Main(){

        frame = new JFrame("JComboBox Event");

        model = new DefaultComboBoxModel();
        model.addElement("Lady Gaga");
        model.addElement("Aura Kasih");
        model.addElement("Luna Maya");

        cbData = new JComboBox(model);
        cbData.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                JComboBox cb = (JComboBox) ae.getSource();
                Object obj = cb.getSelectedItem();
                JOptionPane.showMessageDialog(frame,
                         "You selected: "+obj.toString());
            }
        });

        frame.add(cbData);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private JFrame frame;
    private JComboBox cbData;
    private DefaultComboBoxModel model;
}
Share/Bookmark

Swing - JComboBox Model

Constructor in JComboBox can be inputed by array ob object, vector or combo box model.

import javax.swing.*;
import java.awt.*;

public class Main{
   
    public static void main(String[] args){

        new Main();
    }

    public Main(){

        frame = new JFrame("Combo Box Application");

        model = new DefaultComboBoxModel();
        model.addElement("Lady Gaga");
        model.addElement("Luna Maya");
        model.addElement("Aura Kasih");

        cbData = new JComboBox(model);

        frame.add(cbData);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private JFrame frame;
    private JComboBox cbData;
    private DefaultComboBoxModel model;
}

Share/Bookmark

Java Event

This code will show on how to make Java Event:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main{

    public static void main(String[] args){

         new Main();
    }

    public Main(){

        frame = new JFrame("Event Application");
        bDialog = new JButton("Show Dialog");

        bDialog.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae){
                JOptionPane.showMessageDialog( frame,
                                         "Holla World");
            }
        });

        frame.add(bDialog);
        frame.setLayout(new FlowLayout(FlowLayout.CENTER));
        frame.setSize(400, 300);    
        frame.setLocation(200, 100); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    private JFrame frame;
    private JButton bDialog;
}
Share/Bookmark

Java Desktop Application

This is the most simple desktop application using Java:

import javax.swing.JFrame;

public class Main{

    public static void main(String[] args){
    
        JFrame frame = new JFrame("A Desktop Application");
        frame.setSize(400, 300);
        frame.setLocation(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
 
Share/Bookmark

Saturday, December 18, 2010

Java IO - Rendering Binary File

To render binary file through remote server like http, use:

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;

public class Main{

    public static void main(String[] args){
        try{
            URL image = new URL("http://www.google.co.id/images/"
                               +"srpr/nav_logo27.png");
            URLConnection conn = image.openConnection();
            InputStream input = conn.getInputStream();

            int biner = 0;
            while(biner!=-1){
               biner = input.read();
               System.out.print((char)biner);
           }catch(Exception ex){
               ex.printStackTrace();
           }
        }
    }
}

        
Share/Bookmark

Java Net - Rendering A Web Page

To render a web page, use:

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{

    public static void main(String[] args){
        try{
            URL google = new URL("http://google.com");
            URLConnection conn = google.openConnection();
            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(input);
            String text = "";
            while(text!=null){
                text = reader.readLine();
                System.out.println(text);
            }
        }
    }
}
     

Share/Bookmark

Java IO - Reading A Binary File

To read a binary file, use:

import java.io.*;

public class Main{

    public static void main(String[] args){
        try{
            File file = new File("D://lady_gaga.jpg");
            FileInputStream input = new
                                    FileInputStream(file);       
            int biner = 0;
            while(biner!=-1){            // -1 is end of file
                biner = input.read();
                System.out.print(biner);
            }
        }catch(Exception ex){
            ex.printStackTrace();
    }
}
Share/Bookmark

Java IO - Reading A Text File

Reading one line from text file:

import java.io.*;

public class Main{

    public static void main(String[] args){
        try{
            File file = new File("D://data.txt");
            FileReader reader = new FileReader(file);
                                //FileNotFoundException
            BufferedReader textReader = new
                                        BufferedReader(reader);
            String text = textReader.readLine(); // IOException
            System.out.println(text);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }   
}



To Read A whole text, use:

import java.io.*;

public class Main{

    public static void main(String[] args){
        try{
            File file = new File("D://data.txt");
            FileReader reader = new FileReader(file);
            BufferedReader bReader = new BufferedReader(reader);

            String text = "";
            while(text!=null){
                text = bReader.readLine();
                System.out.println(text);
            }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}




Share/Bookmark