Showing posts with label jlabel. Show all posts
Showing posts with label jlabel. Show all posts

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

Put JLabel into list

Complete source code below will show you, how to put JLabel into list. During my try using java i found no way to put JLabel into JList. So i try to create it on my own. I hope you enjoy what i share with you.

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


import javax.swing.*;

import java.awt.GridLayout;

public class AddJLabelIntoList
{
public static void main(String[]args)
{
//Create label using JLabel that will be put into list
JLabel label1=new JLabel("Duck");
JLabel label2=new JLabel("Sheep");
JLabel label3=new JLabel("Cow");
JLabel label4=new JLabel("Buffalo");
JLabel label5=new JLabel("Horse");
JLabel label6=new JLabel("Goat");

//Create panel that will be use to put all label into it
JPanel panel=new JPanel(new GridLayout(6,1));

//Add all created label into panel
panel.add(label1);
panel.add(label2);
panel.add(label3);
panel.add(label4);
panel.add(label5);
panel.add(label6);

//Create scroll bar for created panel
//Vertical scrollbar always show
//Horizontal scrollbar never show
JScrollPane scrollBar=new JScrollPane(panel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

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

//Add created scroll bar into JFrame
frame.add(scrollBar);

//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 JLabel text center

Complete source code below will show you, how to make text in JLabel to center.

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


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

import javax.swing.SwingConstants;

public class JLabelTextToCenter
{
public static void main(String[]args)
{
//Create a label with text ( I am a JLabel )
JLabel label=new JLabel("I am a JLabel");

//Make JLabel text to center
label.setHorizontalAlignment(SwingConstants.CENTER);

//Create a window using JFrame with title ( JLabel text to center )
JFrame frame=new JFrame("JLabel text to center");

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

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

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

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


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

Put image on JLabel

Complete source code below will show you, how to put image on JLabel.



JLabelImage.jpg


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


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

import java.awt.Toolkit;

public class JLabelImage
{
public static void main(String[]args)
{
//Create image icon from your image file
//You must change location for your image file
//My image file is locate at : C:\Documents and Settings\evergreen\Desktop
ImageIcon ii=new ImageIcon("C:\\Documents and Settings\\evergreen\\Desktop\\JLabelImage.jpg");

//Create a label with your image file
JLabel label=new JLabel(ii);

//Create a window using JFrame with title ( Put image on JLabel )
JFrame frame=new JFrame("Put image on JLabel");

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

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

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

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


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

Set JLabel text bold and italic

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

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJLabelTextBoldAndItalic
{
public static void main(String[]args)
{
//Create label using JLabel with text ( HI EVERYONE !! I love you all )
JLabel label=new JLabel("HI EVERYONE !! I love you all",SwingConstants.CENTER);

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

//Set JLabel font using new created font
label.setFont(newLabelFont);

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

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

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

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

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

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


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

Set JLabel text italic

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

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJLabelTextItalic
{
public static void main(String[]args)
{
//Create label using JLabel with text ( HI EVERYONE !! I love you all )
JLabel label=new JLabel("HI EVERYONE !! I love you all",SwingConstants.CENTER);

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

//Set JLabel font using new created font
label.setFont(newLabelFont);

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

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

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

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

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

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


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

Set JLabel text bold

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

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJLabelTextBold
{
public static void main(String[]args)
{
//Create label using JLabel with text ( HI EVERYONE !! I love you all )
JLabel label=new JLabel("HI EVERYONE !! I love you all",SwingConstants.CENTER);

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

//Set JLabel font using new created font
label.setFont(newLabelFont);

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

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

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

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

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

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


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

Set JLabel text size

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

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJLabelTextSize
{
public static void main(String[]args)
{
//Create label using JLabel with text ( HI EVERYONE !! I love you all )
JLabel label=new JLabel("HI EVERYONE !! I love you all",SwingConstants.CENTER);

//Create font.
//Font Name : Default label font
//Font Style : Default label font style
//Font Size : 22
Font newLabelFont=new Font(label.getFont().getName(),label.getFont().getStyle(),22);

//Set JLabel font using new created font
label.setFont(newLabelFont);

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

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

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

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

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

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


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

Set JLabel text font

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

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


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

import java.awt.BorderLayout;
import java.awt.Font;

public class SetJLabelTextFont
{
public static void main(String[]args)
{
//Create label using JLabel with text ( HI EVERYONE !! I love you all )
JLabel label=new JLabel("HI EVERYONE !! I love you all",SwingConstants.CENTER);

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

//Set JLabel font using new created font
label.setFont(newLabelFont);

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

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

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

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

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

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


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

Get JLabel text

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

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


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

import java.awt.BorderLayout;

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

public class GetJLabelText implements ActionListener
{
//Create button with text ( Click me to get JLabel text )
JButton button=new JButton("Click me to get JLabel text");

//Create JLabel with text ( I LOVE YOU )
JLabel label=new JLabel("I LOVE YOU");

//Create a window with title ( Get JLabel text )
JFrame frame=new JFrame("Get JLabel text");

public GetJLabelText()
{
//Add action listener to button
button.addActionListener(this);

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

//Add created label into JFrame
frame.add(label,BorderLayout.CENTER);

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

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

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

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

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Method getText will get text from label
JOptionPane.showMessageDialog(frame,"JLabel text is : "+label.getText());
}
}

public static void main(String[]args)
{
GetJLabelText gjbt=new GetJLabelText();
}
}


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

Set JLabel text color

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

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


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

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

public class SetJLabelTextColor
{
public static void main(String[]args)
{
//Create label with text HI using JLabel
JLabel label=new JLabel("HI");

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

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

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

Create label using JLabel

Source code below will show you, how to create label in java using JLabel. We will use JLabel when we want to add text or image into graphical user interface.

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


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

public class CreateLabelUsingJLabel
{
public static void main(String[]args)
{
//Create a label with text My First Label using JLabel
JLabel label=new JLabel("My First Label");

//Create a JFrame that we will use to place JLabel
JFrame frame=new JFrame("Create label using JLabel");

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

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

//Set JFrame size to :
//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
***************************************************************************

Why we use JLabel

JLabel is use when you want to add text or image into you graphical user interface.

Put JLabel into JFrame

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

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


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

public class AddJLabelIntoJFrame
{
public static void main(String[]args)
{
//Create a JLabel with text HI that we want to add into JFrame
//SwingConstants.CENTER will make HI locate at the center of JLabel
JLabel label=new JLabel("HI",SwingConstants.CENTER);

//Create a JFrame that will be a window
//Window title is, Put JLabel into JFrame
JFrame frame=new JFrame("Put JLabel into JFrame");

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

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

//Set size for JFrame in pixels
//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