Solved

Conditional Linking to URLS

Posted on 1997-11-13
7
223 Views
Last Modified: 2010-04-16
I have created some independent checkboxes for a shopping class.  After those, I have two buttons "Submit" and "Reset".  Once a user has checked one or more boxes, I want to direct him/her to a URL that contains a description of the items checked.  For instance, is the user checks boxes 1, 6 and 5 the applet should link him to a URL containing the descriptions for those item.  There will be different URLS for different checkbox combinations.  Is there a simple way to do this?
0
Comment
Question by:javababy
[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
  • 4
  • 3
7 Comments
 
LVL 4

Expert Comment

by:russgold
ID: 1230490
From a Java applet, you link to a URL by calling the showDocument() method on AppletContext, passing a URL object. Since you can pass any URL you want, this problem reduces to a simple selection of strings based on boolean values (derived from your checkboxes) - I assume you are not trying to generate the HTML pages on the fly, which cannot be done from an applet.

Probably the simplest way of handling this selection would be via a table based on the mapping of a binary number created from your booleans.  For example, given an array of checkboxes and another of URLs, you might write:

// assume the following declarations:
final int NUM_CHECKBOXES =    // whatever you want...
final int NUM_URLS       = 2 << NUM_CHECKBOXES;

URL[] urls = new URL[ NUM_URLS ];
Checkbox[] checkBox = new Checkbox[ NUM_CHECKBOXES ];

// to select a URL...

int value = 0;
for (int i = 0; i < NUM_CHECKBOXES; i++) {
    value |= ((checkBox[i]. getState() ? 1 : 0) << i);
    }
URL desiredURL = urls[ value ];

:
:

context. showDocument( desiredURL );


0
 

Author Comment

by:javababy
ID: 1230491
I'm too new at this to understand what you mean by a table.  I don't see how the checkbox array handles a combination of checkbox trues.  I can sort of see how a URL array would work -- if checkbox 1, 2 and 4 are true get URL[i] for instance.  Can't you do it simply like a C++ compound if statement, e.g.:
if(cb1 == true && cb2 == true && cb4 == true)
{
     getURL[i] or getURL("http://www.something.com");

0
 
LVL 4

Accepted Solution

by:
russgold earned 100 total points
ID: 1230492
Sorry, I've been talking to too many database people recently ;)

I mean an array, not a table.

And yes, of course you can use many if statements as you suggest, but why?  Doing it that way, you would need 32 different conditionals to handle 5 checkboxes.

The checkbox array just allows you to handle them all with a common set of logic.  The idea is that if checkboxes 0, 1, and 4 are true (remember that arrays in Java are zero-based), the code creates an index as binary: 0001 0011 which equals 19, in base 10.  This selects the URL at urls[19] automatically.

To define which URL gets selected, you just initialize the urls array appropriately.

BTW, if you don't understand an answer, you don't need to reject it immediately; you can ask questions via comments until you understand whether the answer will work for you or not.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:javababy
ID: 1230493
Russgold,
I really appreciate your working with me on this.  I don't know whether I'm ignorant or just tired.  I understand partially what you mean; but, I figured out last night that in order to do this the way I'd planned that I would need 56 different combinations of web pages; so, I went back to the drawing board.  All I'm trying to do is have a applet with 6 category buttons which will link to a URL when one is pressed and allow the user to come back and pick another.  I got the following code to compile; and, the space for the applet shows up on my html; but, it's blank and nothing happens.  Can you help me -- I'm under a deadline!  I'll A your previous answer and spend another 100 points if necessary.  I created a category class as well -- see below other code:

import java.awt.*;
import java.net.*;

public class Browsing extends java.applet.Applet {

      Category cat[] = new Category[6];

      public void init() {

            cat[0] = new Category("T-Shirts", “http://www.MyPage.com/tshirts.html");
            cat[1] = new Category("Music", “http://www.MyPage.com/music.html");
            cat[2] = new Category("Kid Stuff","http://www.MyPage.com/kidstuff.html");
            cat[3] = new  Category("Clipart","http://www.MyPage.com/clipart.html");
            cat[4] = new Category("Hotspots","http://www.MyPage.com/hotspots.html");
            cat[5] = new Category("Humor Dining","http://www.MyPage.com/humor.html");

            setLayout(new GridLayout(cat.length,1, 10, 10));
            for (int i = 0; i< cat.length; i++) {
                  add(new Button(cat[i].name));
            }

      }

      public boolean action(Event evt, Object arg) {
            if (evt.target instanceof Button) {
                  linkTo((String)arg);
                  return true;
            }
            else return false;
      }

      void linkTo(String name) {
            URL theURL = null;
            for (int i = 0; i<cat.length; i++) {
                  if (name.equals(cat[i].name))
                        theURL = cat[i].url;
            }
            if (theURL != null)
                  getAppletContext().showDocument(theURL);
      }
}
\\Category class
import java.net.URL;
import java.net.MalformedURLException;

class Category {
      String name;
      URL url;

      Category(String name, String theURL) {
            this.name = name;
            try{ this.url = new URL(theURL); }
            catch ( MalformedURLException e) {
                  System.out.println("Bad URL: " + theURL);
                  }
      }
}
0
 
LVL 4

Expert Comment

by:russgold
ID: 1230494
Well, it compiles and runs fine for me.  

There are a few possibilities for what could be going wrong. I would start with your HTML file.  What settings did you use in your APPLET tag?  Are you sure the height and width are sufficient?  Also, check your Java log to see if the browser can even find your classes.
0
 
LVL 4

Expert Comment

by:russgold
ID: 1230495
I have loaded your applet onto my web site.  You can try it at:

http://www.netaxs.com/~russgold/ee-demos/TrivialApplet.html
0
 

Author Comment

by:javababy
ID: 1230496
Thanks a bunch!!!  I had just forgotten to compile the category class (told you I was a tired puppy) -- it works now.

Once again thanks for all of your help and patience.

JavaBaby
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
Delete Android all or certain applications data programmatically 9 151
Java Eclipse Loop 3 47
What does this method description mean? 1 13
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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