Showing posts with label set. Show all posts
Showing posts with label set. Show all posts

Set JOptionPane background color

After i make some review over internet and java forum lastly i understand how to change JOptionPane background color.You can try compile and execute simple code below and see it's result.

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

import javax.swing.JOptionPane;
import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

public class SetJOptionPaneBackgroundColor
{
public static void main(String[]args)
{
UIManager uim=new UIManager();
uim.put("OptionPane.background",new ColorUIResource(255,0,0));
uim.put("Panel.background",new ColorUIResource(255,0,0));

JOptionPane.showMessageDialog(null,"Hello world","HELLO WORLD",JOptionPane.INFORMATION_MESSAGE);
}
}

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

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
****************************************************************

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.

Set JFrame decoration to MetalLookAndFeel decoration



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


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

public class SetJFrameDecorationToMetalLookAndFeel
{
public static void main(String[]args)
{
//Set JFrame decoration to current look and feel decoration
//You can try to make this line as comment and
//after that try compile and execute it to see it's differential
JFrame.setDefaultLookAndFeelDecorated(true);

JButton myButton=new JButton("Click On Me");

JFrame myFrame=new JFrame("Set JFrame window decoration to current look and feel");
myFrame.setLayout(new FlowLayout());

myFrame.add(myButton);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setSize(400,400);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}
}


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

Set JSlider tick color

Complete source code below will show you, how to set JSlider tick color. I have tried to find method in JSlider class to set JSlider tick color...but, i can't find it. So i tried to create UI Delegate only for SliderUI that change JSlider tick color. You can try to compile both source code below. Make sure both java file below locate at the same location. JSlider's tick color is set in MyNewMetalSliderUI.java.You can try to change it's color follow what color that you want base on RGB value. After you compile SetJSliderTickColor.java and MyNewMetalSliderUI.java, you try execute SetJSliderTickColor.java. This is because, main method contain in this file.Oooo...before i forget, it's only handle horizontal slider. If you want to make it can use for vertical slider, you can try overwrite method paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) and paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) in MetalSliderUI class. You can review it in Java SE API for MetalSliderUI class.

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


import javax.swing.*;

import java.awt.*;

import javax.swing.plaf.ColorUIResource;

import javax.swing.plaf.metal.MetalSliderUI;

import javax.swing.plaf.ComponentUI;

public class MyNewMetalSliderUI extends MetalSliderUI
{
// Create our own slider UI
public static ComponentUI createUI(JComponent c )
{
return new MyNewMetalSliderUI();
}

//*******************HORIZONTAL MAJOR TICK*******************
public void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=255,GREEN=0,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(255,0,0);

//Set color that will use to draw MAJOR TICK using created color
g.setColor(majorTickColor);

//Draw MAJOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height);
}
}
//*******************HORIZONTAL MAJOR TICK*******************

//*******************HORIZONTAL MINOR TICK*******************
public void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;

if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=12,GREEN=255,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(12,255,0);

//Set color that will use to draw MINOR TICK using created color
g.setColor(majorTickColor);

//Draw MINOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height/2);
}
}
//*******************HORIZONTAL MINOR TICK*******************
}


**********************************************************************
JUST COMPILE
**********************************************************************

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

import javax.swing.*;

import java.awt.*;

public class SetJSliderTickColor
{
public SetJSliderTickColor()
{
//Create slider using JSlider
JSlider mySlider=new JSlider();

//Set major tick spacing to 10
mySlider.setMajorTickSpacing(10);

//Set minor tick spacing to 1
mySlider.setMinorTickSpacing(1);

//Make slider's numbers visible
mySlider.setPaintLabels(true);

//Make slider's ticks visible
mySlider.setPaintTicks(true);

//Create a window using JFrame with title ( Set JSlider tick color )
JFrame frame=new JFrame("Set JSlider tick color");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add created JSlider into JFrame
frame.add(mySlider);

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

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

//Set JFrame locate at center of screen
frame.setLocationRelativeTo(null);

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

public static void main(String[]args)
{
/***********Set our own UI Delegate for Slider*************/
UIManager uim=new UIManager();

//MyNewMetalSliderUI contain settings that use to change tick color
uim.put("SliderUI","MyNewMetalSliderUI");
/***********Set our own UI Delegate for Slider*************/

SetJSliderTickColor sjstc=new SetJSliderTickColor();
}
}


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

Set JOptionPane warning icon

Complete source code below will show you, how to set warning icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneWarningIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Warning Icon-text in message box
//WARNING-JOptionPane title
//JOptionPane.WARNING_MESSAGE-Set warning icon that will be show in message box
JOptionPane.showMessageDialog(null,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE);
}
}


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

