Showing posts with label background. Show all posts
Showing posts with label background. 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 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 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 JTextArea background image

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

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


/**
*AddImageBackgroundInJTextArea will extends JTextArea class.
*So it also inherit all method from JTextArea
*It mean, we can use it directly.For example, setOpaque(false)..
**/
import javax.swing.*;

import java.awt.*;

public class AddImageBackgroundInJTextArea extends JTextArea
{
public AddImageBackgroundInJTextArea()
{
//Create a window using JFrame with title ( Add image background in JTextArea )
JFrame frame=new JFrame("Add image background in JTextArea");

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

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

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

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

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

//Override JTextArea paint method
//It enable us to paint JTextArea background image
public void paint(Graphics g)
{
//Make JTextArea transparent
setOpaque(false);

//Make JTextArea line wrap
setLineWrap(true);

//Make JTextArea word wrap
setWrapStyleWord(true);

//Get image that we use as JTextArea background
ImageIcon ii=new ImageIcon("textAreaBackground.jpg");
Image i=ii.getImage();

//Draw JTextArea background image
g.drawImage(i,0,0,null,this);

//Call super.paint. If we don't do this...We can't see JTextArea
super.paint(g);
}

public static void main(String[]args)
{
AddImageBackgroundInJTextArea aibijta=new AddImageBackgroundInJTextArea();
}
}


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



textAreaBackground.jpg

Set JTextArea background color to JFrame default color

Complete source code below will show you, how to set JTextArea background color same to JFrame default color.

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


import javax.swing.*;

import java.awt.Color;

public class SetJTextAreaBackgroundColorToJFrameColor
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Set JTextArea background color to JFrame color )
JFrame frame=new JFrame("Set JTextArea background color to JFrame color");

//Get JFrame background color
Color jframeColor=frame.getBackground();

//Create a text area with initial text ( Put your text here )
JTextArea textArea=new JTextArea("Put your text here");

//Set text area background color same to JFrame background color
textArea.setBackground(jframeColor);

//Add created text area into JFrame
frame.add(textArea);

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

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

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

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


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

Get default JFrame background color

Complete source code below will show you, how to get default JFrame background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

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


import javax.swing.JFrame;

import java.awt.Color;

