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

Get JTextArea text

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

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


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

import java.awt.BorderLayout;

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

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

//Create JTextArea
JTextArea textArea=new JTextArea();

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

public GetJTextAreaText()
{
//Set line wrapping for text area to true
textArea.setLineWrap(true);

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

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

//Add created text area into JFrame
frame.add(textArea,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 text area
JOptionPane.showMessageDialog(frame,"JTextArea text is : "+textArea.getText());
}
}

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


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

Get JTextField text

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

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


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

import java.awt.GridLayout;

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

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

//Create JTextField
JTextField textField=new JTextField(10);

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

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

//Set JFrame layout to GridLayout
frame.setLayout(new GridLayout(2,1));

//Add created text field into JFrame
frame.add(textField);

//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);
}

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

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


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

JSlider label

Source code below will show you, how to set major and minor tick for a JSlider and make the label visible on JSlider.

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


import javax.swing.JSlider;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class JSliderLabel
{
public static void main(String[]args)
{
//Create a slider using JSlider
//Maximum value = 100
//Minimum value = 0
//Initial value = 50
JSlider slider=new JSlider();

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

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

//Enable label on JSlider
slider.setPaintLabels(true);

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

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

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

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

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 200 pixels
frame.setSize(400,200);

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


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

JSlider tick

Source code below will show you, how to set major and minor tick for a JSlider and make the tick visible on JSlider.

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


import javax.swing.JSlider;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class JSliderTick
{
public static void main(String[]args)
{
//Create a slider using JSlider
//Maximum value = 100
//Minimum value = 0
//Initial value = 50
JSlider slider=new JSlider();

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

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

//Enable tick on JSlider
slider.setPaintTicks(true);

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

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

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

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

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 200 pixels
frame.setSize(400,200);

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


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

Create slider using JSlider

Source code below will show you, how to create slider in java using JSlider class.

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


import javax.swing.JSlider;
import javax.swing.JFrame;

import java.awt.FlowLayout;

public class CreateSliderUsingJSlider
{
public static void main(String[]args)
{
//Create a slider using JSlider
JSlider slider=new JSlider();

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

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

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

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

//Set JFrame size to :
//WIDTH : 400 pixels
//HEIGHT : 200 pixels
frame.setSize(400,200);

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


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

Create slider in java

In java, you can create slider using JSlider class.

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

RELAXING NATURE VIDEO