Get JFrame title

Source code below will show how to get JFrame title.

********************************************************************
COMPLETE SOURCE CODE FOR : GetJFrameTitle.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GetJFrameTitle
{
public static void main(String[]args)
{
//Create a JFrame with title ( My first window )
JFrame frame=new JFrame("My first window");

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(500,500);

//Make JFrame visible
frame.setVisible(true);

//Create a message box that show JFrame title
//Method getTitle() will get JFrame title
JOptionPane.showMessageDialog(frame,"MY NAME IS : "+frame.getTitle());
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Determine when JFrame moved

Source code below will show you, how to determine when a JFrame is moved

*****************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJFrameIsMoved.java
*****************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

public class DetermineWhenAJFrameIsMoved extends JFrame
{
//Create ComponentListener that will listen when JFrame is moving
ComponentListener cl=new ComponentAdapter()
{
public void componentMoved(ComponentEvent event)
{
//Location that will show is base on top left corner of JFrame
JOptionPane.showMessageDialog(null,"New location with :\n"+"X-coordinate : "+getLocation().getX()+"\n"+"Y-coordinate : "+getLocation().getY());
}
};

public DetermineWhenAJFrameIsMoved()
{
super("TRY MOVE ME");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);

//Add ComponentListener to JFrame
addComponentListener(cl);
}

public static void main(String[]args)
{
DetermineWhenAJFrameIsMoved dwajfir=new DetermineWhenAJFrameIsMoved();
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************

Determine when JFrame resized

Source code below will show you, how to determine when JFrame re sized.

***********************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJFrameIsResized.java
***********************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

public class DetermineWhenAJFrameIsResized extends JFrame
{
//Create ComponentListener that will listen when JFrame is resizing
ComponentListener cl=new ComponentAdapter()
{
public void componentResized(ComponentEvent event)
{
JOptionPane.showMessageDialog(null,"New size with :\n"+"WIDTH : "+getSize().width+"\n"+"HEIGHT : "+getSize().height);
}
};

public DetermineWhenAJFrameIsResized()
{
super("TRY RESIZE ME");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);

//Add ComponentListener to JFrame
addComponentListener(cl);
}

public static void main(String[]args)
{
DetermineWhenAJFrameIsResized dwajfir=new DetermineWhenAJFrameIsResized();
}
}


***********************************************************************
JUST COMPILE AND EXECUTE IT
***********************************************************************

Draw rectangle in JFrame

Source code below will show how to draw rectangle in JFrame.

**********************************************************************
COMPLETE SOURCE CODE FOR : DrawRectangleInJFrame.java
**********************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawRectangleInJFrame extends JFrame
{
public DrawRectangleInJFrame()
{
//Set JFrame title
super("Draw A Rectangle In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw rectangle outline
g.drawRect(50,50,300,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill rectangle with RED
g.fillRect(50,50,300,100);
}

public static void main(String[]args)
{
DrawRectangleInJFrame dlijf=new DrawRectangleInJFrame();
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************

Draw square in JFrame

Source code below will show you, how to draw a square in a JFrame. For your information, square has 4 equal side. It mean it has 4 side with same length.

************************************************************************
COMPLETE SOURCE CODE FOR : DrawSquareInJFrame.java
************************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawSquareInJFrame extends JFrame
{
public DrawSquareInJFrame()
{
//Set JFrame title
super("Draw A Square In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw square outline
g.drawRect(50,50,100,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill square with RED
g.fillRect(50,50,100,100);
}

public static void main(String[]args)
{
DrawSquareInJFrame dlijf=new DrawSquareInJFrame();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Draw oval in JFrame

Source code below will show you, how to draw an oval in a JFrame

*************************************************************
COMPLETE SOURCE CODE FOR : DrawOvalInJFrame.java
*************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawOvalInJFrame extends JFrame
{
public DrawOvalInJFrame()
{
//Set JFrame title
super("Draw An Oval In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw oval outline
g.drawOval(50,50,100,300);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill oval with RED
g.fillOval(50,50,100,300);
}

public static void main(String[]args)
{
DrawOvalInJFrame dlijf=new DrawOvalInJFrame();
}
}


*************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************

Draw circle in JFrame

Source code below will show you, how to draw a circle in a JFrame

*******************************************************************
COMPLETE SOURCE CODE FOR : DrawCircleInJFrame.java
*******************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;
import java.awt.Color;

public class DrawCircleInJFrame extends JFrame
{
public DrawCircleInJFrame()
{
//Set JFrame title
super("Draw A Circle In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw circle outline
g.drawOval(50,50,100,100);

//set color to RED
//So after this, if you draw anything, all of it's result will be RED
g.setColor(Color.RED);

//fill circle with RED
g.fillOval(50,50,100,100);
}

public static void main(String[]args)
{
DrawCircleInJFrame dlijf=new DrawCircleInJFrame();
}
}


*******************************************************************
JUST COMPILE AND EXECUTE IT
*******************************************************************

Draw line in JFrame

Source code below will draw a line from top left corner of a JFrame until bottom right corner of the JFrame.

************************************************************************
COMPLETE SOURCE CODE FOR : DrawLineInJFrame.java
************************************************************************


import javax.swing.JFrame;

import java.awt.Graphics;

public class DrawLineInJFrame extends JFrame
{
public DrawLineInJFrame()
{
//Set JFrame title
super("Draw A Line In JFrame");

//Set default close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
setSize(400,400);

//Make JFrame visible
setVisible(true);
}

public void paint(Graphics g)
{
super.paint(g);

//draw line in JFrame from top left corner until bottom right corner
g.drawLine(0,0,getSize().width,getSize().height);
}

public static void main(String[]args)
{
DrawLineInJFrame dlijf=new DrawLineInJFrame();
}
}


************************************************************************
JUST COMPILE AND EXECUTE IT
************************************************************************

Draw image in JFrame

Source code below will show you, how to draw image in JFrame. Image that will be use is drawImageIntoJFrame.jpg like below


drawImageIntoJFrame.jpg


********************************************************************
COMPLETE SOURCE CODE FOR : DrawImageOnJFrame.java
********************************************************************


import javax.swing.JFrame;
import javax.swing.ImageIcon;

import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;

public class DrawImageOnJFrame extends JFrame
{
Image imageToBeDraw;
ImageIcon ii;

public DrawImageOnJFrame()
{
//set JFrame title
super("Draw Image On JFrame");

//Get image. You must change image location follow to image location in your computer.
imageToBeDraw=Toolkit.getDefaultToolkit().getImage("C:\\Documents and Settings\\evergreen\\Desktop\\drawImageIntoJFrame.jpg");

//Create an ImageIcon object
ii=new ImageIcon(imageToBeDraw);

//set close operation for JFrame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//set JFrame size follow the image size
setSize(ii.getIconWidth(),ii.getIconHeight());

//make you JFrame cannot resizable
setResizable(false);

//make JFrame visible. So we can see it.
setVisible(true);
}

public void paint(Graphics g)
{
//This will draw drawImageIntoJFrame.jpg into JFrame
g.drawImage(imageToBeDraw,0,0,this);
}

public static void main(String[]args)
{
DrawImageOnJFrame diojf=new DrawImageOnJFrame();
}
}


********************************************************************
JUST COMPILE AND EXECUTE IT
********************************************************************

Add JPanel into JFrame

Source code below will show you, how to add a JPanel into JFrame.

**************************************************************************
COMPLETE SOURCE CODE FOR : AddJPanelIntoJFrame.java
**************************************************************************


import javax.swing.JPanel;
import javax.swing.JFrame;

public class AddJPanelIntoJFrame
{
public static void main(String[]args)
{
//Create a panel
JPanel panel=new JPanel();

//Create a window using JFrame with title ( Add JPanel into JFrame )
JFrame frame=new JFrame("Add JPanel into JFrame");

//add created panel into JFrame
frame.add(panel);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it
frame.setVisible(true);
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

RELAXING NATURE VIDEO