question about event.getSouce()

i was wondering that if i have this code in my program the compiler replies cannot find symbol

ActionListener addAction =new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

if(event.getSource()instanceof JButton){

JButton clickedButton = (JButton)event.getSource();

if(clickedButton == addButton){

String entry = textField.getText().trim();

tree.add(entry);

}

}

};

};

addButton.addActionListener(addAction);

now if i use thos code it works like a charm but why?

ActionListener addAction =new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

String entry = textField.getText().trim();

tree.add(entry);

};

};

one more question when i try to do this

publicclass TreeListTesterextends JFrameimplements ActionListener

the compiler slaps and replies that it treelist is abstract and it does not override actionPerformed.

and here is my full source code.

import javax.swing.*;

import java.util.*;

import java.awt.*;

import java.awt.event.*;

publicclass TreeListTesterextends JFrame{

privatestaticfinalint FRAME_WIDTH = 400;

privatestaticfinalint FRAME_HEIGHT = 300;

private JButton addButton;

private JButton addAllButton;

private JButton clearButton;

private JButton printButton;

private JButton removeFirst;

private JButton removeLast;

private JPanel buttonPanel;

private JTextField textField;

private JList list;

private JScrollPane scrollPane;

publicstaticvoid main(String[] args){

TreeListTester frame =new TreeListTester();

frame.setVisible(true);

}

public TreeListTester(){

setSize(FRAME_WIDTH, FRAME_HEIGHT);

setTitle("TreeSet Sorted List");

Container contentPane = getContentPane();

finalTreeSetList tree =new TreeSetList();

list =new JList(tree);

scrollPane =new JScrollPane(list);

scrollPane.setBorder(BorderFactory.createLineBorder(Color.RED));

contentPane.add(scrollPane, BorderLayout.CENTER);

textField =new JTextField();

contentPane.add(textField, BorderLayout.NORTH);

buttonPanel =new JPanel(new GridLayout(2,3));

addButton =new JButton("Add");

addAllButton =new JButton("AddAll");

clearButton =new JButton("clear");

printButton =new JButton("Print");

removeFirst =new JButton("RFirst");

removeLast =new JButton("RLast");

addButton.setBackground(Color.magenta);

addAllButton.setBackground(Color.magenta);

clearButton.setBackground(Color.magenta);

printButton.setBackground(Color.magenta);

removeFirst.setBackground(Color.magenta);

removeLast.setBackground(Color.magenta);

buttonPanel.setBackground(new Color(201,178,96));

buttonPanel.add(addButton);

buttonPanel.add(addAllButton);

buttonPanel.add(clearButton);

buttonPanel.add(printButton);

buttonPanel.add(removeFirst);

buttonPanel.add(removeLast);

contentPane.add(buttonPanel,BorderLayout.SOUTH);

ActionListener clearAction =new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

tree.clear();

};

};

clearButton.addActionListener(clearAction);

ActionListener addAction =new ActionListener(){

publicvoid actionPerformed(ActionEvent actionEvent){

String entry = textField.getText().trim();

tree.add(entry);

};

};

addButton.addActionListener(addAction);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

}

thankyou and one last question whats the reason behind entending AbstractListModel like

publicclass TreeSetListextends AbstractListModel

what is AbstractListModel? i searched it on google but cannot find a relevant explanation.

Thanks

[7053 byte] By [lrngjavaa] at [2007-11-27 3:31:52]
# 1

> i was wondering that if i have this code in my program the compiler replies cannot find symbol

Maybe because your parameter is called actionEvent not event!

> the compiler slaps and replies that it treelist is abstract and it does not override actionPerformed.

You must have made your TreeList class implement ActionListener.

floundera at 2007-7-12 8:34:52 > top of Java-index,Java Essentials,New To Java...
# 2

> > i was wondering that if i have this code in my

> program the compiler replies cannot find symbol

>

> Maybe because your parameter is called actionEvent

> not event!

Ah just how stupid and how easy. Thankyou

>

>

> > the compiler slaps and replies that it treelist is

> abstract and it does not override actionPerformed.

>

> You must have made your TreeList class implement

> ActionListener.

No flounder i have my treeList class extends AbstractListModel no implement ActionListener.

lrngjavaa at 2007-7-12 8:34:52 > top of Java-index,Java Essentials,New To Java...
# 3
Hang on! Which class causes the error, TreeList or TreeListTester? Because TreeListTester implements ActionListener but does not have an ActionPerformed method.
floundera at 2007-7-12 8:34:52 > top of Java-index,Java Essentials,New To Java...
# 4
hmm Gotcha i see what you saying. if i implement actionlistener then all i need to do is define actionPerformed method and if i dont implement actionListener then i need to define ActionListener. Thanks for the explanation and lightning response.
lrngjavaa at 2007-7-12 8:34:52 > top of Java-index,Java Essentials,New To Java...
# 5

one more question. i was wondering if i have a search button in my gui and i would like the person to enter a name . if that name exist in my sortedset then i would like to delete that name. do i need to use for loop or iterator and how can i get the text from a user(JOptionPane.InputDialog). and how can i get the text which is already in my swing?

textField.getText()?

lrngjavaa at 2007-7-12 8:34:52 > top of Java-index,Java Essentials,New To Java...