Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check box in Java

Posted on 2004-08-09
7
Medium Priority
?
311 Views
Last Modified: 2013-11-24
I want ro design a JSP or Servlet (use J2SE). In this page I will receive records from Database, every records have 3 fileds( array rows on Web) , when display every fileds will have a checkbox (for examples: filed1, filed2, filed3). On the page If we check Field1 if True , Field2 and Field3 will automaltically check true. If check box of Filed2 and Filed3 is False, check box of Field1 is False.
How can I do that, this is the first time I use Java.
Please help me
0
Comment
Question by:tuanhuong
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 336 total points
ID: 11759160
Hi Friend;

If this is your 1st time in jsp, click on the link bellow for good JSP Tutorials :

http://www.jsptut.com/
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11759167

Here's a previous question about that :

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20294941.html

Here's the html format :

<TD><INPUT type="checkbox" name="cprId_10611583" value="set">TD>
<TD><INPUT type="checkbox" name="cprId_10611584" value="set"></TD>
<TD><INPUT type="checkbox" name="cprId_10611585" value="set"></TD>

then in your jsp code :
...

<%
java.util.Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String) paramNames.nextElement();
if (paramName.indexOf("cprId") != -1) {
String id = paramName.substring(paramName.indexOf("_") + 1);
if (request.getParameter(paramName) != null && request.getParameter(paramName).equalsIgnoreCase("set")) {
// checkbox checked
}
}

Hope that helps . . .
Javatm
0
 
LVL 11

Assisted Solution

by:sompol_kiatkamolchai
sompol_kiatkamolchai earned 332 total points
ID: 11759438
This is another option that you can do in client side by script which could off-load server side processing.

<html>
<body>
<form name="form1">
  <input type="checkbox" name="check1" onchange="if(this.checked){check2.checked=true;check3.checked=true;}">
  <input type="checkbox" name="check2" onchange="if(!this.checked && !check3.checked) {check1.checked=false;}">
  <input type="checkbox" name="check3" onchange="if(!this.checked && !check2.checked) {check1.checked=false;}">
</form>
</body>
</html>
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 332 total points
ID: 11762139
>> If we check Field1 if True , Field2 and Field3 will automaltically check true. If check box of Filed2 and Filed3 is False, check box of Field1 is False.
>> How can I do that,

This little demo program shows the wanted logic:

Save the following in a file called CheckboxDemo.java.
Compile it, and run it.


/*
 * CheckboxDemo.java
 *
 */

public class CheckboxDemo extends javax.swing.JFrame {
   
    public CheckboxDemo() {
        initComponents();
        setLocationRelativeTo(null);
    }
   
    private void initComponents() {
        field1CheckBox = new javax.swing.JCheckBox();
        field2CheckBox = new javax.swing.JCheckBox();
        field3CheckBox = new javax.swing.JCheckBox();

        getContentPane().setLayout(new java.awt.FlowLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        field1CheckBox.setText("Field1");
        field1CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                field1CheckBoxActionPerformed(evt);
            }
        });

        getContentPane().add(field1CheckBox);

        field2CheckBox.setText("Field2");
        field2CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                field2CheckBoxActionPerformed(evt);
            }
        });

        getContentPane().add(field2CheckBox);

        field3CheckBox.setText("Field3");
        field3CheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                field3CheckBoxActionPerformed(evt);
            }
        });

        getContentPane().add(field3CheckBox);

        pack();
    }

    private void field3CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
        if ( !field2CheckBox.isSelected() && !field3CheckBox.isSelected() )  // if both field2 & field3 checkboxes are not selected...
            field1CheckBox.setSelected(false);                                         // also deselect the field1 check box
    }

    private void field2CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
        if ( !field2CheckBox.isSelected() && !field3CheckBox.isSelected() )  // same as above
            field1CheckBox.setSelected(false);
    }

    private void field1CheckBoxActionPerformed(java.awt.event.ActionEvent evt) {
        if (field1CheckBox.isSelected()) {         // if the field 1 check box is selected...
            field2CheckBox.setSelected(true);   // also select the field2
            field3CheckBox.setSelected(true);   // and the field3 check box
        }
    }
   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }
   
    public static void main(String args[]) {
        new CheckboxDemo().show();
    }
   
    private javax.swing.JCheckBox field1CheckBox;
    private javax.swing.JCheckBox field2CheckBox;
    private javax.swing.JCheckBox field3CheckBox;
}

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question