Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Need help and advice with ListSelectionListener PLEASE

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

    Default Need help and advice with ListSelectionListener PLEASE

    Hi I am struggling with a part of my personal project and could really do with some help please.

    I am using a JList, it is populated by objects from a database. Below it there is a JTextArea and what I would like the program to do is to make the object appear in the JTextArea when it has been selected (even a type String version will do at this stage). At the moment nothing is appearing in the JTextArea. I put a print statement in the valueChanged method to ensure when selected the program is picking it up and it is but to my surprise each time I select an option it outputs two lines( <<<<<<< ) which seems like it is registering twice but still no text in the JTextArea.

    Below is my code if you have any tips, help, advice anything please do comment.

    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.JTextArea;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.SwingConstants;
    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 JTextArea participantsArea;
        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 );
    
            Icon fifa = new ImageIcon( 
                    getClass().getResource( "fifa14JOP.jpg" ));
            teamPrompt = new JLabel( "Select Teams:" );
            teamPrompt.setForeground( Color.white );
            teamPrompt.setIcon( fifa );
            teamPrompt.setIconTextGap( 55 );
            teamPrompt.setHorizontalTextPosition(
                    SwingConstants.LEFT );
            teamPrompt.setVerticalTextPosition( 
                    SwingConstants.CENTER );
            teamPrompt.setBounds( 20, 20, 220, 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 );
            
            // Label
            participantsLabel = new JLabel( "Qualified Teams:" );
            participantsLabel.setForeground( Color.white );
            participantsLabel.setBounds( 20, 130, 140, 20 );
            centerPanel.add( participantsLabel );
            
            // TextField
            participantsArea = new JTextArea();
            participantsArea.setEditable( true );
            participantsPane = new JScrollPane( participantsArea );
            participantsPane.setBounds( 20, 160, 180, 60 );
            centerPanel.add( participantsPane ); 
            
            // 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 );
            
        } // end of TourneySetUpDisplay constructor
        
    
    
    class TeamListListener implements ListSelectionListener {
        
        @Override
        public void valueChanged( ListSelectionEvent event ){
            
           int[] selectedIndices = listOfTeams.getSelectedIndices();
           System.out.println( "<<<<<<" );
           Object [] selections = new Club[ selectedIndices.length ];
           String [] stringSelections = new String[ 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

  2. #2
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    33
    Rep Power
    0

    Default Re: Need help and advice with ListSelectionListener PLEASE

    Here is an image of the GUI

    Need help and advice with ListSelectionListener PLEASE-tourneydisplay.png

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,730
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help and advice with ListSelectionListener PLEASE

    A ListSelectionListener is called whenever a list selection changes; when you select an(other) item in your list, the originally selected item will be deselected (the first event) and the other item is selected (the second event). Your listener should check if the indicated item is selected or not.

    kind regards,

    Jos
    Castro__711 likes this.
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Need help and advice with ListSelectionListener PLEASE

    Thanks for that Jos its really helpful. Should anyone else have an input please do not be shy.

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

    Default Re: Need help and advice with ListSelectionListener PLEASE

    Quote Originally Posted by JosAH View Post
    A ListSelectionListener is called whenever a list selection changes; when you select an(other) item in your list, the originally selected item will be deselected (the first event) and the other item is selected (the second event). Your listener should check if the indicated item is selected or not.

    kind regards,

    Jos
    How does the first time I select an item return two outputs if I have not selected the second item?
    Also if you know anymore as to why it will not add the objects to my JTextArea could you please enlighten me, its making me go a little crazy..

  6. #6
    Join Date
    Dec 2013
    Location
    Dublin
    Posts
    33
    Rep Power
    0

    Default Re: Need help and advice with ListSelectionListener PLEASE

    Hi Jos

    I am happy and sane again :) I have got it working... It was something silly which I am sure you probably seen within the code.. I guess sometimes you should just step back for awhile take a breather and look over the simple things... It can be frustrating when you are stuck but the rush when you overcome it is pretty nice... Take care and happy coding....

Similar Threads

  1. Replies: 5
    Last Post: 07-09-2014, 06:43 PM
  2. hi need some advice
    By addrianbfr in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 05-10-2012, 09:39 AM
  3. Advice
    By tabchas in forum New To Java
    Replies: 1
    Last Post: 07-25-2011, 09:59 AM
  4. How to use ListSelectionListener in Jtable?
    By Vignesh Karthick in forum AWT / Swing
    Replies: 0
    Last Post: 02-10-2011, 06:01 AM
  5. Writing a custom ListSelectionListener
    By Muskar in forum New To Java
    Replies: 0
    Last Post: 12-20-2010, 08:39 AM

Tags for this Thread

Posting Permissions

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