Applet : JApplet Class

As when in Swing we prefer it to the AWT. Finally, now we can use a JApplet that can have control of Swing. JApplet class does / of course extends Applet class.

Here is an Exmpl of JApplet :

    import java.applet.*; 
    import javax.swing.*; 
    import java.awt.event.*; 
    public class JAppletExmpl extends JApplet implements ActionListener{ 


    JButton b; 
    JTextField tf; 
    public void init(){ 
     
    tf=new JTextField(); 
    tf.setBounds(30,40,150,20); 
     
    b=new JButton(“Click”); 
    b.setBounds(80,150,70,40); 
     
    add(b);add(tf); 
    b.addActionListener(this); 
     
    setLayout(null); 
    } 
     
    public void actionPerformed(ActionEvent e){ 
    tf.setText(“Welcome”); 
    } 
    }  

In the example above, we’ve created all of the controls in an init() method because it is only invoked once.

Now we need to create the Html :

myapplet.html

<html>
    <body>
    <applet code=”JAppletExmpl.class” width=”300″ height=”300″>
    </applet>
    </body>
</html>

AWT List ft. ActionListener

Before seeing the inside of this post/article, it’s better you check on the previous post first which is Java AWT List. After you finish reading that one, you can comeback to this page. But hey… the choices are yours! so don’t mind it.

Okay, let’s just thought that you have known the basic of Java AWT List and now we’re going to add a few spice onto it which is ActionListener. We’re going to make the program outputs whatever objects we choose on the list by clicking an “output” button.

Let’s begin… The Exmpl !!! :
 *This Exmpl can be quit long because it involves ActionListener


//1st import  * we always uses JFrame
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    

//2nd creates the object constructor and methods
    public class ListExmpl  extends JFrame
    {
         /**
         *
         */
        private static final long serialVersionUID = -5024249607576251689L;
        //for this time we’re going to try adding a serialVersionUID for the program       


        ListExmpl(){
            JFrame f= new JFrame(“List ft.ActionListener”);
            final Label label = new Label();        
            label.setAlignment(Label.CENTER);
            label.setSize(500,100);
            Button b=new Button(“Output!”);  //here we create the button
            b.setBounds(200,150,80,30);
            final List l1=new List(3, false);  //here we create the list
            l1.setBounds(100,100, 70,70);
            l1.add(“C”);
            l1.add(“C++”);
            l1.add(“Java”);
            final List l2=new List(3, true);
            l2.setBounds(100,200, 70,70);
            l2.add(“Turbo C++”);
            l2.add(“Spring”);
            l2.add(“Hibernate”);
            f.add(l1); f.add(l2); f.add(label); f.add(b);
            f.setSize(500,500);
            f.setLayout(null);
            f.setVisible(true);  

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            b.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {     
              String data = “Programming language Selected: “+l1.getItem(l1.getSelectedIndex());
              data += “, Framework Selected:”;
              for(String frame:l2.getSelectedItems()){
                       data += frame + ” “;
              }
              label.setText(data);
              }
             });        
    }  

//3rd call and run the objects
    public static void main(String args[])
    {
       new ListExmpl();
    }
    }  

This’ll output :

Java AWT List

A list in Java represents a list of text items, it could be 1 or more than 1. List really help us a lot, we (user) can choose either one of the item or multiple items. Java AWT List also inherits Component class.

Now see… the ancient.. Declaration of the class :

public class List extends Component implements ItemSelectable

That’s that for the theory I guess, and you know what happens after theory??
Yess!! Exmpl time! :


//1st import
     import java.awt.*;
    import javax.swing.JFrame;

//2nd create the object constructor and methods 
    public class ListExmpl extends JFrame 
    { 
         ListExmpl(){ 
            JFrame f= new JFrame(); 
            List l1=new List(5); 
            l1.setBounds(100,100, 90, 75); 
            l1.add(“You see : 1”); 
            l1.add(“You see : 2”); 
            l1.add(“You see : 3”); 
            l1.add(“You see : 4”); 
            l1.add(“You see : 1”);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(l1); 
            f.setSize(300, 300); 
            f.setLayout(null); 
            f.setVisible(true); 
         }  

//3rd call and run the object
    public static void main(String args[]) 
    { 
       new ListExmpl(); 
    } 
    }  

This’ll output :


Check The Box Boy! (Java CheckBox)