public class GetDefaultJFrameBackgroundColor
{
public static void main(String[]args)
{
//Create a window using JFrame
JFrame frame=new JFrame();

//Get JFrame background color using method getBackground()
Color frameBackgroundColor=frame.getBackground();

//Get RED value from color
int redValue=frameBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=frameBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=frameBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


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

Get default JPanel background color

Complete source code below will show you, how to get default JPanel background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

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


import javax.swing.JPanel;

import java.awt.Color;

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

//Get panel background color using method getBackground()
Color panelBackgroundColor=panel.getBackground();

//Get RED value from color
int redValue=panelBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=panelBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=panelBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


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

Get default JLabel background color

Complete source code below will show you, how to get default JLabel background color and after that, it will print RED,GREEN,BLUE(RGB) value for the color.

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


import javax.swing.JLabel;

import java.awt.Color;

public class GetDefaultJLabelBackgroundColor
{
public static void main(String[]args)
{
//Create a label using JLabel
JLabel label=new JLabel();

//Get label background color using method getBackground()
Color labelBackgroundColor=label.getBackground();

//Get RED value from color
int redValue=labelBackgroundColor.getRed();

//Get GREEN value from color
int greenValue=labelBackgroundColor.getGreen();

//Get BLUE value from color
int blueValue=labelBackgroundColor.getBlue();

//Print RGB value that we get
System.out.println("R : "+redValue);
System.out.println("G : "+greenValue);
System.out.println("B : "+blueValue);

//You can check what color that match the RGB value that we get using 'Color picker' at above
}
}


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

Set JPanel background color

Source code below will show you, how to set JPanel background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

import java.awt.Color;

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

//Create JFrame with title ( Set JPanel background color )
JFrame frame=new JFrame("Set JPanel background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPanel background color to color that you choose
panel.setBackground(color);

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

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

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

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


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

Set JButton background color

Source code below will show you, how to set JButton background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


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

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

public class SetJButtonBackgroundColor
{
public static void main(String[]args)
{
//Create button using JButton
JButton button=new JButton("Click me");

//Create JFrame with title ( Set JButton background color )
JFrame frame=new JFrame("Set JButton background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JButton background color to color that you choose
button.setBackground(color);

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

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

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

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


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

Set JTextField background color

Source code below will show you, how to set JTextField background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JTextField;
import javax.swing.JFrame;

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

public class SetJTextFieldBackgroundColor
{
public static void main(String[]args)
{
//Create text field using JTextField
JTextField textField=new JTextField(10);

//Create JFrame with title ( Set JTextField background color )
JFrame frame=new JFrame("Set JTextField background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JTextField background color to color that you choose
textField.setBackground(color);

//Add JTextField into JFrame
frame.add(textField);

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

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

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


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

Set JTextArea background color

Source code below will show you, how to set JTextArea background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JTextArea;
import javax.swing.JFrame;

import java.awt.Color;

public class SetJTextAreaBackgroundColor
{
public static void main(String[]args)
{
//Create text area using JTextArea
JTextArea textArea=new JTextArea();

//Create JFrame with title ( Set JTextArea background color )
JFrame frame=new JFrame("Set JTextArea background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JTextArea background color to color that you choose
textArea.setBackground(color);

//Add JTextArea into JFrame
frame.add(textArea);

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

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

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


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

Set JLabel background color

Source code below will show you, how to set JLabel background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JLabel;
import javax.swing.JFrame;

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

public class SetJLabelBackgroundColor extends JLabel
{
//Create JFrame with title ( Set JLabel background color )
JFrame frame=new JFrame("Set JLabel background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

public SetJLabelBackgroundColor()
{
//Set text for JLabel
setText("Set JLabel Background Color");

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

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

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

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

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

//Fill background color to JLabel
public void paint(Graphics g)
{
g.setColor(color);
g.fillRect(0,0,getWidth(),getHeight());
super.paint(g);
}

public static void main(String[]args)
{
SetJLabelBackgroundColor sjlbc=new SetJLabelBackgroundColor();
}
}


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

Set JCheckBox background color

Source code below will show you, how to set JCheckBox background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JCheckBox;
import javax.swing.JFrame;

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

public class SetJCheckBoxBackgroundColor
{
public static void main(String[]args)
{
//Create check box with text HI using JCheckBox
JCheckBox checkBox=new JCheckBox("HI");

//Create JFrame with title ( Set JCheckBox background color )
JFrame frame=new JFrame("Set JCheckBox background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JCheckBox background color to color that you choose
checkBox.setBackground(color);

//Add JCheckBox into JFrame
frame.add(checkBox);

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

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

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


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

Set JRadioButton background color

Source code below will show you, how to set JRadioButton background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JRadioButton;
import javax.swing.JFrame;

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

public class SetJRadioButtonBackgroundColor
{
public static void main(String[]args)
{
//Create radio button with text HI using JRadioButton
JRadioButton radioButton=new JRadioButton("HI");

//Create JFrame with title ( Set JRadioButton background color )
JFrame frame=new JFrame("Set JRadioButton background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JRadioButton background color to color that you choose
radioButton.setBackground(color);

//Add JRadioButton into JFrame
frame.add(radioButton);

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

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

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


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

Set JComboBox background color

Source code below will show you, how to set JComboBox background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JComboBox;
import javax.swing.JFrame;

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

public class SetJComboBoxBackgroundColor
{
public static void main(String[]args)
{
//Create JComboBox contents
String[]comboBoxContents={"Cow","Sheep","Horse"};

//Create comboBox using JComboBox
JComboBox comboBox=new JComboBox(comboBoxContents);

//Create JFrame with title ( Set JComboBox background color )
JFrame frame=new JFrame("Set JComboBox background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JComboBox background color to color that you choose
comboBox.setBackground(color);

//Add JComboBox into JFrame
frame.add(comboBox);

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

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

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


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

Set JList background color

Source code below will show you, how to set JList background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JList;
import javax.swing.JFrame;

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

public class SetJListBackgroundColor
{
public static void main(String[]args)
{
//Create JList contents
String[]listContents={"Cow","Sheep","Horse"};

//Create list using JList
JList list=new JList(listContents);

//Create JFrame with title ( Set JList background color )
JFrame frame=new JFrame("Set JList background color");

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

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JList background color to color that you choose
list.setBackground(color);

//Add JList into JFrame
frame.add(list);

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

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

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


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

Set JPasswordField background color

Source code below will show you, how to set JPasswordField background color base on RGB. You can get RGB value for your color using "Color picker" at above.

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


import javax.swing.JPasswordField;
import javax.swing.JFrame;

import java.awt.Color;

public class SetJPasswordFieldBackgroundColor
{
public static void main(String[]args)
{
//Create password field with number of columns equal to 10
JPasswordField passwordField=new JPasswordField(10);

//Create JFrame with title ( Set JPasswordField background color )
JFrame frame=new JFrame("Set JPasswordField background color");

//Set color base on RGB
//You can get RGB value for your color at "Color picker" at above
//R=255
//G=0
//B=0
Color color=new Color(255,0,0);

//Set JPasswordField background color to color that you choose
passwordField.setBackground(color);

//Add JPasswordField into JFrame
frame.add(passwordField);

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

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

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


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

Set JWindow background color

Source code below will show you how to change JWindow background color. If we directly use setBackground method for JWindow, it seem it's color don't change. So we will use JPanel. In this case, we will set color that we want for JWindow background to JPanel background color. After that we will add the created JPanel into JWindow. So after this, if you want to create a JWindow that contain other components like JTextField or JButton with background color, you must add into a JPanel with background color that you want first. After that, add the JPanel into JWindow.

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


import java.awt.Color;

import javax.swing.JWindow;

import javax.swing.JPanel;

public class SetJWindowBackgroundColor
{
public static void main(String[]args)
{
//Color is base on RGB
//R=0 , G=219 , B=252
Color color=new Color(0,219,252);

JPanel myPanel=new JPanel();
myPanel.setBackground(color);

JWindow myWindow=new JWindow();
myWindow.add(myPanel);
myWindow.setSize(400,400);
myWindow.setVisible(true);
}
}


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

RELAXING NATURE VIDEO