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

x
?
Solved

choice fields????

Posted on 2002-03-05
6
Medium Priority
?
248 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
[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
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 200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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