Listening Order in Java

I wrote my table cell editor, which extends AbstractCellEditor and implements TableCellEditor , ItemListener and a MouseListener . Is there a way in which the mouseClicked method should execute earlier than the itemStateChanged method? I am trying to do the following:

 private int rowClicked; private JTable table; public void itemStateChanged(ItemEvent e) { if (rowClicked == 5) { // Do something to row 5. } } public void mouseClicked(MouseEvent e) { Point p = e.getPoint(); rowClicked = table.rowAtPoint(p); } 
+4
source share
4 answers

Here is a good article explaining the lack of listener notification in swing: Swing in a better world

+8
source

I ran into a similar problem and just wrote this class. This is a composite action listener in which action listeners take precedence. Higher priorities are called first. It is not general and applies only to listeners of actions.

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; import java.util.TreeSet; public class CompositeActionListenerWithPriorities implements ActionListener { private Map<Integer, ArrayList<ActionListener>> listeners = new TreeMap<Integer,ArrayList<ActionListener>>(); @Override public void actionPerformed(ActionEvent e) { TreeSet<Integer> t = new TreeSet<Integer>(); t.addAll(listeners.keySet()); Iterator<Integer> it = t.descendingIterator(); while(it.hasNext()){ int x = it.next(); ArrayList<ActionListener> l = listeners.get(x); for(ActionListener a : l){ a.actionPerformed(e); } } } public boolean deleteActionListener(ActionListener a){ for(Integer x : listeners.keySet()){ for(int i=0;i<listeners.get(x).size();i++){ if(listeners.get(x).get(i) == a){ listeners.get(x).remove(i); return true; } } } return false; } public void addActionListener(ActionListener a, int priority){ deleteActionListener(a); if(!listeners.containsKey(priority)){ listeners.put(priority,new ArrayList<ActionListener>()); } listeners.get(priority).add(a); } } 
+3
source

Ideally, you should not try to get the line number editable inside the editor. After the user has finished editing in the editor and moved to another cell, JTable will get the current value in the editor using the getCellEditorValue () method, and then call setValueAt (Object aValue, int rowIndex, int columnIndex) on the table model. Therefore, it is best to handle something specific to the string in the setValueAt () method.

+2
source

You cannot depend on an order to trigger events , but you can forward events as needed. In this case, do not try to define a string in the ItemListener . Instead, let CellEditor conclude and use the new value to update the model, as suggested here .

+2
source

All Articles