Solved

Check box in Java

Posted on 2004-08-09
7
268 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 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now