Results 1 to 4 of 4
  1. #1
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default problems with my ActionListener

    I don't know why but the code does not like my ActionListener. It looks identical to a similar code I compiled (where there are no issues there) but it just doesn't seem to like this code.

    What I am trying to do is to:
    1. read a file (successful)
    2. put into arrays (successful)
    3. in the interface, enter a title and see if it is in the btitle array (unsuccessful).

    Here is my code:

    Java Code:
    public class Homework2b_GK extends JFrame {
    
    	private JPanel panel;
    	JPanel resultPanel;
    	private JButton button;
    	private JCheckBox boldcheck;
    	private JCheckBox redcolor;
    	private JTextField bookTitle;
    	private JLabel titleLabel;
    	private JLabel authorLabel;
    	private JLabel availableLabel;
    	private JLabel searchLabel;
    	private JLabel resultLabel;
    
    	String title;
    	String author;
    	String available;
    	String bookRecord;
    	ArrayList btitle;
    	ArrayList bauthor;
    	ArrayList bavailable;
    
    	public Homework2b_GK(String str) {
    		super(str);
    
    	}
    
    	public static void main(String[] args) {
    
    		Homework2b_GK myGUI = new Homework2b_GK("Show book records");
    		myGUI.readFromFile();
    		myGUI.createGUI();
    
    	}
    
    	public void readFromFile() {
    		FileReader text;
    		ArrayList<String> btitle = new ArrayList();
    		ArrayList<String> bauthor = new ArrayList();
    		ArrayList<String> bavailable = new ArrayList();
    
    		try {
    			FileReader file = new FileReader("book.txt");
    			BufferedReader buff = new BufferedReader(file);
    			boolean eof = false;
    			int i = 0;
    			while (!eof) {
    				String line = buff.readLine();
    
    				if (line == null)
    					eof = true;
    				else {
    
    					String parts[] = line.split(";");
    					btitle.add(parts[0]);
    					bauthor.add(parts[1]);
    					bavailable.add(parts[2]);
    				}
    			}
    		}
    
    		catch (IOException ex) {
    			ex.printStackTrace();
    		}
    		// getting size of array and testing if all titles entered
    		int size = btitle.size();
    		System.out.println(size);
    		for (int s = 0; s < size; s++) {
    			System.out.println(btitle.get(s));
    		}
    	}
    
    	
    		
    
    	public void createGUI() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Container contentPane = getContentPane();
    		contentPane.setLayout(new BorderLayout());
    
    		titleLabel = new JLabel(title);
    		authorLabel = new JLabel(author);
    		availableLabel = new JLabel(available);
    
    		searchLabel = new JLabel("Book title:");
    		button = new JButton("Search");
    		this.add(button);
    		button.addActionListener(new SearchEntry());
    
    		contentPane.setLayout(new BorderLayout());
    		JPanel searchPanel = new JPanel();
    
    		bookTitle = new JTextField(20);
    		searchPanel.add(searchLabel);
    		searchPanel.add(bookTitle);
    		searchPanel.add(button);
    
    		JPanel resultPanel = new JPanel();
    		resultLabel = new JLabel("Results");
    
    		JPanel optionPanel = new JPanel();
    		boldcheck = new JCheckBox("Bold");
    		redcolor = new JCheckBox("Red Color");
    
    		optionPanel.add(boldcheck);
    		optionPanel.add(redcolor);
    
    		resultPanel.add(resultLabel);
    		contentPane.add(resultPanel, BorderLayout.NORTH);
    
    		contentPane.add(searchPanel, BorderLayout.CENTER);
    		contentPane.add(optionPanel, BorderLayout.PAGE_END);
    
    		setSize(500, 400);
    		setVisible(true);
    
    	}
    
    
    public class SearchEntry implements ActionListener {
    		
    		public void ActionPerformed(ActionEvent evt){
    			//getting book title from text field and to see if it is in the book title array
    			title=bookTitle.getText();
    			int size = btitle.size();
    			System.out.println(title);
    			
    			for (int s = 0; s < size; s++){
    				if (title==btitle.get(s)){
    					System.out.println("found");
    				}else {
    					System.out.println("not found");
    				}
    			}
    				
    				
    			
    			}
    	}
    
    }

    Here is the output I am getting:
    Java Code:
    3
    t1
    t2
    t3
    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
    	The type Homework2b_GK.SearchEntry must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)
    
    	at Homework2b_GK$SearchEntry.actionPerformed(Homework2b_GK.java:148)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Java is case sensitive...take a look at the API for ActionListener ActionListener (Java Platform SE 6)
    ActionPerformed is not defined (actionPerformed is)

  3. #3
    xyknight is offline Member
    Join Date
    Mar 2011
    Posts
    29
    Rep Power
    0

    Default

    Thank you. I fixed that but still get the following output:

    Java Code:
    3
    t1
    t2
    t3
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at Homework2b_GK$SearchEntry.actionPerformed(Homework2b_GK.java:153)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Have a close look at your readFromFile method - you don't instantiate the instance variables for all the lists, but rather create local ones which loose scope when the method exits (in other words the instance variables are null). Further, if you want to compare strings, do so using the equals method and not ==

Similar Threads

  1. Swing/ActionListener help?
    By XelaIsPwn in forum AWT / Swing
    Replies: 4
    Last Post: 03-01-2011, 02:58 AM
  2. ActionListener issue.
    By kbro3 in forum New To Java
    Replies: 14
    Last Post: 02-24-2011, 06:08 AM
  3. JButton ActionListener problems
    By Fortu in forum New To Java
    Replies: 6
    Last Post: 02-05-2011, 04:20 PM
  4. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 08:36 AM
  5. Demonstrating the ActionListener
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 04-23-2008, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •