Solved

Check box in Java

Posted on 2004-08-09
7
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
Javatm earned 84 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 83 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 83 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

752 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