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

    Default JList and JButton event handler not working

    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()
    	{	
    		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" );
    		
    		ButtonHandler handler = new ButtonHandler();
    		selectJButton.addActionListener( handler );
    		add(selectJButton);
    		System.out.println( "ugh" );
    		
    		
    	}
    	
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed( ActionEvent event )
    		{
    			System.out.println( "Pressed" );
    			if( getSelectedIndex() == 0 )
    			{
    				department.setName( "CSE" );
    				System.out.printf( "%s", department.getName() );
    			}
    			if( getSelectedIndex() == 1 )
    			{
    				department.setName( "EE" );
    			}
    			if( getSelectedIndex() == 2 )
    			{
    				department.setName( "Physics" );
    			}
    			if( getSelectedIndex() == 3 )
    			{
    				department.setName( "Biology" );
    			} 
    			
    		}
    	}
    	
    }
    When I click the JButton "select" I dont think it is registering it as an event. The system.out.println() is not showing up.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    I think you need either selectJButton.getSelectedIndex() or event.getSelectedIndex()

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

    Default

    Does your code even compile? I'm getting a lot of compile errors as you seem to be calling methods and referencing objects that don't exist.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    i think that getSelectedIndex() is referred to the JList names,so it should be
    Java Code:
    ..................
    names.getSelectedIndex()..........
    But what is that object with the name department which has also the getName() mehtod?

Similar Threads

  1. Jbutton not working??
    By rmabrey in forum AWT / Swing
    Replies: 7
    Last Post: 09-24-2008, 05:30 PM
  2. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 09:24 PM
  3. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  4. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:15 AM
  5. Help with Handler
    By baltimore in forum AWT / Swing
    Replies: 1
    Last Post: 08-04-2007, 09: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
  •