Showing posts with label jbutton. Show all posts
Showing posts with label jbutton. Show all posts

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

Set JButton pressed icon

Complete source code below will show you, how to set JButton pressed icon. When someone click the button, icon on that button we will change to pressed icon that we set for the button. You can download icon that we use in this tutorial at below.When you want to compile and execute it, please make sure, icon file locate at same location with this java file.

Note : If you want to create pressed icon, i suggest you make it size and shape same with non-pressed icon.

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


import javax.swing.*;

public class SetJButtonPressedIcon
{
public static void main(String[]args)
{
//Create image icon from "notpressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\notpressedicon.png
ImageIcon notPressIcon=new ImageIcon("notpressedicon.png");

//Create image icon from "pressedicon.png"
//You can download it at below
//If your icon locate at other location,
//please put it's full address location.For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\pressedicon.png
ImageIcon pressIcon=new ImageIcon("pressedicon.png");

//Create a button with text ( GO ) and not press icon ( notpressedicon.png )
JButton button=new JButton("GO",notPressIcon);

//Set presses icon for JButton
button.setPressedIcon(pressIcon);

//Set horizontal text position to center
button.setHorizontalTextPosition(SwingConstants.CENTER);

//Set vertical text position to bottom.
//So, text will locate under icon
button.setVerticalTextPosition(SwingConstants.BOTTOM);

//Create a window using JFrame with title ( Set JButton pressed icon )
JFrame frame=new JFrame("Set JButton pressed icon");

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

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

//Set JFrame size
frame.setSize(350,150);

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

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


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


pressedicon.png




notpressedicon.png

Set JButton icon

Complete source code below will show you, how to set icon on JButton.

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


import javax.swing.*;

public class SetJButtonIcon
{
public static void main(String[]args)
{
//Create an image icon from png file named ( callme.png )
//You can download this image below
//This file is locate at same location with this java file.
//If your file locate at other location,
//you must put it's full location address. For example :
//C:\\Documents and Settings\\evergreen\\Desktop\\callme.png
ImageIcon iconForButton=new ImageIcon("callme.png");

//Create a button from JButton with text ( Call me ) and image icon ( callme.png )
JButton button=new JButton("Call me",iconForButton);

//set button horizontal text position to center
//You can select one of the following SwingConstants:
//SwingConstants.CENTER
//SwingConstants.RIGHT
//SwingConstants.LEFT
//SwingConstants.LEADING
//SwingConstants.TRAILING (the default)
button.setHorizontalTextPosition(SwingConstants.CENTER);

//set vertical text position to bottom
//It make our text button locate under button icon
//You can select one of the following SwingConstants:
//SwingConstants.CENTER (the default)
//SwingConstants.TOP
//SwingConstants.BOTTOM
button.setVerticalTextPosition(SwingConstants.BOTTOM);

//Create a window from JFrame with title ( Set JButton icon )
JFrame frame=new JFrame("Set JButton icon");

//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(300,150);

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

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


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



callme.png

Set JButton select color

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

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


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

import javax.swing.plaf.ColorUIResource;

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


//Key to know : "Button.select"

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

//Create a button using JButton with text ( CLICK ME AND SEE WHAT COLOR )
JButton button=new JButton("CLICK ME AND SEE WHAT COLOR");

//Create a window using JFrame with title ( JButton select color )
JFrame frame=new JFrame("JButton select 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(300,80);

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


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

Set JButton text bold and italic

Source code below will show you, how to set JButton text to bold and italic.

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


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

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

public class SetJButtonTextBoldAndItalic
{
public static void main(String[]args)
{
//Create button with text ( MY BUTTON )
JButton button=new JButton("MY BUTTON");

//Create font.
//Font Name : Default button font
//Font Style : Bold And Italic
//Font Size : Default button font size
Font newButtonFont=new Font(button.getFont().getName(),Font.ITALIC+Font.BOLD,button.getFont().getSize());

//Set JButton font using new created font
button.setFont(newButtonFont);

//Create a window using JFrame with title ( Set JButton text bold and italic )
JFrame frame=new JFrame("Set JButton text bold and italic");

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

//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(400,100);

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


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

Set JButton text italic

Source code below will show you, how to set JButton text to italic.

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


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

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

public class SetJButtonTextItalic
{
public static void main(String[]args)
{
//Create button with text ( MY BUTTON )
JButton button=new JButton("MY BUTTON");

//Create font.
//Font Name : Default button font
//Font Style : Italic
//Font Size : Default button font size
Font newButtonFont=new Font(button.getFont().getName(),Font.ITALIC,button.getFont().getSize());

//Set JButton font using new created font
button.setFont(newButtonFont);

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

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

//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(400,100);

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


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

Set JButton text bold

Source code below will show you, how to set JButton text to bold.

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


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

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

public class SetJButtonTextBold
{
public static void main(String[]args)
{
//Create button with text ( MY BUTTON )
JButton button=new JButton("MY BUTTON");

//Create font.
//Font Name : Default button font
//Font Style : Bold
//Font Size : Default button font size
Font newButtonFont=new Font(button.getFont().getName(),Font.BOLD,button.getFont().getSize());

//Set JButton font using new created font
button.setFont(newButtonFont);

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

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

//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(400,100);

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


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

Set JButton text size

Source code below will show you, how to set JButton text size.

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


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

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

public class SetJButtonTextSize
{
public static void main(String[]args)
{
//Create button with text ( MY BUTTON )
JButton button=new JButton("MY BUTTON");

//Create font.
//Font Name : Default button font
//Font Style : Default button font style
//Font Size : 16
Font newButtonFont=new Font(button.getFont().getName(),button.getFont().getStyle(),16);

//Set JButton font using new created font
button.setFont(newButtonFont);

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

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

//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(400,100);

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


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

Set JButton text font

Source code below will show you, how to set JButton text font.

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


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

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

public class SetJButtonTextFont
{
public static void main(String[]args)
{
//Create button with text ( MY BUTTON )
JButton button=new JButton("MY BUTTON");

//Create font.
//Font Name : Tahoma
//Font Style : Default button font style
//Font Size : Default button font size
Font newButtonFont=new Font("Tahoma",button.getFont().getStyle(),button.getFont().getSize());

//Set JButton font using new created font
button.setFont(newButtonFont);

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

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

//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(400,100);

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


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

Get JButton text

Source code below will show you how to get text from JButton.

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


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

import java.awt.FlowLayout;

public class GetJButtonText
{
public static void main(String[]args)
{
//Create a button with text ( Click me )
JButton button=new JButton("Click me");

JFrame frame=new JFrame("Get JButton text");
frame.setLayout(new FlowLayout());
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);

//Method getText() will get text from JButton
JOptionPane.showMessageDialog(frame,"JButton text is : "+button.getText());
}
}


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

Set JButton text color

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

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


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

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

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

//Create JFrame with title ( Set JButton text color )
JFrame frame=new JFrame("Set JButton text 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 text color to color that you choose
button.setForeground(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 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
**************************************************************************

Create button using JButton

Source code below will show you, how to create button in Java using JButton class.

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


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

public class CreateButtonUsingJButton
{
public static void main(String[]args)
{
//Create button using JButton
//Button that we create contain text My first button
JButton myButton=new JButton("My first button");

JFrame frame=new JFrame("Create button using JButton");

//add button that we create in JFrame
frame.add(myButton);

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

//set JFrame size
//WIDTH = 300 pixels
//HEIGHT = 100 pixels
frame.setSize(400,100);

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


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

Put JButton into JFrame

Source code below will show you, how to add a JButton into JFrame. In java, we will use JButton to create button.

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


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

public class AddJButtonIntoJFrame
{
public static void main(String[]args)
{
//Create a JButton with text CLICK ME
JButton button=new JButton("CLICK ME");

//Create a JFrame that will be a window
//It's title is, Put JButton into JFrame
JFrame frame=new JFrame("Put JButton into JFrame");

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

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

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

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


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

RELAXING NATURE VIDEO