Set JOptionPane error icon

Complete source code below will show you, how to set error icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneErrorIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Error Icon-text in message box
//ERROR-JOptionPane title
//JOptionPane.ERROR_MESSAGE-Set error icon that will be show in message box
JOptionPane.showMessageDialog(null,"Error Icon","ERROR",JOptionPane.ERROR_MESSAGE);
}
}


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

Set JOptionPane information icon

Complete source code below will show you, how to set information icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneInformationIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Information Icon-text in message box
//INFORMATION-JOptionPane title
//JOptionPane.INFORMATION_MESSAGE-Set information icon that will be show in message box
JOptionPane.showMessageDialog(null,"Information Icon","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
}
}


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

Set JOptionPane question icon

Complete source code below will show you, how to set question icon in JOptionPane.

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


import javax.swing.*;

public class SetJOptionPaneQuestionIcon
{
public static void main(String[]args)
{
//null-Message Box appear in no parent component
//Question Icon-text in message box
//QUESTION-JOptionPane title
//JOptionPane.QUESTION_MESSAGE-Set question icon that will be show in message box
JOptionPane.showMessageDialog(null,"Question Icon","QUESTION",JOptionPane.QUESTION_MESSAGE);
}
}


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

Set ToolTipText background color

Complete source code below, will show you how to set ToolTipText background color.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Color;

public class SetToolTipTextBackgroundColor
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with text ( Set ToolTipText background color )
JFrame frame=new JFrame("Set ToolTipText background color");

public SetToolTipTextBackgroundColor()
{
//Create a color base on RGB value
//You can get your color value base on RGB using Color picker at above
//R=204 G=0 B=153
Color backgroundColor=new Color(204,0,153);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext background color using created Color
uim.put("ToolTip.background",backgroundColor);

//Set tooltiptext for created button
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextBackgroundColor stttbc=new SetToolTipTextBackgroundColor();
}
}


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

Set ToolTipText text font

Complete source code below, will show you how to set ToolTipText text font.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Font;

public class SetToolTipTextFont
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with text ( Set ToolTipText text font )
JFrame frame=new JFrame("Set ToolTipText text font");

public SetToolTipTextFont()
{
//Create a font :
//Name : Verdana
//Style : Bold
//Size : 20
Font textFont=new Font("Verdana",Font.BOLD,20);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text font using created Font
uim.put("ToolTip.font",textFont);

//Set tooltiptext for created button
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextFont stttf=new SetToolTipTextFont();
}
}


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

Set ToolTipText text color

Complete source code below, will show you how to set ToolTipText text color.

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


import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.Color;

public class SetToolTipTextColor
{
/**
*Create a button with text ( Put your cursor on me )
*/
JButton button=new JButton("Put your cursor on me");

//Create a window using JFrame with title ( Set ToolTipText text color )
JFrame frame=new JFrame("Set ToolTipText text color");

public SetToolTipTextColor()
{
//Create a color base on RGB value
//You can get your color value base on RGB using Color picker at above
//R=255 G=255 B=255 is RGB value for white
Color textColor=new Color(255,255,255);

//Create UIManager
UIManager uim=new UIManager();

//Set tooltiptext text color using created Color
uim.put("ToolTip.foreground",textColor);

//Set tooltiptext for created button
//So, it's tooltiptext text color will be WHITE
button.setToolTipText("I am a button");

frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[]args)
{
SetToolTipTextColor stttc=new SetToolTipTextColor();
}
}


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

Set JOptionPane icons



