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>

petes1234a at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 2
You can also work around it by creating a final int-array of size 1, and simply using the first element as counter.
finalfrontiera at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 3
for a good explanation of why the inner class cannot work with non-final local variables, please look at this: http://forum.java.sun.com/thread.jspa?threadID=736802&messageID=4232040
petes1234a at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 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.

camickra at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 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 > top of Java-index,Desktop,Core GUI APIs...
# 6

In this case you should create a custom Action that extends TextAction. TextAction has a method that tells you the text component that last had focus, so there is no need to search the array to find the text area, you just invoke the method. This posting shows how to create the Action:

http://forum.java.sun.com/thread.jspa?forumID=257&threadID=302962

You can then just add the Action to the JButton.

camickra at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 7
hey thanks again
Zsefva at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...
# 8
Hey sorry... the array stores the text item i need... and i need to write them back, so there is a need for the array.... anyone can help?
Zsefva at 2007-7-12 19:39:17 > top of Java-index,Desktop,Core GUI APIs...