Java JSpinner & Its Definition

What is Java JSpinner

Okay lads, today we’re talking about Spinner. But hey! it’s not a fidget spinner that we’re talking. Because It is a Java JSpinner and JSpinner allows the users to select a number or an object value from an already sequence using single line input.

The Syntax :
public class JSpinner extends JComponent implements Accessible  

First of all here are common constructor of Java JSpinner:

  • JSpinner() : This class is used to construct a spinner with an Integer SpinnerNumberModel and in addition of an initial value of 0 and with no minimum or maximum limits.
  • JSpinner(SpinnerModel model) : This is another one of Java JSpinner that is used to construct a spinner for a given model.
Especially relevant here are common methods ofmJava JSpinner :

 

  • void addChangeListener(ChangeListener listener) : This method is used to add another listener to the list which will be notified each time a change to the model occurs.
  • Object getValue() :While this method is used to return the current value of the model.
Finally It’s Exmpl time! :
 
import javax.swing.*;    
public class SpinnerExample {  
    public static void main(String[] args) {    
    JFrame f=new JFrame(“Not Fidget Spinner”);    
    SpinnerModel value =  
             new SpinnerNumberModel(10, //initial value  
                0, //minimum value  
                20, //maximum value  
                2); //step  
    JSpinner spinner = new JSpinner(value);   
            spinner.setBounds(100,100,50,30);    
            f.add(spinner);    
            f.setSize(300,300);    
            f.setLayout(null);    
            f.setVisible(true);     
}  
}  
 
And This will output :

ForEach() Method : Lambda Expressions & Method Reference

Definition of ForEach() Method

ForEach() Method

Due to Java 8 new features, that provides us java coders a method named forEach() to iterate the elements. forEach() method is defined in iterable and Stream interface. The collection classes which is the one that extends the iterable interface can use forEach loop to iterate elements. As a result you can pass lambda expressions as an argument in this method because it takes a single parameter which is a functional interface.

Continue reading ForEach() Method : Lambda Expressions & Method Reference

Java Split Method

What Is Java Split Method?

programming Java, Java Split Method
Programming Java

There’s a method in java that is used to “split” string into pieces, the split is based on a given regular expressions and a return char array. There are 2 type of syntax that can be used for Java Split Method, (for a fun fact java split method is available since java 1.4).

Continue reading Java Split Method

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 :


CheckBox ft. ItemListener

Today we’re going to continue the CheckBox class, for today I have a code that uses ItemListener for the checkbox itself. Before this check out the CheckBox article. Let’s begin cause this’ll be a really short post.

//1st import
import java.awt.*; 
import java.awt.event.*;

//2nd create the constructor of the objects and all of the methods
import javax.swing.JFrame; 
public class CheckboxExmpl2 extends JFrame

     CheckboxExmpl2(){ 
        JFrame f= new JFrame(“CheckBox Example Item Listener”); 
        final Label label = new Label();         
        label.setAlignment(Label.CENTER); 
        label.setSize(400,100); 
        Checkbox checkbox1 = new Checkbox(“Java 7”); 
        checkbox1.setBounds(100,100, 50,50); 
        Checkbox checkbox2 = new Checkbox(“Java 8”); 
        checkbox2.setBounds(100,150, 50,50); 
        f.add(checkbox1); f.add(checkbox2); f.add(label);
    //creates the 1st ItemListener
        checkbox1.addItemListener(new ItemListener() { 
             public void itemStateChanged(ItemEvent e) {              
                label.setText(“Java 7 Checkbox: ”  
                + (e.getStateChange()==1?”checked”:”unchecked”)); 
             } 
          });


   //creates the 2nd ItemListener
        checkbox2.addItemListener(new ItemListener() { 
             public void itemStateChanged(ItemEvent e) {              
                label.setText(“Java 8 Checkbox: ”  
                + (e.getStateChange()==1?”checked”:”unchecked”)); 
             } 
          }); 
        f.setSize(300,300); 
        f.setLayout(null); 
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

//3rd call and run the object
public static void main(String args[]) 

    new CheckboxExmpl2(); 

}

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 :

DateFormat Format Codes

When we want to specify a time format in Date class in Java, we need to use a time pattern string. This pattern will reserve all ASCII letters ass pattern letters, which you can see in a list down below :

