Tuesday, July 28, 2015

What are different types of entry points in Java?

Java provides mainly three entry points


public static void main(String args[])
public static void main(String []args)
public static void main(String... args)

or

static public void main(String args[])
static public void main(String []args)
static public void main(String... args)




How to Center a JFrame or JDialog?

Get the current size of the desktop and the size of current JFrame and divide by 2 the size difference.

Full example code

import javax.swing.*;
import java.awt.*;
public class MyWindow extends JFrame
{
    public MyWindow()
    {
       
        setSize(300,300);
       
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screen.getWidth()-getWidth()) /2);
        int y = (int) ((screen.getHeight()-getHeight()) /2);
        setLocation(x, y);
       
        setVisible(true);
    }
    public static void main(String args[])
    {
        new MyWindow();
    }
}

Sunday, July 26, 2015

How to change Icon of the Jframe

Copy and Paste any Image file into your current project folder and use getClass() method to get reference of current folder then use its getResource() method to lookup the image file.

It returns a type of URL class from java.net package.
Load the image into memory using ImageIcon class
Now use setIconImage()  method to set the Icon image


Example

import javax.swing.*;
import java.net.*;
public class MyWindow extends JFrame
{
    public MyWindow()
    {
        URL iconURL = getClass().getResource("icon.jpg");
        ImageIcon icon = new ImageIcon(iconURL);
        setIconImage(icon.getImage());
        setSize(300,300);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new MyWindow();
    }
}

How to make JFrame window Full Screen

Java provides getToolkit() method to get current environment details and then getScreentSize() of Toolkit class to get the current screen size. We can apply the size directly to the JFrame.

Solution 1

import javax.swing.*;
public class MyWindow extends JFrame
{
    public MyWindow()
    {
        setSize(getToolkit().getScreenSize());
        setVisible(true);
    }
    public static void main(String args[])
    {
        new MyWindow();
    }
}

Solution 2

import javax.swing.*;
import java.awt.*;
public class MyWindow extends JFrame 
{
    public MyWindow()
    {
        Toolkit t=getToolkit();
        Dimension d=t.getScreenSize();
        setSize(d);
        setVisible(true);
    }
    public static void main(String args[])
    {
        new MyWindow();
    }
}

Important Java Resources

There are few important URLs required for Java Development Tools and Technologies

Power of Java

http://www.java.com

For free software download 

http://www.oracle.com/technetwork/java/index.html

For Java related articles

http://www.oracle.com/technetwork/articles/java/index.html

Actual facts about Java by James Gosling in May 1996

http://www.oracle.com/technetwork/java/index-136113.html

Free Java Magzine

http://www.oracle.com/technetwork/java/javamagazine/index.html

Demos and Videos related with Java

https://www.youtube.com/user/java

Online Java compiler

https://ideone.com/

BlueJ IDE

http://www.java.com/en/java_in_action/bluej.jsp

Learn Java with Oracle Academy

https://academy.oracle.com/oa-web-overview.html

Java Documentation

https://docs.oracle.com/javase/tutorial/