Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

choice fields????

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

971 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