- Class Checkbox
- 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
- Method Summary
- Methods declared in class java.awt.Component
- Methods declared in class java.lang.Object
- Constructor Details
- Checkbox
- Checkbox
- Checkbox
- Checkbox
- Checkbox
- Method Details
- addNotify
- getLabel
- setLabel
- getState
- setState
- getSelectedObjects
- getCheckboxGroup
- setCheckboxGroup
- addItemListener
- removeItemListener
- getItemListeners
- getListeners
- processEvent
- processItemEvent
- paramString
- getAccessibleContext
- How to get Multiple Selected Checkbox Value in Java Servlet
- index.html
- MultipleForm.java
- Output
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.
How to get Multiple Selected Checkbox Value in Java Servlet
Hi coder, In this tutorial, you will see how to get all the value of the multiple selected checkbox in Servlet. In this example, a form contains some checkboxes, Users have checked them and when they hit the submit button, send your form to the Java Servlet class and this class will show the value of multiple checkboxes.
index.html
See below the HTML form consisting of a single text box and four checkboxes, each representing a language name for programming.
method=»post» attribute send form data to the server as an HTTP POST request. And action=» MultipleForm» attribute specifies the relative URL of the Servlet file responsible for handling the posted data from that form.
HTML Form Your Name Language JAVA PHP ASP C++
MultipleForm.java
Create a Java Servlet class that is mapped to the URL: MultipleForm, as mentioned in the attribute for action form.
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = ) //it is annotations public class MultipleForm extends HttpServlet < public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException < response.setContentType("text/html"); PrintWriter out=response.getWriter(); String heading="Get Multiple Selected Checkbox Value in Servlet"; out.print("" +heading+ "
"); if(request.getParameter("btn_submit")!=null) //check button click event < String name=request.getParameter("txt_name"); //get textbox name "txt_name" out.println(name); out.println(""); String language[]=request.getParameterValues("chk_language"); //get checkbox name value "chk_language" and store in language[] array for(int i=0;i > > >
@WebServlet annotation defines the servlet URL in front of the class. If the user submits the form, the servlet container will execute the doPost method for the servlet. We will perform the following tasks within doPost method.
String name=request.getParameter("txt_name"); //get textbox name "txt_name" out.println(name);
String language[]=request.getParameterValues("chk_language"); //get checkbox name value "chk_language" and store in language[] array for(int i=0;i
Use request.getParameterValues() method to get multiple selected checkboxes values from the «chk_language» attribute name of checkbox. And to store string type variables in the language array.
length – Using this method to find the length of the variable and you can obtain the size of the array. This method can be used for int[], double[], String[].
Apply for loop, the condition i < language.length used to find the variable length of the language array variable that increases by the integer type variable i.
Output
Invoke project in NetBeans IDE the editor will automatically open the browser type any name in the text box and check the checkboxes for some programming language names. I’ve tested 2 checkboxes.