Adding mouse listener to an array of textarea
HI
I am stuck with this problem. I am trying to register mouse listeners to an array of JTextArea. However, when using a for loop to register, it requires a final counter. The following code may explain what i am trying to do:
for(int index=0;index<filelist.length;index++){
filelist[index] =new JTextArea(5,15);
filelist[index].setEditable(false);
filelist[index].addMouseListener(new MouseListener(){
publicvoid mouseClicked(MouseEvent evt){
if (!evt.isConsumed() && SwingUtilities.isLeftMouseButton(evt)){
if (evt.getClickCount() == 1){
Action a =null;
ActionMap map = filelist[index].getActionMap();
if (map !=null){
a = map.get(DefaultEditorKit.selectLineAction);
}
if (a !=null){
a.actionPerformed(new ActionEvent(evt.getComponent(),
ActionEvent.ACTION_PERFORMED, null, evt.getWhen(),
evt.getModifiers()));
}
}
}
}
publicvoid mousePressed(MouseEvent e){}
publicvoid mouseReleased(MouseEvent e){}
publicvoid mouseEntered(MouseEvent e){}
publicvoid mouseExited(MouseEvent e){}
});
}
line ActionMap map = filelist[index].getActionMap(); requires me to put index as final.
If the array was a button, it would be easier because I can assign an actioncommand and then register the event handler. However this is a MouseEvent.
Any advice?>
[2786 byte] By [
Zsefva] at [2007-11-27 7:57:27]

# 1
One solution, and I don't know if this is the "elegant" solution or not, is to refactor all of the code out of the mouseClicked(MouseEvent evt) and put it into a method of the class containing this event.
for(int index=0;index<filelist.length;index++){
filelist[index] = new JTextArea(5,15);
filelist[index].setEditable(false);
filelist[index].addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent evt) {
mouseClickedEvnt(evt);
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
}
mouseClickedEvnt(MouseEvent evt)
{
if (!evt.isConsumed() && SwingUtilities.isLeftMouseButton(evt)){
if (evt.getClickCount() == 1){
Action a = null;
ActionMap map = filelist[index].getActionMap();
if (map != null){
a = map.get(DefaultEditorKit.selectLineAction);
}
if (a != null){
a.actionPerformed(new ActionEvent(evt.getComponent(),
ActionEvent.ACTION_PERFORMED, null, evt.getWhen(),
evt.getModifiers()));
}
}
}
Message was edited by:
petes1234>
# 4
Your whole approach is wrong. You don't need to create individual listeners for every text area. You should be creating a generic listener. The code would be something like:
MouseListener listener = new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
JTextArea textArea = (JTextArea)e.getSource();
// do rest of code here
}
};
for (.....)
{
JTextArea textArea = new JTextArea(5, 15);
textArea.setEditable( false );
textArea.addMouseListener( listener );
filelist[index] = textArea;
}
In fact you probably don't even need to add the text area to an array, since you just get the text area from the mouse event source.
# 5
Thanks all for replying. I will try the generic mouse listener.
Now what if there is a need to know the index of the array in the event handler?
one question here is that does e.getSource get you the array index of the array?
JButton remv = new JButton("Remove");
remv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(filelist[rownumber].getSelectedText()==null){
return;
}
}
});
Zsefva at 2007-7-12 19:39:17 >
