Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

java actionlistener

  Asked By: Hayrah    Date: Jan 18    Category: Java    Views: 649

I have one quetsion about java actionlistener

here is the code

new ActionListener() {
public void actionPerformed(ActionEvent e)
String skinname = "";
skinname = list.getSelectedValue().toString() ;
); // end of addActionListener.

system.out.println(skinname) //I want to print skinname here

It will not print because skinname variable define locally. So, can
anypleaes tell me what else I can try so I can print skinname
variable. I tried set and get methods as well but did not work.

If I define skinname outside the ActionListener() then its give me
error says "local variable skinname is accesed from within inner
class; needs to be declared final"

I really appriciated your help.



3 Answers Found

Answer #1    Answered By: Roderick King     Answered On: Jan 18

Make the String skinname a property of the OuterClass.

class OuterClass{
String skinname;

new ActionListener() {
public void  actionPerformed(ActionEvent e) {
skinname = list.getSelectedValue().toString() ;

Answer #2    Answered By: Fatih Farooq     Answered On: Jan 18

I try that. Now its compiles but when I execute its print  the
value null.

Answer #3    Answered By: Isaac Evans     Answered On: Jan 18

Make sure that getSelectedValue() is not null. If list  is a JList
object and the selection is empty, null will be returned.

Try something like this
MouseListener mouseListener = new MouseAdapter() {
public void  mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int index = list.locationToIndex(e.getPoint());
skinname = list.getSelectedValue().toString() ;

For more details on JList

Didn't find what you were looking for? Find more on java actionlistener Or get search suggestion and latest updates.