Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By sehudson

Thread: Having trouble with my ListListener

  1. #1
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    28
    Rep Power
    0

    Default Having trouble with my ListListener

    Hi I have created a small GUI that consists of a JList, a JTextField, a JCheckBox and a JButton. The problem I am having is when I select an option in the JList it should then appear in the JTeaxtField but nothing is happening. If you could give me any guidance on this it would be greatly appreciated. The JList is populated from a database and contains clubs, which are football teams.

    here is the code:


    /*
    * This class will create the GUI and its components. It will provide the
    * first interface the user will come across in the application. It will
    * be the settings and team selection window that will act as a
    * gateway for the upcoming windows.
    */

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JList;
    import javax.swing.JScrollBar;
    import javax.swing.JScrollPane;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JCheckBox;
    import javax.swing.JButton;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.ListSelectionModel;
    
    import java.awt.Component;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.util.List;
    import java.util.ArrayList;
    
    
    public class TourneySetUpDisplay extends JFrame {
        
        private List < Club > qualifiedTeams;
        private JPanel centerPanel;
        private JLabel teamPrompt;
        private JList listOfTeams;
        private JList qualifiedTeamsList;
        private JScrollPane teamPane;
        private JScrollBar teamsScrollBar;
        private JLabel participantsLabel;
        private JTextField participantsField;
        private JScrollPane participantsPane;
        private int numberParticipants;
        private JLabel homeAndAwayLabel;
        private JCheckBox homeAndAwayCheck;
        private JButton getFixturesButton;
        private TourneyQueries query;
        
        
        public TourneySetUpDisplay(){
            super( "Tournament" );
            query = new TourneyQueries();
            
            // create centerPanel
            centerPanel = new JPanel();
            centerPanel.setBackground( Color.BLUE );
            centerPanel.setLayout( null );
            
            // create and display prompt above JList
            teamPrompt = new JLabel( "Select Teams:" );
            teamPrompt.setForeground( Color.white );
            teamPrompt.setBounds( 20, 20, 180, 20 );
            centerPanel.add( teamPrompt );
            
             // create, populate and add JList to centerPanel  
            qualifiedTeamsList = new JList();
            listOfTeams = new JList( query.getAllClubs().toArray() );
            teamPane = new JScrollPane( listOfTeams );
            teamPane.setBounds( 20, 50, 180, 60 );
            centerPanel.add( teamPane );
    /*
    * create label and textField that prompt and indicate
    * number of teams taking place in the tournament
    */
    Java Code:
       
            // Label
            participantsLabel = new JLabel( "Qualified Teams:" );
            participantsLabel.setForeground( Color.white );
            participantsLabel.setBounds( 20, 130, 140, 20 );
            centerPanel.add( participantsLabel );
            
            // TextField
            participantsField = new JTextField();
            participantsField.setEditable( false );
            participantsPane = new JScrollPane( participantsField );
            participantsPane.setBounds( 20, 160, 180, 60 );
            centerPanel.add( participantsPane ); 
            
            /*
             * create Label and JCheckBox that prompt the user to 
             * select if the tournament teams play home and away
             */
            
            // Label
            homeAndAwayLabel = new JLabel( "Play Home and Away:" );
            homeAndAwayLabel.setForeground( Color.white );
            homeAndAwayLabel.setBounds( 20, 230, 150, 20 );
            centerPanel.add( homeAndAwayLabel );
            
            // CheckBox
            homeAndAwayCheck = new JCheckBox();
            homeAndAwayCheck.setBackground( Color.BLUE );
            homeAndAwayCheck.setBounds( 180, 230, 20, 20 );
            centerPanel.add( homeAndAwayCheck );
            
            // create and display the getFixtures button
            getFixturesButton = new JButton( "Get Fixtures" );
            getFixturesButton.setBackground( Color.white );
            getFixturesButton.setForeground( Color.BLUE );
            getFixturesButton.setBounds( 20, 260, 180, 20 );
            centerPanel.add( getFixturesButton );
            
            // instantiate qualifiedTeams variable
            qualifiedTeams = new ArrayList();
            
            // add centerPanel to the JFrame
            add( centerPanel );
            
            // set ListSelectionModel and add ListListener
            TeamListListener tll = new TeamListListener();
            listOfTeams.addListSelectionListener( tll );
            
            // add eventListener to CheckBox
    //        homeAndAwayCheck.addActionListener(
    //                new ActionListener() {
    //
    //            @Override
    //            public void actionPerformed( ActionEvent event ) {
    //                
    //                
    //            }
    //        } 
    //        );
    //        
            
            
        } // emd of TourneySetUpDisplay constructor
        
        public void setQualifiedTeams( Object team ){
            
            qualifiedTeams.add( ( Club )team );
            
        } // end of getQualifiedTeams method
        
        public List < Club > getQualifiedTeams(){
            
            return qualifiedTeams;
            
        } // end of getQualifiedTeams
    
    
    class TeamListListener implements ListSelectionListener {
        
        @Override
        public void valueChanged( ListSelectionEvent event ){
            
           int[] selectedIndices = listOfTeams.getSelectedIndices();
           System.out.println("<<<<<<");
           Object [] selections = new Club[ selectedIndices.length ];
    
           
           for( int lcv = 0; lcv < selectedIndices.length; lcv++ ){
               
               selections[ lcv ] = listOfTeams.getModel().getElementAt(
                       selectedIndices[ lcv ] );
    
           } // end of for loop
           
           qualifiedTeamsList.setListData( selections );
           int firstSelectedIndex = listOfTeams.getSelectedIndex();
    
        } // end of valueChanged method
    } // end of MyFirstListListener class
    } // end of TourneySetUpDisplay class
    Last edited by Castro__711; 01-14-2014 at 03:26 PM.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default Re: Having trouble with my ListListener

    Can you wrap your code int code tags? It makes it easier to read.
    Castro__711 likes this.

  3. #3
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    28
    Rep Power
    0

    Default Re: Having trouble with my ListListener

    System.out.println("<<<<<<");
    This was just to demonstrate that each time I selected an option that it was working..

  4. #4
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    28
    Rep Power
    0

    Default Re: Having trouble with my ListListener

    That should be a lot clearer now.

  5. #5
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    28
    Rep Power
    0

    Default Re: Having trouble with my ListListener

    Also each time I select an option it outputs two lines : <<<<<<
    which I am finding really strange?
    Again any guidance or help on this matter would be very welcome...

Similar Threads

  1. ListListener problems
    By danielD in forum AWT / Swing
    Replies: 1
    Last Post: 05-06-2012, 09:26 PM
  2. Io trouble need help
    By loopsnhoops in forum New To Java
    Replies: 18
    Last Post: 06-03-2011, 03:35 AM
  3. Having trouble with IO
    By loopsnhoops in forum New To Java
    Replies: 0
    Last Post: 06-02-2011, 04:06 AM
  4. Im having trouble with.......
    By Java Noobie in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 05:06 PM
  5. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 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
  •