Awt checkbox in java

Checkbox class

Checkbox class is used to create a checkbox control, which contains a box that can be checked or unchecked by clicking on it. Checkbox is a component which extends Component class and it can be added to the container like Frame or a component like Panel.

Constructors of Checkbox

Constructor Description
public Checkbox() Creates a checkbox with no text, this checkbox is unchecked by default..
public Checkbox(String text) Creates a checkbox with a text, this checkbox is unchecked by default..
public Checkbox(String text, boolean b) Creates a checkbox with a text, this checkbox is checked or unchecked depending on the boolean value.

Methods of Checkbox class

Methods Description
public void setName(String text) Sets a name on the Checkbox, this name will not be displayed.
public String getName() Gets a String message of Checkbox, this name will not be displayed.
public void setLabel() Sets a String text on button.
public String getLabel() Gets the String text of this button.
public void setState(boolean b) Sets a state of Checkbox.
public boolean getState() Gets the state of Checkbox.
Читайте также:  Python postgresql delete table

An example variants of Checkbox

import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.EmptyBorder; public class CheckboxEx1 < JFrame jf; Checkbox chk1, chk2, chk3, chk4; CheckboxEx1() < jf= new JFrame("Variants of Checkbox"); //Creating an no-message Checkbox chk1 = new Checkbox(); //Creating a pre-selected Checkbox with a message chk2 = new Checkbox("Yes",true); //Creating an unselected Checkbox with a message chk3 = new Checkbox("No"); jf.add(chk1); jf.add(chk2); jf.add(chk3); jf.setLayout(new FlowLayout()); jf.setSize(300,120); jf.setVisible(true); >public static void main(String. ar) < new CheckboxEx1(); >>

When you run the code, you are presented a window shown below -:

Figure 1

  • First checkbox has no label.
  • Second checkbox is already checked by default
  • Third checkbox is unchecked by default.

Handling Checkbox events when a Checkbox is checked or unchecked.

In the upcoming code, we are going to handle events when a Checkbox is checked or unchecked by implementing ItemListener interface.

import java.awt.*; import java.awt.event.*; public class CheckboxEx2 implements ItemListener < Frame jf; Checkbox chk1, chk2; Label label1; CheckboxEx2() < jf= new Frame("Checkbox"); chk1 = new Checkbox("Water"); chk2 = new Checkbox("Coffee"); label1 = new Label(); jf.add(chk1); jf.add(chk2); chk1.addItemListener(this); chk2.addItemListener(this); jf.setLayout(new FlowLayout()); jf.setSize(232,150); jf.setVisible(true); >public void itemStateChanged(ItemEvent ie) < Checkbox ch =(Checkbox)ie.getItemSelectable(); if(ch.getState()==true) < label1.setText(ch.getLabel()+ " is checked"); jf.add(label1); jf.setVisible(true); >else < label1.setText(ch.getLabel()+ " is unchecked"); jf.add(label1); jf.setVisible(true); >> public static void main(String. ar) < new CheckboxEx2(); >>

When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2

When you check a checkbox an event ItemEvent is fired and you are presented a message to display which checkbox is last checked/unchecked. For example, when you check the checkbox with label- happy, you are notified like —

Figure 3

When you uncheck this checkbox, an event ItemEvent is fired you are notified with a message that you’ve unchecked the checkbox(including its name).

Figure 4

Источник

Awt checkbox in java

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Class Checkbox

The following code example creates a set of check boxes in a grid layout:

setLayout(new GridLayout(3, 1)); add(new Checkbox("one", null, true)); add(new Checkbox("two")); add(new Checkbox("three"));

This image depicts the check boxes and grid layout created by this code example:

The button labeled one is in the «on» state, and the other two are in the «off» state. In this example, which uses the GridLayout class, the states of the three check boxes are set independently.

Alternatively, several check boxes can be grouped together under the control of a single object, using the CheckboxGroup class. In a check box group, at most one button can be in the «on» state at any given time. Clicking on a check box to turn it on forces any other check box in the same group that is on into the «off» state.

Nested Class Summary

Nested classes/interfaces declared in class java.awt.Component

Field Summary

Fields declared in class java.awt.Component

Fields declared in interface java.awt.image.ImageObserver

Constructor Summary

Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group.

Creates a check box with the specified label, in the specified check box group, and set to the specified state.

Method Summary

Processes item events occurring on this check box by dispatching them to any registered ItemListener objects.

Removes the specified item listener so that the item listener no longer receives item events from this check box.

Methods declared in class java.awt.Component

Methods declared in class java.lang.Object

Constructor Details

Checkbox

Creates a check box with an empty string for its label. The state of this check box is set to «off,» and it is not part of any check box group.

Checkbox

Creates a check box with the specified label. The state of this check box is set to «off,» and it is not part of any check box group.

Checkbox

Creates a check box with the specified label and sets the specified state. This check box is not part of any check box group.

Checkbox

Constructs a Checkbox with the specified label, set to the specified state, and in the specified check box group.

Checkbox

Creates a check box with the specified label, in the specified check box group, and set to the specified state.

Method Details

addNotify

Creates the peer of the Checkbox. The peer allows you to change the look of the Checkbox without changing its functionality.

getLabel

setLabel

getState

Determines whether this check box is in the «on» or «off» state. The boolean value true indicates the «on» state, and false indicates the «off» state.

setState

Sets the state of this check box to the specified state. The boolean value true indicates the «on» state, and false indicates the «off» state. Note that this method should be primarily used to initialize the state of the checkbox. Programmatically setting the state of the checkbox will not trigger an ItemEvent . The only way to trigger an ItemEvent is by user interaction.

getSelectedObjects

getCheckboxGroup

setCheckboxGroup

Sets this check box’s group to the specified check box group. If this check box is already in a different check box group, it is first taken out of that group. If the state of this check box is true and the new group already has a check box selected, this check box’s state is changed to false . If the state of this check box is true and the new group has no check box selected, this check box becomes the selected checkbox for the new group and its state is true .

addItemListener

Adds the specified item listener to receive item events from this check box. Item events are sent to listeners in response to user input, but not in response to calls to setState(). If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT’s threading model.

removeItemListener

Removes the specified item listener so that the item listener no longer receives item events from this check box. If l is null, no exception is thrown and no action is performed. Refer to AWT Threading Issues for details on AWT’s threading model.

getItemListeners

getListeners

Returns an array of all the objects currently registered as FooListener s upon this Checkbox . FooListener s are registered using the addFooListener method. You can specify the listenerType argument with a class literal, such as FooListener.class . For example, you can query a Checkbox c for its item listeners with the following code:

ItemListener[] ils = (ItemListener[])(c.getListeners(ItemListener.class));

processEvent

Processes events on this check box. If the event is an instance of ItemEvent , this method invokes the processItemEvent method. Otherwise, it calls its superclass’s processEvent method. Note that if the event parameter is null the behavior is unspecified and may result in an exception.

processItemEvent

  • An ItemListener object is registered via addItemListener .
  • Item events are enabled via enableEvents .

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

paramString

Returns a string representing the state of this Checkbox . This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null .

getAccessibleContext

Gets the AccessibleContext associated with this Checkbox. For checkboxes, the AccessibleContext takes the form of an AccessibleAWTCheckbox. A new AccessibleAWTCheckbox is created if necessary.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Оцените статью