Checkbox class in java is used to, you know… create a checkbox. Check box basically is used to turn 1 or more option to true (on) or false (off). Clicking for either one of the checkbox changes the state of one of the option from “off” to “on” or from “on” to “Off”.

before we began, you need to know the class declaration :
  
public class Checkbox extends Component implements ItemSelectable

Let’s began, Exmpl time! :

 //1st import it

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


//2nd create methods and constructor   
    public class CheckboxExmpl extends JFrame
    { 
         CheckboxExmpl(){ 
           JFrame f= new JFrame(“Checkbox Exmpl”); 
            Checkbox checkbox1 = new Checkbox(“Java 7”); 
            checkbox1.setBounds(100,100, 50,50); 
            Checkbox checkbox2 = new Checkbox(“Java 8”, true); 
            checkbox2.setBounds(100,150, 50,50); 
            f.add(checkbox1); 
            f.add(checkbox2); 
            f.setSize(300,300); 
            f.setLayout(null); 
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }  

//call and run the object
    public static void main(String args[]) 
    { 
        new CheckboxExmpl(); 
    } 
    }  

This will output :

Simple Panely Panel Class

Panel in java.. probably is the simplest container class there is. It provides a space, which in that space the application can attach any other components. Panel inherits Container class.

Class Declaration :
public class Panel extends Container implements Accessible 

Done…
now It’s Exmpl time! :


// 1st it imports
    import java.awt.*;
// 2nd create the methods and constructor 
    public class PanelExample { 
         PanelExample() 
            { 
            Frame f= new Frame(“Panel Example”);   
            Panel panel=new Panel(); 
            panel.setBounds(40,80,200,200);   
            panel.setBackground(Color.blue); 
            Button b1=new Button(“Button 1”);    
            b1.setBounds(50,100,80,30);   
            b1.setBackground(Color.green);  
            Button b2=new Button(“Button 2”);  
            b2.setBounds(100,100,80,30);   
            b2.setBackground(Color.green);  
            panel.add(b1); panel.add(b2);
// 3rd sets the Frame condition.
            f.add(panel); 
            f.setSize(400,400);   
            f.setLayout(null);   
            f.setVisible(true);   
            }
// 4th call and run the methods and constructor / Application / Program
            public static void main(String args[]) 
            { 
            new PanelExample(); 
            } 
    } 

This’ll output :

Program… listen to Mouse! (MouseListener)

Yes… You can listen to your mouse, I mean not literally but.. you can make your program act as when you do something with you mouse, for example : make your program print out something when you click your mouse. Java MouseListener acts whenever the state of the mouse is changed. The MouseListener is notified agains MouseEvent. You can found MouseListener interface in a java.awt.event package. It has five methods.

Check these methods out :

  • public abstract void mouseClicked(MouseEvent e);  //when you click the mouse
  • public abstract void mouseEntered(MouseEvent e);  //when you entered the mouse / move
  • public abstract void mouseExited(MouseEvent e);  //when you exited the mouse / don’t move
  • public abstract void mousePressed(MouseEvent e);  //when you press / drag the mouse 
  • public abstract void mouseReleased(MouseEvent e);  //when you release the mouse  


 Anyway, that’s that for the theory, now you know what time it is !
It’s Exmpl time! :

//1st import
   import java.awt.*; 
    import java.awt.event.*;
