MyJList myList = new MyJList();
myList.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
System.out.println("Selected!");
}
}
});
.
.
.
class MyList extends JList{
public MyList () {
super();
this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.setSelectedIndex(0);
}
}
When I click on list item with mouse, I see message «Selected!».
When program start, this message not shown, but item #0 is selected.
You setSelectedIndex in the constructor
Then after that, add the SelectionListener
when setSelectedIndex is called...there is no Listener
This is exactly what should happen. valueChanged is only called when the user selects the item. setSelectedIndex does not invoke any listeners.
Look at the order of you code:
a) you create the list and set the index to 0
b) you add the ListSelectionListener. Well nothing has changed since you added the listener so no event is fired.
Try adding:
list.setSelectedIndex(1)
after adding the listener to see if the event is fired.
Related
I have to handle an event in a combobox when the user click the item (not when the combobox change the state).
I have four combobox:
(1Combo: Parent category)
(2 Combo: The sons of the category 1)
(3 Combo: the sons of the category 2)
(4 combo: the sons of the category 3)
Each one calls the list to add the items for the other one (the sons of the category choosed).
But my problem is that I have an itemstatechange event and I want to know if the item has been clicked NOT if the combo changes state.
public void itemStateChanged(ItemEvent e) {
if (e.getSource()==jComboBoxCategorias1) {
handleEventCombo1();
}
if (e.getSource()==jComboBoxCategorias2) {
handleEventCombo2();
}
if (e.getSource()==jComboBoxCategorias3) {
handleEventCombo3();
}
if (e.getSource()==jComboBoxCategorias4) {
handleEventCombo4();
}
}
You can add a mouse listener to the combobox and implement the mouseClicked method.
comboBox.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(comboBox.getSelectedItem());
}
});
Don't forget that comboBox is actually a container. So if you really want to have all the mouse events you should add the listener to all the components it contains.
public void addMouseListener(final MouseListener mouseListener) {
this.comboBox.addMouseListener(mouseListener);
final Component[] components = this.comboBox.getComponents();
for(final Component component : components) {
component.addMouseListener(mouseListener);
}
this.comboBox.getEditor().getEditorComponent().addMouseListener(mouseListener);
}
Please visit swing mouse listeners being intercepted by child components for more details.
I'm making a simple menu to delete items on a tree. However, after deleting the items, the tree does not receive a selection event, therefore, the code in the listener does not execute (the listener, in the full code, updates a part of the UI).
I have simplified the code below, leaving out details. It is something like this:
tree.addListener (SWT.Selection, new Listener(){
public void handleEvent(Event e) {
(....)
}
}
I also tried this:
tree.addSelectionListener (new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent e){
(...)
}
public void widgetSelected(SelectionEvent e) {
(...)
}
}
On my menu action (delete selection), there is this:
TreeItem [] selected = tree.getSelection();
tree.deselectAll();
if (selected.length > 0)
{
for( TreeItem i : selected){
i.dispose();
}
}
After deleting the selection, my selection listener does not fire. It does fire if I deselect all itens using the ctrl+click combination.
What should I do? Is there a way to fire the SWT.Selection event to the tree after deleting the itens or should I isolate the code inside the listener to call it again? Shouldn't the tree.deselectAll() fire a Selection event?
You can send a selection event programmatically with:
Event event = new Event();
event.widget = tree;
event.display = tree.getDisplay();
event.type = SWT.Selection;
tree.notifyListeners(SWT.Selection, event);
Have same situation and found
this link mentioning, that programmatically setSelection may never send this event due to design, so always send it (if needed) programmatically after setting too
A combo box will fire an event if a DIFFERENT value is selected. I want to be also be able to listen to the SAME item being selected (that is, valueProperty has no change). There seems to be no way to do this.
I tried extending the ComboBox and finding a way to listen for the little popup menu being closed, but I don't even have access to that! What can I do?
Here is what I was trying:
class ResponsiveComboBox<E> extends ComboBox<E> {
public ResponsiveComboBox() {
super();
assert getContextMenu() != null; //Asssertion failed!
this.getContextMenu().setOnHiding((WindowEvent event) -> {
fireEvent(new ActionEvent());
});
}
}
comboBox.showingProperty().addListener((obs, wasShowing, isShowing) -> {
if (! isShowing) {
System.out.println("Combo box popup hidden");
}
});
This event handler might be triggered before the value is changed.
I have a JButton titled "select"
In the class that creates that JButton and other classes, I want to use an if condition with ActionPerformed method.
Something like(pseudo-code)
if(_selectListener.actionPerformed(ActionEvent)) { //i.e., if select Button is clicked,
//do something
}
Is this possible?
I want to call this method because I have to handle a situation in which a player should be able to choose something by clicking "select" button, or another "scroll" button, and I want to control it using something similar to a bunch of if statements like the one above.
If it is possible, what is the syntax for it? What is the argument ActionEvent?
Thank you!
The easiest and cleanest way is to add a dedicated, specific action listener to each button. That way, when the actionPerformed() method is called, you're sure that the associated button has been clicked, and don't need to test which button has been clicked:
selectButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on select button
}
});
scrollButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// handle click on scroll button
}
});
Another way is to use a common ActionListener, and use the getSource() method of ActionEvent to know which component triggered the event. Compare the result with each potential button to determine which is the one that has been clicked:
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == selectButton) {
// handle click on select button
}
else if (e.getSource() == scrollButton) {
// handle click on scroll button
}
}
What is the argument ActionEvent?
The answer is in the documentation. Read it.
no you cant call, if needs boolean expression/value, but this method returns void.
I add a listener to JCheckBox component and I want call listener manually.how do it?
myCheckBox.selected(false)
then I want to called myCheckBox listener. Do you have better idea?
I know I'm a bit late, but this should do the trick:
ItemListener listener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
//whatever your itemStateChanged() looks like.
}
};
JCheckBox checkBox = new JCheckBox();
checkBox.addItemListener(listener);
Then, whenever you need to call it manually:
listener.itemStateChanged(
new ItemEvent(checkBox, ItemEvent.ITEM_STATE_CHANGED, checkBox, 0));
If you created your listener anonymously, you can still access it like:
checkBox.getItemListeners()[0].itemStateChanged(
new ItemEvent(checkBox, ItemEvent.ITEM_STATE_CHANGED, checkBox, 0));
I really don't know what checkBox component you use. You don't tell us which framework do you use or provide other helpful context infos.
But in general: Your listener is impl. an interface. This interface defines the callbackmethod that your component (checkBox) calls.
If you have a instance of your listener obj. you can call this method directly.
Instead of trying to call the Listener, why not just use a separate method?
ItemListener listener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
method();
}
};
public void method() {
//code you want to run
}
Then just call method() when you want to run the code separate from the Listener.