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.
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.
How do I disable input on my editable Combobox? (Well, actually JFoenix JFXCombobox but it's basically the same apart from it's appearance)
setEditable(false) would disable keyboard input on the Combobox but the list would still appear
setDisabled(true) would disable whole Combobox but I want user to be able to focus Combobox so he can copy it's contents if necessery.
Why do I want it like that? In my forms user must first click edit button to be able to change stuff.
Basically, the method ComboBox.setEditable adds/removes an Editor to the ComboBox which can be retrieved via ComboBox.getEditor()
To keep the TextField (to copy from) but disable user-input, simply set the editable flag on the underlying TextField:
private ComboBox<String> myComboBox;
[...]
myComboBox.setEditable(true);
myComboBox.getEditor().setEditable(false);
EDIT:
As #jewelsea said in a comment below, you can hide the list as soon as the user requests to open it:
myComboBox.setOnShown(event -> comboBox.hide());
I think it would be "cleaner" to disable the button which opens the dropdown but unfortunately I have not yet found a way to do that.
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 having a problem on what to use and what to code if I want my combo box be generated once the user type a key...
like:
once i pressed letter "A" from keyboard inside the combo box, all items starting from letter "A" will show up from the drop down list.
How can I code this when:
items from the combo box came from my database and it's default value is the first item stored on my database.
Actually I don't have any idea how to code it. All I have was a template from JFrame.
Items from database were just drag to combo box and it automatically generate a class.
Please Help. Thanks in advance ^_^
ok here is go two classes one for JComboBox and 2nd for JTextField (you are neede both)
you can set for both if is strict (if Iten Exist) or not you can input any value as you wants here
much luck
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?)