Set JTextField selection color

Complete source code below will show you how to set JTextField selection color.

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


import javax.swing.*;

import java.awt.*;

public class SetJTextFieldSelectionColor
{
public static void main(String[]args)
{
JTextField jtf=new JTextField("Select this text");

//Color that will use as JTextField's selection color
//It is base on RGB value
//Red=255
//Green=0
//Blue=0
//You can use color picker at above to get RGB value
Color selectionColor=new Color(255,0,0);

//Set JTextField selection color
jtf.setSelectionColor(selectionColor);

JFrame myWindow=new JFrame("Set JTextField selection color");

myWindow.add(jtf);
myWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myWindow.setSize(400,50);
myWindow.setLocationRelativeTo(null);
myWindow.setVisible(true);
}
}


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

Set JTextField caret color

Complete source code below will show you, how to set JTextField caret color.

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


import javax.swing.*;

import java.awt.*;

public class SetJTextFieldCaretColor
{
public static void main(String[]args)
{
JTextField jtf=new JTextField(20);
jtf.setFont(new Font("Verdana",Font.BOLD,34));

JFrame myFrame=new JFrame("Set JTextField caret color");

//Set caret color base on RGB value
//R=255
//G=0
//B=0
//You can get RGB value from color picker at above
jtf.setCaretColor(new Color(255,0,0));

myFrame.setLayout(new FlowLayout());
myFrame.add(jtf);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


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

JTextField caret


Java : Transparent JTextField

Complete source code below, will show you how to create a transparent text field in Java.

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


import javax.swing.*;

import java.awt.*;

public class TransparentJTextField
{
public static void main(String[]args)
{
JFrame myFrame=new JFrame("Transparent JTextField");

JTextField test=new JTextField(10);

//MAKE JTextField TRANSPARENT
test.setOpaque(false);

myFrame.setLayout(new FlowLayout());
myFrame.add(test);
myFrame.setSize(400,100);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


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

For loop act as while loop


public class ForActAsWhile
{
public static void main(String[]args)
{
/*
* while(true)
* {
* System.out.println("HELLO WORLD");
* }
*
*
*Now we will create for loop
*that will act like while loop at above
*/

/*
*For loop at below is an unstopable loop.
*It will act like while loop at above.
*It has no i++ at last.So (i) value is always 0.
*And 0 is less then 1.This condition is always true.
*NOTE : <<DON'T EXECUTE IT!!>>.This is only for learning.
*/
for(int i=0;i<1;)
{
System.out.println("HELLO WORLD");
}
}
}

Java play wav file

Complete source code below will show you, how to play wav file in java.

Click here to download Star-Wars-4118.wav that will use with this code. Place this wav file same location with this source code file.

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


import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

public class PlayWavFile
{
public static void main(String[]args)
{
String filename="Star-Wars-4118.wav";

int EXTERNAL_BUFFER_SIZE = 524288;

File soundFile = new File(filename);

if (!soundFile.exists())
{
System.err.println("Wave file not found: " + filename);
return;
}

AudioInputStream audioInputStream = null;
try
{
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
}
catch(Exception e)
{
e.printStackTrace();
return;
}

AudioFormat format = audioInputStream.getFormat();

SourceDataLine auline = null;

//Describe a desired line
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

try
{
auline = (SourceDataLine) AudioSystem.getLine(info);

//Opens the line with the specified format,
//causing the line to acquire any required
//system resources and become operational.
auline.open(format);
}
catch(Exception e)
{
e.printStackTrace();
return;
}

//Allows a line to engage in data I/O
auline.start();

int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

try
{
while (nBytesRead != -1)
{
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
{
//Writes audio data to the mixer via this source data line
//NOTE : A mixer is an audio device with one or more lines
auline.write(abData, 0, nBytesRead);
}
}
}
catch(Exception e)
{
e.printStackTrace();
return;
}
finally
{
//Drains queued data from the line
//by continuing data I/O until the
//data line's internal buffer has been emptied
auline.drain();

//Closes the line, indicating that any system
//resources in use by the line can be released
auline.close();
}
}
}


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

Java invisible cursor

Complete source code below will show you, how to create invisible cursor in java.

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


import java.awt.Toolkit;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Image;

import javax.swing.JFrame;

public class MakeCursorInvisible
{
//Create an empty byte array
byte[]imageByte=new byte[0];

Cursor myCursor;
Point myPoint=new Point(0,0);

//Create image for cursor using empty array
Image cursorImage=Toolkit.getDefaultToolkit().createImage(imageByte);

public MakeCursorInvisible()
{
//Create cursor
myCursor=Toolkit.getDefaultToolkit().createCustomCursor(cursorImage,myPoint,"cursor");

JFrame frame=new JFrame("Put your cursor into me");

//Set cursor for JFrame using created cursor(myCursor)
frame.setCursor(myCursor);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
MakeCursorInvisible mci=new MakeCursorInvisible();
}
}


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

Set file to Read-only in java

Complete source code below will show you, how to set a file to Read-only in java.

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


import java.io.File;

public class SetFileReadOnly
{
public static void main(String[]args)
{
File myFile=new File("MyFile.txt");

//Set MyFile.txt to Read-only
myFile.setReadOnly();
}
}


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

Click here to download MyFile.txt

Note : Put MyFile.txt with java file above at the same location.

Java palindrome checker

What is palindrome ?
-Palindrome is a word or sequence that reads the same backwards as forwards.


For example :
MADAM

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


import java.util.Scanner;
import java.util.StringTokenizer;

public class JavaPalindromeChecker
{
public static void main(String[]args)
{
System.out.println("Put your text below");
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
StringTokenizer st=new StringTokenizer(a);
String b="";
while(st.hasMoreTokens())
{
b=b+st.nextToken();
}
int stringLength=b.length();
String benchMark="Palindrome";
if(stringLength%2!=0)
{
int c=((stringLength-1)/2);
for(int i=0;i<c;i++)
{
char before=b.charAt(c-(i+1));
char after=b.charAt(c+(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
else
{
int c=stringLength/2;
for(int i=0;i<c;i++)
{
char before=b.charAt(i);
char after=b.charAt(stringLength-(i+1));
if(before!=after)
{
benchMark=new String("Not Palindrome");
}
}
}
System.out.println(benchMark);
}
}


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

Java transparent splash screen



Sample


**********************************************************************
TRANSPARENT IMAGE THAT WILL BE USE IN SOURCE CODE (batman.png)
**********************************************************************
Note : Make sure batman.png locate at same location with java file below.You can use other location, but for this simple tutorial, we will use same location.





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

import javax.swing.JWindow;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

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


public class TransparentSplashScreen extends JWindow
{
//Get transparent image that will be use as splash screen image.
Image bi=Toolkit.getDefaultToolkit().getImage("batman.png");

ImageIcon ii=new ImageIcon(bi);

public TransparentSplashScreen()
{
try
{
setSize(ii.getIconWidth(),ii.getIconHeight());
setLocationRelativeTo(null);
show();
Thread.sleep(10000);
dispose();
JOptionPane.showMessageDialog(null,"This program will exit !!!","<>",JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception exception)
{
exception.printStackTrace();
}
}

//Paint transparent image onto JWindow
public void paint(Graphics g)
{
g.drawImage(bi,0,0,this);
}

public static void main(String[]args)
{
TransparentSplashScreen tss=new TransparentSplashScreen();
}
}


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

RELAXING NATURE VIDEO