Solved

choice fields????

Posted on 2002-03-05
6
211 Views
Last Modified: 2010-03-31
Hi!!!

How would i make my choice field to behave like the select field in html?For example, the select (in html) accepts two values: the description and its values.
<select>
    <option value="PRO">PROVINCE</option>
    <option value="REG">REGION</option>
</select>

i want that if i select in the choice fields, i want it to return the value?


raymond
0
Comment
Question by:rdy1437
6 Comments
 
LVL 7

Expert Comment

by:tomboshell
ID: 6843737
JComboBox?  Is that what you are looking for?  You are doing a Java program and are a web-designer?  Once you pass the array containing the names to the constructor, the combo box will conatin the values that you can select and access.   Then you just need to assign a listener to the combo box to work with the values and do whatever action you need.
0
 

Author Comment

by:rdy1437
ID: 6843740
how about in awt?
0
 

Expert Comment

by:fundoosathish
ID: 6846539
hi,
   if its a servlet you want to include the code into and make it run using IE then all you need to do is give the same select statements in the out.println("") statements and it will work.
   if you are running the java program and its not a servlet then do as tomboshell says.

0
 
LVL 7

Accepted Solution

by:
tomboshell earned 50 total points
ID: 6846651
It appears that the java.awt.Choice class has one constructor that takes no parameters.  You will have to manually add each item in a method.  

here is a quick awt class example that you could use

import java.awt.*;
import java.awt.event.*;

public class myFrame extends Frame{
     private String[] items=new String[]{"One","Two","Three","Four","Five"};
     private Choice box;

     public static void main(String[] args)
     {          myFrame mf=new myFrame();
     }

     public myFrame(){
          init();
          this.show();
     }

     public void init(){
          this.setTitle("blaH");
          this.setSize(300,300);
          box=addMyChoice();
          this.add(box);
          this.addWindowListener(new AppCloser());
     }

     public Choice addMyChoice(){
          // construct the choice box, add the items from the array and a listener
          Choice myC=new Choice();

          for(int i=0; i < items.length; i++){
               myC.add(items[i]);
          }

          myC.addItemListener(new ItemListener(){
               public void itemStateChanged(ItemEvent e){
                    selectionMethod();
               }
          });

          return myC;
     }

     public void selectionMethod(){
          // print out what was selected.
          String temp=box.getSelectedItem();
          System.out.println("Item selected was :"+temp);
     }

     protected static class AppCloser extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
              System.exit(0);
          }
     }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 8658708
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- points to tomboshell

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strDist challenge 35 84
wordcount challenge 11 75
Eclipse Neon and jdk 1.8.0 11 66
Java asynchronous logging 4 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

707 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

15 Experts available now in Live!

Get 1:1 Help Now