Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Conditional Linking to URLS

Posted on 1997-11-13
7
Medium Priority
?
229 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 400 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
Technology Partners: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

722 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