Complete source code below will show you, how to set JOptionPane icons using any GIF image. Before you compile and execute complete source code below, you need to download all images below and place them with your java file that contain source code below.You also can use any GIF image that you want, but make sure it's location is right. This is because i suggest you place all GIF image at same location with source code file. So you don't need to put any file path in your source code. If you still want to use GIF image at other location, you can try step by step below.

For example, your GIF image with name MyImage.gif locate at C:\Documents and Settings...like below :

C:\Documents and Settings\MyImage.gif

So, you must put in your source code like this :
"C:\\Documents and Settings\\MyImage.gif"


WARNING.gif



QUESTION.gif



INFORMATION.gif



ERROR.gif


Click here for how to create all icons above

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


import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.LookAndFeel;

import java.awt.GridLayout;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ChangeJOptionPaneIcons implements ActionListener
{

JButton button=new JButton("JOptionPane with Error Icon");
JButton button2=new JButton("JOptionPane with Information Icon");
JButton button3=new JButton("JOptionPane with Question Icon");
JButton button4=new JButton("JOptionPane with Warning Icon");

JFrame frame=new JFrame("Change JOptionPane icons");

public ChangeJOptionPaneIcons()
{
//---START SETTING NEW ICON---
UIManager uim=new UIManager();

Object error=LookAndFeel.makeIcon(getClass(),"ERROR.gif");
Object information=LookAndFeel.makeIcon(getClass(),"INFORMATION.gif");
Object question=LookAndFeel.makeIcon(getClass(),"QUESTION.gif");
Object warning=LookAndFeel.makeIcon(getClass(),"WARNING.gif");

//Set new icon for Error message
uim.put("OptionPane.errorIcon",error);

//Set new icon for Information message
uim.put("OptionPane.informationIcon",information);

//Set new icon for Question message
uim.put("OptionPane.questionIcon",question);

//Set new icon for Warning message
uim.put("OptionPane.warningIcon",warning);
//---END SETTING NEW ICON---

button.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);

frame.setLayout(new GridLayout(4,1));
frame.add(button);
frame.add(button2);
frame.add(button3);
frame.add(button4);

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

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
JOptionPane.showMessageDialog(frame,"Error Icon","ERROR",JOptionPane.ERROR_MESSAGE);
}
else if(event.getSource()==button2)
{
JOptionPane.showMessageDialog(frame,"Information Icon","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
}
else if(event.getSource()==button3)
{
JOptionPane.showMessageDialog(frame,"Question Icon","QUESTION",JOptionPane.QUESTION_MESSAGE);
}
else if(event.getSource()==button4)
{
JOptionPane.showMessageDialog(frame,"Warning Icon","WARNING",JOptionPane.WARNING_MESSAGE);
}
}

public static void main(String[]args)
{
ChangeJOptionPaneIcons cjopi=new ChangeJOptionPaneIcons();
}
}
//END


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

Set JButton text color using UIManager

Complete source code below will show you, how to set JButton text color using UIManager. When you set JButton text color using this style, it will make all JButton in your program will have same text color. So, if you want to set only one text color for JButton, i suggest you click here.

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


import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

import javax.swing.*;

import java.awt.*;

