Solved

choice fields????

Posted on 2002-03-05
6
242 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

615 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