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
No comments:
Post a Comment