public class SetJButtonForegroundColorUseUiManager
{
public static void main(String[]args)
{
/*
*Create color base on RGB color model
*that will use as JButton foreground color.
*Red=255
*Green=0
*Blue=0
*/
Color foregroundColor=new Color(255,0,0);

//Set JButton foreground color
UIManager.put("Button.foreground",new ColorUIResource(foregroundColor));

//Create a window using JFrame with title ( Set JButton foreground color using UIManager )
JFrame frame=new JFrame("Set JButton foreground color using UIManager");

//Create two button
JButton button1=new JButton("Button 1");
JButton button2=new JButton("Button 2");

//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());

//Add created button into frame
frame.add(button1);
frame.add(button2);

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

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

//Make JFrame locate at center
frame.setLocationRelativeTo(null);

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


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

Set JPanel layout using BoxLayout

Complete source code below will show you how to set JPanel layout using BoxLayout

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


import javax.swing.*;

import java.awt.*;

public class SetJPanelLayoutUsingBoxLayout
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JPanel layout using BoxLayout )
JFrame frame=new JFrame("Set JPanel layout using BoxLayout");

//Create a panel using JPanel
JPanel panel=new JPanel();

//Set JPanel layout using BoxLayout with vertical lay out
//You can try change to BoxLayout.X_AXIS to make components lay out in horizontal
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

//Create a button with text ( First button )
JButton button=new JButton("First button");

//Create a button with text ( Second button )
JButton button2=new JButton("Second button");

//Create a button with text ( Third button )
JButton button3=new JButton("Third button");

//Create a dimension with :
//Width : 10 pixels
//Height : 10 pixels
Dimension dim=new Dimension(10,10);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button into panel
panel.add(button);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button2 into panel
panel.add(button2);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

//Add button3 into panel
panel.add(button3);

//Create an invisible box with dimension size and add into created panel
//You can try eliminate this and see what happen
panel.add(Box.createRigidArea(dim));

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

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

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

//Make JFrame locate at center of screen
frame.setLocationRelativeTo(null);

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


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

Set JPanel layout using GridBagLayout

Source code below will show you, how to set JPanel layout using GridBagLayout.

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


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

public class SetJPanelLayoutToGridBagLayout extends JPanel
{
/**Note**
*When it say, "It will take 4 grid for it's width"
*it mean it will use 4 box in grid and it's direction is to right
*
*When it say, "It will take 4 grid for it's height"
*it mean it will use 4 box in grid and it's direction is to bottom
*/

//Create GridBigLayout object
GridBagLayout gbl=new GridBagLayout();

//Create GridBagConstraints object
GridBagConstraints gbc=new GridBagConstraints();

//Create a label using JLabel with text ( Name : )
JLabel label=new JLabel("Name :");

//Create a label using JLabel with text ( Age : )
JLabel label2=new JLabel("Age :");

//Create a text field using JTextField with numbers of column equal to 12
JTextField textField=new JTextField(12);

//Create a text field using JTextField with numbers of column equal to 3
JTextField textField2=new JTextField(3);

//Create a button with text ( OK )
JButton button=new JButton("OK");

//Create a button with text ( Cancel )
JButton button2=new JButton("Cancel");

//Create a box with vertical layout
Box box1=new Box(BoxLayout.Y_AXIS);

//Create a box with horizontal layout
Box box2=new Box(BoxLayout.X_AXIS);

//Create a box with horizontal layout
Box box3=new Box(BoxLayout.X_AXIS);

//Create a box with vertical layout
Box box4=new Box(BoxLayout.Y_AXIS);

//Create a window using JFrame with title ( Set JPanel layout using GridBagLayout )
JFrame frame=new JFrame("Set JPanel layout using GridBagLayout");

//Constructor
public SetJPanelLayoutToGridBagLayout()
{
//Make text in label allign to right
label.setHorizontalAlignment(SwingConstants.RIGHT);
label2.setHorizontalAlignment(SwingConstants.RIGHT);

//Set panel layout to GridBagLayout
setLayout(gbl);

//Field that will be use when display area is larger than component size.
//It mean when it has some space between component, component will
//enlarge to horizontal direction
gbc.fill=GridBagConstraints.HORIZONTAL;

/*
*Add label into panel with top left corner of the label at crossing between
*first horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label,1,1,4,1);

/*
*Add label2 into panel with top left corner of the
*label at crossing between
*third horizontal and first vertical line.
*It will take 4 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(label2,3,1,4,1);

/*
*Add textField into panel with top left corner of the text field at
*crossing between first horizontal and sixth vertical line
*It will take 12 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField,1,6,12,1);

/*
*Add textField2 into panel with top left corner of the text field at
*crossing between third horizontal and sixth vertical line
*It will take 3 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(textField2,3,6,3,1);

/*
*Add button into panel with top left corner of the button at
*crossing between fifth horizontal and fifth vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button,5,5,5,2);

/*
*Add button2 into panel with top left corner of the button at
*crossing between fifth horizontal and eleventh vertical line
*It will take 5 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(button2,5,11,5,2);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box1
*/
box1.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box1 into panel with top left corner of the box at
*crossing between first horizontal and fifth vertical line
*It will take 1 grid for it's width
*It will take 3 grid for it's height
*/
addComponent(box1,1,5,1,3);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box2
*/
box2.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box2 into panel with top left corner of the box at
*crossing between second horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box2,2,1,17,1);

/*
*Add a component that is invisible with 30 pixels width
*and 10 pixels height into box3
*/
box3.add(Box.createRigidArea(new Dimension(30,10)));

/*
*Add box3 into panel with top left corner of the box at
*crossing between fourth horizontal and first vertical line
*It will take 17 grid for it's width
*It will take 1 grid for it's height
*/
addComponent(box3,4,1,17,1);

/*
*Add a component that is invisible with 10 pixels width
*and 10 pixels height into box4
*/
box4.add(Box.createRigidArea(new Dimension(10,10)));

/*
*Add box4 into panel with top left corner of the box at
*crossing between fifth horizontal and tenth vertical line
*It will take 1 grid for it's width
*It will take 2 grid for it's height
*/
addComponent(box4,5,10,1,2);

//Add panel into JFrame
frame.add(this);

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

//SetJFrame size
frame.setSize(500,400);

//Make JFrame center on the screen
frame.setLocationRelativeTo(null);

//Disble JFrame from resizable
frame.setResizable(false);
frame.setVisible(true);
}

//Method addComponent
public void addComponent(Component component, int row, int column, int width, int height)
{
gbc.gridx=column;
gbc.gridy=row;
gbc.gridwidth=width;
gbc.gridheight=height;
gbl.setConstraints(component,gbc);
add(component);
}

//Main method
public static void main(String[]args)
{
SetJPanelLayoutToGridBagLayout sjpltgbl=new SetJPanelLayoutToGridBagLayout();
}
}


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

