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

