Pages

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

Get selected JCheckBox

Complete source code below will show you, how to get selected JCheckBox.

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


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

import java.awt.GridLayout;

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

public class GetSelectedJCheckBox implements ActionListener
{
//Create String that will be use to hold selected JCheckBox text
String selectedCheckBox="";

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create button using JButton with text ( Get selected JCheckBox )
JButton button=new JButton("Get selected JCheckBox");

//method actionPerformed that we override because we implement ActionListener
public void actionPerformed(ActionEvent event)
{
//Action for button
if(event.getSource()==button)
{
//Check first JCheckBox is selected or not
if(checkBox1.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox1.getText()+"\n";
}

//Check second JCheckBox is selected or not
if(checkBox2.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox2.getText()+"\n";
}

//Check third JCheckBox is selected or not
if(checkBox3.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox3.getText()+"\n";
}

//Check fourth JCheckBox is selected or not
if(checkBox4.isSelected())
{
selectedCheckBox=selectedCheckBox+checkBox4.getText()+"\n";
}

//Show message that tell you, what check box that you selected
JOptionPane.showMessageDialog(null,"Selected check box is : \n"+selectedCheckBox);

//Make selectedCheckBox string like initial with empty string
selectedCheckBox=new String("");
}
}

//Constructor
public GetSelectedJCheckBox()
{
//Create a window using JFrame with title ( Get selected JCheckBox )
JFrame frame=new JFrame("Get selected JCheckBox");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(5,1);

//Add ActionListener to button
button.addActionListener(this);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

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

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}

//Main method
public static void main(String[]args)
{
GetSelectedJCheckBox gsjcb=new GetSelectedJCheckBox();
}
}


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

Put JCheckBox into group

Complete source code below will show you, how to create group for JCheckBox.

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


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

import java.awt.GridLayout;

public class PutJCheckBoxIntoGroup
{
public static void main(String[]args)
{
//Create a window using JFrame with title ( Put JCheckBox into group )
JFrame frame=new JFrame("Put JCheckBox into group");

//Create check box using JCheckBox
JCheckBox checkBox1=new JCheckBox("Duck");
JCheckBox checkBox2=new JCheckBox("Chicken");
JCheckBox checkBox3=new JCheckBox("Cow");
JCheckBox checkBox4=new JCheckBox("Sheep");

//Create layout that will be use by JFrame
GridLayout gl=new GridLayout(4,1);

//Create button group using ButtonGroup
ButtonGroup bg=new ButtonGroup();

//Add all created check box into button group
bg.add(checkBox1);
bg.add(checkBox2);
bg.add(checkBox3);
bg.add(checkBox4);

//Set JFrame layout
frame.setLayout(gl);

//Add all created check box into JFrame
frame.add(checkBox1);
frame.add(checkBox2);
frame.add(checkBox3);
frame.add(checkBox4);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,200);
frame.setVisible(true);
}
}


*************************************************************************
JUST COMPILE AND EXECUTE IT
*************************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You