Solved

Check box in Java

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
Should localization be done inside spring controller 5 26
xampp tool 12 50
How to execute a Python program and gather return output in Java 2 30
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

778 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