How to get information from an ActionEvent

Alright i have a basic form that has some text fields and a combobox. Now the information in my combo box is dependant on what was typed in one of the text fields.. For instance, my combo box contains a list of arrangements, and the user is to pick just one.. the list is composed of arrangements for a particular number called WTU's.. So if the user puts in 5 in the WTU's field, then the combo box will be populated with all of the arrangements that have 5 WTUs. I'm trying to figure out how to implement this.. I have a focusListener on the WTU's field that checks when the focus is lost to make sure a numeric value was typed in that field, and that works fine. I figure i should use DefaultComboBoxModel to get the data.. i can fiugre that much out.. but how do i get the data for the number of the WTUs only during some kind of action. I'm guessing i have to talk to these two objects using some kind of abstract event..

Any ideas?

[956 byte] By [mwolfe38a] at [2007-10-2 12:41:29]
# 1

> Now the information in my combo box is dependant on what was typed in one of the text fields

perhaps making this textfield into a combobox (if there are not too many options)

might make life a bit simpler

here's a simple example of the first comboBox controlling the contents of the

second comboBox

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Testing extends JFrame

{

String[] cbo1Text = {"Snacks","Drinks","Cigarettes"};

String[][] cbo2Text = {{"Chocolate Bar","Chips","Candy"},{"Soft","Beer","Wine","Spirits"},

{"Menthol","Camel","Marlboro","Lite"}};

JComboBox cbo1 = new JComboBox(cbo1Text);

JComboBox cbo2 = new JComboBox(cbo2Text[0]);

public Testing()

{

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocation(400,300);

getContentPane().setLayout(new GridLayout(1,2));

cbo1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ae){

DefaultComboBoxModel cbo2NewModel = new DefaultComboBoxModel(cbo2Text[cbo1.getSelectedIndex()]);

cbo2.setModel(cbo2NewModel);}});

getContentPane().add(cbo1);

getContentPane().add(cbo2);

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

instead of hard-coded values, as above, you may want to filter a larger data set

but the effect would be the same

Michael_Dunna at 2007-7-13 9:46:36 > top of Java-index,Security,Event Handling...
# 2

Wether or not to use DefaultComboBoxModel depends on your data format - it doesn't have much to do with the interaction between the ComboBox and the WTU TextField.

What you'll probably want to do is listen to events which indicated the user is done with the WTU TextField. An example of this could be focusLost() events, or keyPressed() events containing the ENTER key.

Whenever you detect such an event, run the validation that is currently in your focusLost() handler. If the input validates, search for, and set as your ComboBox model, the list of corresponding arrangements.

DaanSa at 2007-7-13 9:46:36 > top of Java-index,Security,Event Handling...