Solved

Check box in Java

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.
Suggested Courses

628 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