Pages

Get JCheckBox text

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

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


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

import java.awt.BorderLayout;

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

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

//Create JCheckBox with text ( I am a checkbox )
JCheckBox checkBox=new JCheckBox("I am a checkbox");

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

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

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

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

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


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