问题
I've created an application in netbeans IDE 6.9 where I need to set values for each value in the jcombobox. In my pane I've a combobox and below that are the textfields for entering values for each value in the combobox. Can anyone suggest that how do I link the combobox with textfield. I mean there are different values for each value in the combobox. I want that user selects a value in the combobox then its corresponding value should be displayed(if it has already been entered) otherwise a blank space should be shown. I want that all the values for each combobox values should be set in one go(the user should not press the ok button). -Thanks in advance
回答1:
I can only guess from the question that each item in your combo box is an object and you want to edit multiple fields of the selected item.
You could use a bunch of individual text fields, one for each "value" in the selected "value in the combobox".
A better UI would be a property pane to list and edit the fields. The commercial PropertyGrid
in JIDE Grids can actually combine the combobox and property pane in one place.
You can commit each field value after it is entered, or commit all when the editor loses focus (for example when you select another item in the combo).
回答2:
Wouldn't you want to use an ActionListener? Then when an ActionEvent happens for the combo box you could fill the text field with the values from the currently selected item? And if blank then allow them to add to the text field and have an ActionListener on that where if the value is not in the list that is in the combo box to add it to the list in the combo box?
回答3:
jComboBox1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
String str = (String)jComboBox1.getSelectedItem();
jTextField1.setText(str);
}
});
来源:https://stackoverflow.com/questions/3787056/linking-jcombobox-values-with-jtextfield-values