Easiest way to make JFrame center

Complete source code below will show you, the easiest way to make JFrame get to the center of screen by only one line of code. We will use method setLocationRelativeTo(null) to make our JFrame get to the center. Remember, when you want to use this code, you must put it after you set JFrame size.

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


import javax.swing.JFrame;

public class EasiestWayToMakeJFrameCenter
{
public static void main(String[]args)
{
JFrame frame=new JFrame("Easiest way to make JFrame center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);

//setLocationRelativeTo(null) will make JFrame get to the center
frame.setLocationRelativeTo(null);
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
*****************************************************************

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