Character     Description           Example
G               Era designator             AD
y             Year in four digits           2001
M              Month in year             July or 07
d                 Day in month              10
h       Hour in A.M./P.M. (1~12)   12
H         Hour in day (0~23)            22
m              Minute in hour             30
s             Second in minute            55
S                Millisecond                 234
E                Day in week               Tuesday
D                Day in year                360
F        Day of week in month        2 (second Wed. in July)
w               Week in year               40
W             Week in month             1
a           A.M./P.M. marker           PM
k          Hour in day (1~24)            24
K       Hour in A.M./P.M. (0~11)   10
z                  Time zone                   Eastern Standard Time
‘             Escape for text                 Delimiter
”               Single quote                   `

Date and Time Java

In java there are a class in java.util package that encapsulate the current date and time. That class is called the Date class, and the Date class supports 2 constructor as i’ll write down below :

  1. Date() : this constructors initialize the object with the current Date and Time.
  2. Date (long milisec) : this constructor accept an arguments that equals the number of miliseconds that have elapsed sinced January 1, 1970 00:00:00 GMT (midnight).

Besides that 2 main constructor, it has many more methods inside it and i’ll tell you about 6 of it which can be seen below :

  • boolean after(Date date) : This will returns true if the current Date object contains a date that is later than the one specified by date, otherwise if it doesn’t, it returns false.
  • boolean before(Date date) : This will returns true if the current Date object contains a date the is  earlier that the one specified by date, otherwise if it doesn’t, it’ll return false.
  • Object clone() : The current Date object will be duplicated. 
  • int compareTo(Date date):Compares the value of the Current object with that of date. Returns 0 if the values are equal. Returns a negative value if the invoking object is earlier than date. Returns a positive value if the invoking object is later than date.
  • int hashCode( ): Returns a hash code for the current Date object.
  • String toString( ) : Converts the Current Date object into a string and returns the result.

Get the Current Date and Time :

This is really easy I could say, for you to get the current date and time. You just need to create a simple Date object and use toString() to get the result of the current date and time, it works as below :

import java.util.Date;
pisplay time and date using toString()
      Syublic class DateandTimeDemo {

   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // System.out.println(date.toString());

   }
}

This will output whatever the date and time you’re currently have.
for me it outputs :

Mon Feb 06 21:16:17 ICT 2017

Conclusions

  • Date class encapsulate the current Date and Time.
  • There are 2 constructor :
    • Date();
    • Date(long milisec);

MouseMotion Listener

MouseMotion listener is a class extension out of Java Adapter class, so since I’ve explain about Adapter class, So for today post i’m just going to show the example and the output of the MouseMotion Listener. By the way mouseMotion listener let your program to listen and act to your mouse movement.

Enough for the talk, it’s Exmpl time! :

//1st import
    import java.awt.*; 
    import java.awt.event.*; 
//2nd methods and constructor

public class MouseMotionAdapterExmpl extends MouseMotionAdapter{ 
        Frame f; 
        MouseMotionAdapterExmpl(){ 
            f=new Frame(“Mouse Motion Adapter”); 
            f.addMouseMotionListener(this); 
             
            f.setSize(400,400); 
            f.setLayout(null); 
            f.setVisible(true); 
        } 
    public void mouseDragged(MouseEvent e) { 
        Graphics g=f.getGraphics(); 
        g.setColor(Color.ORANGE); 
        g.fillOval(e.getX(),e.getY(),20,20); 
    } 
//3rd call and run the methods
    public static void main(String[] args) { 
        new MouseMotionAdapterExmpl(); 
    } 
    } 

Here is the result :

MouseMotion Listener Extension From Adapter Class
MouseMotion

Java Adapter Class

Java has a class named, Adapter class that provides an implementation of the listener interface. If your code inherit an adapter class, then you will not be forced to provide the implementation of all the methods. So adapter class saves code.
You can found adapter class in java.awt.event, java.awt.dnd and javax.swing. event packages. Here, look at a list of methods from Java Adapter class :

Java.awt.event :

  • WindowAdapter   : WindowListener
  • KeyAdapter   : KeyListener
  • MouseAdapter  :  MouseListener
  • MouseMotionAdapter :  MouseMotionListener
  • etc.

Javax.swing.event :

  • DragSourceAdapter  :  DragSourceListener
  • DragTargetAdapter   : DragTargetListener

 Java.awt.dnd :

  • MouseInputAdapter  :  MouseInputListener
  • InternalFrameAdapter  :  InternalFrameListener 

Here is an Exmpl of 1 method that Adapter class have :
 Window Adapter  :

//1st import
    import java.awt.*; 
    import java.awt.event.*; 
//2nd create methods and constructor
    public class AdapterExample{ 
        Frame f; 
        AdapterExample(){ 
            f=new Frame(“Window Adapter Exmpl”); 
            f.addWindowListener(new WindowAdapter(){ 
                public void windowClosing(WindowEvent e) { 
                    f.dispose(); 
                } 
            }); 
             
            f.setSize(300,300); 
            f.setLayout(null); 
            f.setVisible(true); 
        } 
//3rd call and run the methods
    public static void main(String[] args) { 
        new AdapterExample(); 
    } 
    } 

Conclusions

  • Adapter class saves code.
  • java.awt.event.
  • java.awt.dnd.
  • javax.swing.event