I have 2 JComboBox, the second gets populates from Database after an item gets selected on first JComboBox. The problem is that the second jcombobox go to populate every time I type a letter. I want to make the second jcombobox wait until the item in first jcombobox gets complete entered.
private void jobCdItemStateChanged(java.awt.event.ItemEvent evt) {
if (evt.getStateChange() == ItemEvent.SELECTED
&& jobCd.getSelectedItem() != "Select..."
&& jobCd.getSelectedItem().toString().length() > 0) {
populatePartNoListFilter();
}
}
A little code would help to know exactly what you are doing but my guess is that your first combo is editable and you are populating the second combo using an event listener that's being called on every key stroke.
According to the documentation using an ActionListener when the combo is editable should work since:
The ActionListener will receive an ActionEvent when a selection has
been made. If the combo box is editable, then an ActionEvent will be
fired when editing has stopped.
If you are using an ActionListener but you still don't find the behavior fits your needs you could populate the second combo by adding a FocusListener on the first one and move the code that populates the second to its focusLost() method.
If this option doesn't suit your needs either, I recommend reading the documentation for the different available events, or give a detailed description of the behavior you are looking for so that someone might come up with a suggestion of the event handling you need to do.
Related
I've spent over a day now trying to figure this out. I've tried MouseListener, ItemListener, and ActionListener examples I've found, but none seem to do what I need.
Here's what I've got: I've got a JComboBox that works well. The user can type in the text box and it autocompletes, and there's a drop-down and everything. The purpose of the box is for performing searches. The user types a search and hits enter, which causes the search results to be highlighted in the main pane. The user can also arrow through the drop-down and initiate the result highlight by hitting enter from the dropdown.
Here's the problem: The user can click an item in the dropdown under the text field and the text field fills in with that value, but the search results are not highlighted. The user still has to hit enter after having clicked what they want.
Here's what I want: I want to detect the moment the user clicks the dropdown menu item they want so that I can use that event to call my method which highlights the result in the main pane. I don't want them to have to hit the enter key after clicking an item in the dropdown.
I've got a key adapter that catches the enter-key press to call the method which highlights results and it works well. The method it calls is called seekAll().
When I tried the mouse listener, it only seemed to work on the text field and not the dropdown. When I tried the ItemListener, I could not distinguish between items being highlighted/hovered/arrowed across and the click making the drop-down menu selection. The result of the action listener was similar to my attempt with the ItemListener.
Here's an example of the ActionListener I tried:
public abstract class HeaderFinderBox {
private WideComboBox searchTermBox;
public HeaderFinderBox() {
final String[] labels = setupData();
//just returns an array of strings
this.searchTermBox = GUIFactory.createWideComboBox(labels);
searchTermBox.setEditable(true);
searchTermBox.setBorder(null);
searchTermBox.setBackground(GUIFactory.DARK_BG);
AutoCompleteDecorator.decorate(searchTermBox);
//This adds my key listener
//searchTermBox.getEditor().getEditorComponent()
// .addKeyListener(new BoxKeyListener());
searchTermBox.addActionListener(new SearchListener());
}
class SearchListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
LogBuffer.println("Action performed on item from the combo box");
//seekAll();
}
}
}
I commented the call to seekAll() because I get null pointer exceptions when the you type your first character into the text box and the field blanks. With that call commented out, I get the "Action performed on item from the combo box" message whenever these things happen:
Twice for each character typed on the keyboard
Once when you hit enter
3 times when you click an item in the dropdown
3 times after arrowing to an item in the dropdown and then hitting enter
The only one of these that I want a single hook for doing something is 1 firing upon the user clicking an item in the dropdown. That would be number 3 in the list above, but it fires 3 times for a single click. And how do I differentiate that event from all the others (1, 2, & 4)?
If ActionListener is supposed to be able to ONLY give me a single actionPerformed upon click of an item in the dropdown, then what am I doing wrong?
I'm using a javax.swing.JTable to show rows in a database table.
I need to fire two different event for two different cases:
when is selected at least a row (or a click on at least a row is performed).
when a double click on a row is performed.
I already looked for an answer on stack overflow, but I didn't find anything satisfying .
Any idea?
when is selected at least a row (or a click on at least a row is performed).
You should monitor changes to the row selection using the JTables ListSelectionModel via a ListSelectionListener. This will notify you when the selection is changed by the user using the mouse or the keyboard or if the selection is changed programmatically for some reason
See How to Write a List Selection Listener for more details
when a double click on a row is performed
The only way you can detect this is through a use of a MouseListener. Normally, users expect that a left mouse button click will do one action and the right mouse button will do something else.
You will want to use SwingUtilities.isLeftMouseButton or SwingUtilities.isRightMouseButton to determine what the user is actually doing.
See How to Write a Mouse Listener for more details
You can add a mouse listener to the table and capture event over there with mouse event like below
table.addMouseListener
(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
}
if (e.getClickCount() == 1)
{
}
}
}
);
}
and to capture selection event you can use
table.getSelectionModel().addListSelectionListener(...);
I would like to know if there is a way to detect if changes in the selection of a item in a swing JCombobox is done by a user (actively) or is causes by repopulating the Jcombobox.
I have to dynamically repopulate the items of the combobox based on other selection, this also invokes the actionPerformed event
so actionPerformed is invoked by:
selection changed by user
repopulating the jcombobox items.
how to tell the difference?
Thanks of helping !
No, not really.
A possible solution is to disable event notification while the combo box is updated. This can be done in (at least) one of two ways...
Firstly, you could physically remove the listener from the combo box, if you have a reference to it.
Secondly, you set a boolean flag, which when true, the listener would ignore the event.
For example...
As you know, without using !getValueIsAdjusting when you select a row in a jtable (by clicking) the selection change event fire twice. It doesn't happen if you select a row using the keyboard arrow. To resolve it, you check if getValueIsAdjusting returns false.
My question is why does the event fire twice if I select a row by clicking it, but not when using the keyboard arrow? And what does getValueIsAdjusting do to resolve it?
As the javadoc which JB Nizet linked to states, getValueIsAdjusting() checks whether a specific event (a change) is part of a chain, if so it will return true. It will only return false when the specified event is the final one in the chain.
In your case, selecting a row by clicking actually fires two events: a mouseDown and mouseUp event and both are sent to your event listener. If you correctly implement getValueIsAdjusting() to return whenever the value is true, you will only act on the final event in the chain, which is the mouseUp event that fires when you let go of the left mouse button.
The Java Tutorials include an example that captures events, you can use that to log the selection events and experiment with it yourself. Remove the return on the event.getValueIsAdjusting() check to log every event that's fired.
String interessen[]= {"aaaaaaa", "bbbbbbbb", "ccccccccc", "ddddddd"};
myList = new JList<>(interessen);
myList.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting())
System.out.println(myList.getSelectedValue());
}
});
The code above shows what getValueIsAdjusting do, without these method an event may be called eg. two times (it depends of event).
Output without getValueIsAdjusting loop after clicking on some element of JList:
aaaaaaa
aaaaaaa
with loop:
aaaaaaa
I have a JComboBox named "jComboBox18" and a JTextArea "jTextArea11". Now I want that whenever a item is selected from the "jComboBox18" combo box its corresponding description is shown in the "jTextArea11" textarea.
I have added the appropriate listener to the JComboBox But the JTextArea is not showing any text. The code that I have written is as follows:
private void jComboBox18ItemStateChanged(java.awt.event.ItemEvent evt) {
Object item = jComboBox18.getSelectedItem();
if(item != null) {
ems.logic.Process selectedProcess = (ems.logic.Process)item;
jTextArea11.setText(selectedProcess.getProcessDescription());
jTextArea11.updateUI();
jTextArea11.revalidate();
jTextArea11.validate();
}
}
=====================EDITED===========================================
The method is being called for sure. I am changing the state of one more combobox
which is also being written in this method and its state changes successfully whenever item is selected from the "jComboBox18"
I think That should work. In fact, you should only need the setText() call. My guess is that you're function isn't getting called for some reason. Put a break point in your code and make sure it's getting called.
In the code shown your method is named as jComboBox18ItemStateChanged. Are you sure this method is being called. The ItemListener for a JComboBox should implement the interface ItemListener which declares that the subclasses should implement the below method.
void itemStateChanged(ItemEvent e);
How are you adding an instance of ItemListener to your JComboBox ?
EDIT:
After reading your edit and comments one another possiblity that I can think of is that:
you have a listener that is triggered when the textarea is updated and probably itis undoing the changes done in JComboBox listener.