Results 1 to 4 of 4
  1. #1
    H3rtaherta is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default event handler not working properly

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JButton;
    import javax.swing.JScrollPane;
    import javax.swing.ListSelectionModel;
    
    public class Hw4AtfDepartmentList extends JFrame
    {
    	private JList names;
    	private JButton selectJButton;
    	private final String departmentNames[] = { "CSE", "EE", "Physics", "Biology" };
    	
    	public Hw4AtfDepartmentList( Hw4AtfDepartment department )
    	{	
    		super( "Department Names" );
    		setLayout( new FlowLayout() );
    		
    		names = new JList( departmentNames );
    		names.setVisibleRowCount( 4 );
    		names.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    		
    		add( new JScrollPane( names ) );
    		
    		selectJButton = new JButton( "Select" );
    		
    		selectJButton.addActionListener( 
    		
    			new ActionListener()
    			{
    				public void actionPerformed ( ActionEvent event )
    				{
    					System.out.println( "U pressed the button" );
    				}
    			}
    		);
    		add(selectJButton);
    	}
    
    	
    }
    this is not all the code, theres about 7 different files to post so this wont compile on its own. However, the gui appeared properly with the list and the select button is next to the list like it should. However, when I press select it will not print out "U pressed the button". Why wont it work?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You have a bug, but I don't see the bug in the code posted above. It makes me wonder when you pass an array of this class to itself in its constructor. Could you have multiple instances of this class in your program, one shadowing the other?

  3. #3
    H3rtaherta is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Its not passing it's self. The class names are different. One has list at the end.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah true. anyway, the above code works, so the bug is elsewhere.

Similar Threads

  1. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 01:00 AM
  2. Menu item not working properly for mouse events
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 09:56 AM
  3. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 06:48 PM
  4. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 PM
  5. Help with Handler
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-04-2007, 10:42 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
  •