Solved

Conditional Linking to URLS

Posted on 1997-11-13
7
218 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
  • 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:

760 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

19 Experts available now in Live!

Get 1:1 Help Now