Set JTextField background image

Complete source code below will show you, how to set image background in a JTextField. You can download image (textFieldBackgroundImage.jpg) that use in source code at below.

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


import javax.swing.*;

import java.awt.*;

public class SetJTextFieldBackgroundImage extends JTextField
{
public SetJTextFieldBackgroundImage()
{
JFrame frame=new JFrame("Set JTextField background image");
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,65);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}

public void paint(Graphics g)
{
setOpaque(false);

//Image that will be set as JTextField background
//I suggest, image that will be use is blur...
//If image is locate at other location, you must put it's full location address.
//For example : C:\\Documents and Settings\\evergreen\\Desktop\\textFieldBackgroundImage.jpg
ImageIcon ii=new ImageIcon("textFieldBackgroundImage.jpg");
Image i=ii.getImage();
g.drawImage(i,0,0,this);
super.paint(g);
}

public static void main(String[]args)
{
SetJTextFieldBackgroundImage sjtfbi=new SetJTextFieldBackgroundImage();
}
}


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



textFieldBackgroundImage.jpg

Set JButton background color on click

Click here

Set JButton gradient color

Complete source code below will show you, how to change JButton default gradient color.



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


import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.UIManager;

import javax.swing.plaf.ColorUIResource;

public class SetJButtonGradientColor
{
public static void main(String[]args)
{
//Create UIManager object
UIManager manager=new UIManager();


//IMPORTANT : Key to know : "Button.gradient"

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above

//******************************************************
//Create linked list that will store all gradient information
//You can try to understand it by change it's value
LinkedList<Object> a=new LinkedList<Object>();
a.add(0.3);
a.add(0.3);

//First colour : R=2,G=208,B=206
a.add(new ColorUIResource(2,208,206));

//Second colour : R=136,G=255,B=254
a.add(new ColorUIResource(136,255,254));

//Third colour : R=0,G=142,B=140
a.add(new ColorUIResource(0,142,140));
//******************************************************

//Set Button.gradient key with new value
manager.put("Button.gradient",a);

//Create a button using JButton with text ( BUTTON )
JButton button=new JButton("BUTTON");

//Create a window using JFrame with title ( JButton gradient color )
JFrame frame=new JFrame("JButton gradient color");

//Add created button into JFrame
frame.add(button);

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

//Set JFrame size
frame.setSize(330,80);

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


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

RELAXING NATURE VIDEO