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
Solved

Conditional Linking to URLS

Posted on 1997-11-13
7
221 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
What's wrong with this code? 4 26
jsp insert to database example 2 44
servlet and mdb, jms error 1 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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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