Solved

Conditional Linking to URLS

Posted on 1997-11-13
7
219 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

895 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

20 Experts available now in Live!

Get 1:1 Help Now