Could anyone pls help out with the logic of codes that uses Choice menu.

The choice buttons were created with its items(3 in this case)

I have another sets of 9 buttons in an array

Each button,when pressed gets a corresponding value from an array of integers defined elsewhere

The obtained value assigned to a particular button shows up in seperate textfield

The challenge I am faced with now is this:
The array of buttons must be able to listen to each of the selection of the choice menu, such that each item selected causes the button to pick from different sets of arrays of values when pressed (I already have three arrays of vaules)

I'll quite appreciate if anyone could help out with this
Thanks.

Recommended Answers

All 5 Replies

If I understood correctly whenever you click a button you need to get a value from a "set of values" depending on the value of the radio button?
If yes then you don't need any listeners to the radio buttons. Whenever a button is clicked, you can get the value of the radio selected and then with if statements select the set you want to take values from:

// When a button is clicked
actionPerformed(Action event) {
    // get the value of the choice button currently selected
   // with that choose which "set of values" you will use
}

Check the API. I assume that you use javax.swing.

Thanks a lot Javaddict,I do really appreciate your attempt to help.Here I'm including sections of my problem perhaps that would make things a a lot more clear
My choice button were created thus

Choice travelChoice;
travelChoice=new Choice();
travelChoice.addItem("A ");
travelChoice.addItem("B ");
travelChoice.addItem("C ");

In another class of the same package I have my arrays of values as

static final int[] fare = { 110, 200,100, 160,220,270,280,370,380 };
static final int[] fareo={110, 110,110, 160, 160, 220,220,220,220,};
static final int[] fares={160, 320,320, 400, 400, 400,400,400,400};

got a listener that listen to my arrays of buttons

class ZoneListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
amount = Integer.parseInt(e.getActionCommand());

the buttons currently created and listen to array of values as thus

for (int i=0; i<TicketMachine.nZones; i++) {
zoneBtn[i].setActionCommand("" + TicketMachine.fare[i]);
zoneBtn[i].addActionListener(zl);
		}

All these currently work fine.

What I want to achieve now is this,when the first choice is selected from the choice menu,
the array of buttons should be ready to pick from the first set of values for (fare[])
and when the second array is selected,the button should pick from the second array of values (fareo[])
and finally when the third item on the menu is selected,the button should be ready to listen to the third setS of array of value(fares[])

Here is where I am currently stuck and what i am trying to do

public class MyOption implements ItemListener 
{
public void itemStateChanged(ItemEvent e)
{
int Option,
Option = travelChoice.getSelectedIndex();
.....

I'm not sure if my logic is reasonable at all.I am a complete programming infant.Thanks once more again

Just have an int[] array variable like "selectedFares[]" that points to the correct fare array. Your stateChanged method just needs to set it when they choose one, i.e.

switch (option) {
  case 0:
    selectedFares = fare;
    break;

case 1:
   selectedFares = fareo;
   break;

... etc
}

If your fares were in a 2D array, it would be even easier since the first parameter of the array could correspond to the index of your choice menu.

Thanks very much Ezzaral,you must be a savior indeed!However,I have being trying my hands on the idea you supplied but have not being able to get it working.I'm still on it.

Let's say your fares were in an array fares[3][] . Then your option group index directly corresponds to a fare schedule

selectedFare = fares[optionIndex];

No need for a switch. The rest of your code just references selectedFare[i] .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.