I have a JComboBox. I add a ActionListener using the following code:
addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
textComboActionPerformed(evt);
}
});
This is what it looks like when it is first displayed. It has a couple options in it.
You will notice that Basic Text Box is the first element, and so it will be the element that shows when the JComboBox is first displayed.
If you click on the JComboBox, you will see the options that are available under it.
However, if I select "Basic Text Box" it will not cause the ActionEvent to fire. It has something to do with it being the first element in the list. If I select any other JComboBox, then the ActionEvent is fired.
EDIT: On Linux, say you select an element, and the event fires. Then you select that element again, it will not fire the second time. It is not isolated to just the first element. It has to do with selecting the already selected element twice.
This behavior only happens on Linux. On Windows, the Event fires not matter which element I click on, even the first. Any ideas on why this would be? Does behavior like this vary from JVM to JVM?
Thanks
First, I think the correct listener to use is ItemListener (instead of an ActionListener).
As you state in your comment this gives you consistent behavior across platforms: you don't get an event at all when the already selected item is "re-selected". This is exactly how an ItemListener is supposed to work according to the JavaDocs:
aListener will receive one or two
ItemEvents when the selected item
changes.
When you select the same value that's already selected, obviously you don't change the value, so no event is fired. However, that's not quite what you want apparently. As an alternative, I suggest displaying the JComboBox without a preselected item:
JComboBox comboBox = new JComboBox(model);
comboBox.setSelectedItem(null);
I don't know if that's possible for your application but this way you'll definitely get an event whenever an actual value is selected. (It also makes more sense from a usability point of view, I think, because why would a non-expert click on a combo box to select the value that's already selected?)
Related
I have a registration panel where if student, instructor, and course administrator can register so if student is selected it should show something like this when Student is selected:
and should show like this if other two are selected like this when any of other two are selected:
I tried using if condition on the selected item in where I have added those text fields but it seems it only works at the beginning of the program when I run it on the basis of what is pre-selected and does not change when I select other items in JComboBox. Is there a solution to this?
You can achieve this in different ways. One of such ways is to use Action Listeners. A JComboBox object generates an action event when a selection is made (see Handling Events on a Combo Box).
In your case, you need to trigger an event based on the selection made in a combo box. This action should change the visibility of components in your panel, which are simply changing the visible attribute from true to false (or vice versa) depending on the selection made.
I have a cuttingOperationComboBox ComboBox and a method that changes items and value of this ComboBox:
public void changeGlass(Glass newGlass)
{
ObservableList<Operation> list = new FilteredList<Operation>(ProductGlassCuttingUI.this.operationsDB.getOperationsList(), operation -> operation.getOperationType().toString().equals("RE") &&
operation.getGlassThickness() == newGlass.getGlassThickness());
if(!list.contains(this.cuttingOperationComboBox.getValue()))
cuttingOperationComboBox.setValue(list.get(0));
cuttingOperationComboBox.setItems(list);
}
I also have change listener added to cuttingOperationComboBox.valueProperty().
It is fired first time by cuttingOperationComboBox.setValue(list.get(0)); and here everything is fine. But when cuttingOperationComboBox.setItems(list); fires change listener, newValue in it is null although list is not empty. Moreover it happens only when ComboBox is visible. Tobe more precise: cuttingOperationComboBox is displayed in TreeView as a part of TreeCell graphics. As long as tree view node containing combobox is collapsed everything is ok, but when i expand this node and combobox shows, the above problem appears.
Anybody know what i am doing wrong?
Whenever you call setItems() of a ComboBox (or other controls involving list of items), the ComboBox (or the respective control) will always clear all selection. I believe this is the behavior caused by the underlying selection model, and I believe it is the desired behavior for most use-cases.
Without knowing exactly what you are achieving, it's hard to give you an exact solution. If you simply want to retain the value after setting new list, then you might need to keep a copy of the old selection item, and then manually set it in after setItems(). If you don't want the ChangeListener to respond to setItems(), either check for null newValue in the listener, or keep a boolean flag somewhere that would tell the listener that the particular call was triggered by setItems().
I would like to prevent an event from being fired when a user selects a value already selected in a JComboBox.
For instance, assume I have a JComboBox whose model has the following values:
Cat
Dog
Fish
Bird
Snake
The currently selected value is "Cat". I would like to prevent an Listeners from being notified if the user selects "Cat" again, whilst "Cat" is already selected.
I have tried to implement this by adding a check in the setSelectedItem(Object) in the model. This however did not work.
My next assumption is that if I want this functionality, I will need to subclass JComboBox and override it's setSelectedItem(Object) and contentsChanged(ListDataEvent) functions.
Given the documentation for contentsChanged(ListDataEvent) however, I am hesitant to override it. As such my question for all of you:
Is there a better way to get this desired functionality that doesn't require sub classing JComboBox and overriding it's setSelectedItem(Object) and contentsChanged(ListDataEvent) functions?
I would like to prevent an action event from being fired when a user
selects a value already selected in a JComboBox.
use ItemListener,
wrap code into if (e.getStateChange() == ItemEvent.SELECTED) { as is shown, described in Oracle tutorial
for example
I am working with java applets inorder to embed this applet into one web page. In this applet i used a Choice class to show dropdown list. I know ItemListener helps to know which item is selected once we select an item but Is this possible to know which item has the foucs currently??. i.e, i used the following code
choice = new Choice();
choice.addItem("");
choice.addItem("Choice1");
choice.addItem("Choice 2");
choice.addItem("Choice 3");
Now if i place cursor on choice2 instead of click on it,it has to perform some action. What are the ways to do this?
If I understand your question correctly, you want to add an ItemListener. After you do that, the item listener will be called when something is selected by user input, and then you can take the appropriate action.
One question though, is there a particular reason you're using awt instead of swing?
I have 7 jcomboboxes, say:
customerInfo1
customerInfo2
customerInfo3
customerInfo4
customerInfo5
customerInfo6
customerInfo7
All but the first one are set to setEnabled(false) and have setSelectedItem("Please Select a Customer from the dropdown menu"). I am having the hardest time figuring out how I would go about listening for a state change on customerInfo1 which would then make customerInfo2 setEnabled(true). And, once customerInfo2 is enabled have a state change event trigger the same thing for customerInfo3, and so on. Basically I don't want a given customerInfo jcombobox to be enabled until something other than what was initially set is selected in the preceding one. Your clear and specific help is much appreciated.
you can add a focus listener on the combo boxes such that if a combo has lost focus then you can check about the condition and take your action accordingly!!!
eg
if we have a combo box then we would add a focus listener on it and check the condition whether the check box is checked or not then add the required combobox.
JComboBox jb1;
jb1.addFocusListener(this);
this is to be done in main and the following code in other method in the same class
public void focusLost(FocusEvent fe)
{
//your code for enabling or disabling the combo box
}
this should do the required thing
Regards
ChArAnJiT
You need to call a JavaScript method on change event of the first combo box.
Say if you change the state of the first combo box, this JavaScript method will be called and it will change the state of other combo boxes as u wish.
Same thing have to be done for all the other combo boxes.