//2nd create the Method/object and constructor of MouseListener 
    public class MouseListenerExmpl extends Frame implements MouseListener{ 
        MouseListenerExmpl(){ 
            addMouseListener(this); 
             
            setSize(300,300); 
            setLayout(null); 
            setVisible(true); 
        } 
        public void mouseClicked(MouseEvent e) { 
            Graphics g=getGraphics(); 
            g.setColor(Color.GREEN); 
            g.fillOval(e.getX(),e.getY(),30,30); 
        } 
        public void mouseEntered(MouseEvent e) {} 
        public void mouseExited(MouseEvent e) {} 
        public void mousePressed(MouseEvent e) {} 
        public void mouseReleased(MouseEvent e) {}           
//3rd call and run the method and constructor
    public static void main(String[] args) { 
        new MouseListenerExmpl(); 
    } 

and it’ll output this :

Java MenuBar : MainMenu and SubMenu

Ever wonder how to create a menu, just like in Mozilla Firefox and many other softwares? Well you can, 1 way is to create a MenuBar in java, not only the main menu but you can also create a subMenu inside the Menu. The object of the Menu class itself is a pull down menu component that is displayed on the menu bar. The Menu inherits the MenuItem class.

Menu and MenuItem class declaration :

public class Menu extends MenuItem implements MenuContainer

It’s Exmpl time :

1st import :

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

2nd framework :

class MenuExmpl extends JFrame{
 MenuExmpl(){
 } 
 public static void main(String args[]){
 }
}

3rd, Fill it! :


MenuExmpl(){ 
       
JFrame f= new JFrame(“Menu and MenuItem Example”); 
            MenuBar mb=new MenuBar();  //Creates menu object
               Menu menu=new Menu(“Menu”); //creates the main menu 
               Menu submenu=new Menu(“Preferences”);  //create the subMenu
            MenuItem i1=new MenuItem(“File”);  //create the MenuItem
            MenuItem i2=new MenuItem(“New”); //create the MenuItem 
            MenuItem i3=MenuExmpl(){ 
            JFrame f= new JFrame(“Menu and MenuItem Example”); 
               new MenuItem(“Settings”);  //create the MenuItem
            MenuItem i4=new MenuItem(“View”);  //create the MenuItem
            MenuItem i5=new MenuItem(“Windows”);  //create the MenuItem
               menu.add(i1);  //add item 1 to main Menu
               menu.add(i2);  //add item 2 to main Menu
               menu.add(i3);  //add item 3 to main Menu
            submenu.add(i4);  //add item 4 to sub Menu
            submenu.add(i5);  //add item 5 to sub Menu
               menu.add(submenu);  //add Sub Menu to Main Menu
               mb.add(menu); 
             f.setMenuBar(mb); 
             f.setSize(400,400); 
             f.setLayout(null); 
             f.setVisible(true); 

             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }  

public static void main(String args[]) 
    { 
    new MenuExmpl();  //call and run the menu object
    }  

if you put it together it will look as below :

import javax.swing.JFrame; 
    class MenuExmpl  extends JFrame
    {        
       
        MenuExmpl(){ 
        JFrame f= new JFrame(“Menu and MenuItem Example”); 

            MenuBar mb=new MenuBar(); 
               Menu menu=new Menu(“Menu”);
               Menu submenu=new Menu(“Preferences”); 
            MenuItem i1=new MenuItem(“File”); 
            MenuItem i2=new MenuItem(“New”);  
            MenuItem i3=MenuExmpl(){ 
            JFrame f= new JFrame(“Menu and MenuItem Example”); 
               new MenuItem(“Settings”); 
            MenuItem i4=new MenuItem(“View”);
            MenuItem i5=new MenuItem(“Windows”); 
               menu.add(i1); 
               menu.add(i2); 
               menu.add(i3); 
            submenu.add(i4);
            submenu.add(i5); 
               menu.add(submenu); 
               mb.add(menu); 
             f.setMenuBar(mb); 
             f.setSize(400,400); 
             f.setLayout(null); 
             f.setVisible(true);              

             f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    } 
    public static void main(String args[]) 
    { 
    new MenuExmpl(); 
    } 
    }

The result is this :

Java Choices Menu

Today we’ll do a popup menu, called choices. Choice is a popup menu that will show 1 or more choice to the user, and when the user select one it will be shown in the top menu. Choice inherits Component class.

Class Declaration

public class Choice extends Component implements ItemSelectable 

It’s Exmpl time! :


1st import

import java.awt.*;

2nd framework 

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

3rd, fill it !:

ChoiceExmpl(){
  Frame f= new Frame(); 
            Choice c=new Choice(); 
            c.setBounds(200,200, 80,80); 
            c.add(“Choice 1”); 
            c.add(“Choice 2”); 
            c.add(“Choice 3”); 
            c.add(“Choice 4”); 
            c.add(“Choice 5”); 
            f.add(c); 
            f.setSize(500,500); 
            f.setLayout(null); 
            f.setVisible(true); 

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

Then, put it all in.. this is all you’ve done :

    import java.awt.*; 
    public class ChoiceExmpl 
    { 
            ChoiceExmpl(){ 
            Frame f= new Frame(); 
            Choice c=new Choice(); 
            c.setBounds(200,200, 80,80); 
            c.add(“Choice 1”); 
            c.add(“Choice 2”); 
            c.add(“Choice 3”); 
            c.add(“Choice 4”); 
            c.add(“Choice 5”); 
            f.add(c); 
            f.setSize(500,500); 
            f.setLayout(null); 
            f.setVisible(true); 
         } 
    public static void main(String args[]) 
    { 
       new ChoiceExmpl(); 
    } 
    } 

The result is this :


Java TextField

Since we’re talking about textField a lot in our previous discussion so, today we’ll do textField itself. In this part I’m going to make a non-actionlistener textField Okay let’s get goin ..

textField is a class that are built for text component that will allows the user an editing of a single line text, the textField itself Inherits TextComponent Class. (public class TextField extends TextComponent).

okay now, let’s get coding :


1st Import :

import java.awt.*;

2nd framework :

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

3nd, fill it! :

public static void main(String args[]){
  Frame f= new Frame(“Example of TextField”);   
  TextField t1,t2;   
   t1=new TextField(“textField textField textField”);   
   t1.setBounds(50,100200,30);   
   t2=new TextField(“TextField Java AWT”);   
   t2.setBounds(50,150200,30);   
  f.add(t1); f.add(t2);   
   f.setSize(400,400);   
  f.setLayout(null);   
  f.setVisible(true);
}

Here is what you get :

import java.awt.*;

class textFieldExmpl{
   public static void main(String args[]) {
    Frame f= new Frame(“Example of TextField”); //creates a new Frame with title  
    TextField t1,t2;   //creates a new TextField (t1, t2)
      t1=new TextField(“textField textField textField”); //set the data for t1  

      t1.setBounds(50,100200,30);   //set the position, size, bound
      t2=new TextField(“TextField Java AWT”);   //set the data for  t2
       t2.setBounds(50,150200,30);   //set the position, size, bound
    f.add(t1); f.add(t2);   //add t1 and t2 to the Frame
      f.setSize(400,400);   //set the size of the frame
      f.setLayout(null);   //set the layout to null
      f.setVisible(true); //set the visibility to true
 }
}
Here is  the result :

Java AWT TextField
TextField

Java JComboBox

Previously we have created a progress bar in java or loading bar you’re familiar with, which is build using the swing package, and today we’ll create another functionality out of swing package and specifically we’ll create a JComboBox, a JComboBox class is of course used to create a comboBox (drop-down list). In a time there are only one item you can select from the item list. The comboBox would look like this:

Creating a JComboBox in java
JComboBox


So let’s get it work from scratch, as always… Importing!!! packages :

 import javax.swing.*;  //because comboBox is created using swing package

After that public main method, constructor and public static void main method :

public class ComboBox(){
 comboBox(){
 }
  public static void main(String[] args){
 }
}

Okay, we’re now in the 1/4 process of the program, let’s continue, JFrame :

 public class ComboBox { 
    JFrame f; //creates the JFrame and instantiate it as f
    ComboBox(){ 
        f=new JFrame(“ComboBox-ing”); 

   //now invoke the JFrame with the tittle of “ComboBox-ing”     
        f.setLayout(null);  //set the layout to null
        f.setSize(400,500);  //set the size of the JFrame to be 400×500
        f.setVisible(true);  //set the visibility to true
    }  

Now that we have the frame for our program, let’s start implementing out JComboBox into the program:

comboBox(){ 
        f=new JFrame(“ComboBox-ing”); 
         
        String country[] = {“IOndonesia”, “Aus”, “U.S.A”, “Chyna”};//creates a string
        JComboBox jb = new JComboBox(country); 

   //create a JComboBox object instantiated to be jb
        jb.setBounds(50, 50, 90, 20); //set the x,y, width, height
        f.add(jb); //add jb to JFrame f
       
        f.setLayout(null); 
        f.setSize(400,500); 
        f.setVisible(true); 
         
    }  

And right that we need to run it, and where exactly? of course in the public statid void main method.

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

And all together will unite to this one code :

    import javax.swing.*; 
    public class ComboBox { 
    JFrame f; 
    ComboBox(){ 
        f=new JFrame(“ComboBox-ing”); 
         
        String country[] = {“IOndonesia”, “Aus”, “U.S.A”, “Chyna”};
        JComboBox jb = new JComboBox(country);
        jb.setBounds(50, 50, 90, 20);
        f.add(jb);
       
        f.setLayout(null); 
        f.setSize(400,500); 
        f.setVisible(true); 
         
    } 
    public static void main(String[] args) { 
        new ComboBox(); 
         
    } 
    }  

JComboBox in java
JComboBox