Solved

choice fields????

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

919 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

22 Experts available now in Live!

Get 1:1 Help Now