Results 1 to 3 of 3
Thread: GUI Listener not Listening
- 11-23-2010, 11:42 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
GUI Listener not Listening
Hello everyone. I am attempting to create an NHL game tracker. For some reason my GUI listener isn't recognizing when I click a button. The only buttons that I have active thus far are Clear, Closed, and Standings. What might cause this? How can I fix it?
Java Code:/** * Module: GameTracker.java * Author: M. Patten * Date: 2010-11-04 * Description: Program to track game information for NHL teams. This program will * also access a database using JDBC. **/ import java.awt.HeadlessException; import java.awt.event.*; import javax.swing.*; public class GameTracker extends JFrame { private static final long serialVersionUID = 11; private static final double FR_WIDTH_FACTOR = 0.275; private static final double FR_HEIGHT_FACTOR = 0.294; // Create Arrays for each ComboBox private final static String[] MONTH = { "Month", "January", "February", "March", "April", "May", "June", "July", "Augaust", "September", "October", "November", "December" }; private final static String[] DAY = { "Day", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }; private final static String[] YEAR = { "Year", "2010", "2011" }; private String [] teamArray; private int [] teamIdArray; // GUI Items private JLabel dateLabel = new JLabel("<< Date >>"); private JComboBox monthCombo = new JComboBox(MONTH); private JComboBox dayCombo = new JComboBox(DAY); private JComboBox yearCombo = new JComboBox(YEAR); private JLabel homeLabel = new JLabel("HOME", JLabel.CENTER); private JLabel awayLabel = new JLabel("AWAY", JLabel.CENTER); private JLabel teamLabel = new JLabel("<< Team >>"); private JComboBox homeCombo = new JComboBox(); private JComboBox awayCombo = new JComboBox(); private JLabel goalsLabel = new JLabel("Goals"); private JTextField goalHomeField = new JTextField("", 3); private JTextField goalAwayField = new JTextField("", 3); private JCheckBox overtimeCheckBox = new JCheckBox("Overtime?"); private JCheckBox shootoutCheckBox = new JCheckBox("Shootout?"); public JButton addButton = new JButton("Add"); public JButton clearButton = new JButton ("Clear"); private JButton deleteButton = new JButton("Delete"); private JButton retrieveButton = new JButton("<< Retrieve >>"); private JButton standingsButton = new JButton("Standings"); public JButton closeButton = new JButton("Close"); // Data object private NHLData data = null; private GUIListener listener = new GUIListener(); public GameTracker() throws HeadlessException { // Set-up basic JFrame this.setTitle("NHL Game Tracker"); int frWidth = (int) (FR_WIDTH_FACTOR * this.getToolkit().getScreenSize().width); int frHeight = (int) (FR_HEIGHT_FACTOR * this.getToolkit().getScreenSize().height); this.setSize(frWidth, frHeight); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); // Create the NHLData object try { data = new NHLData(); teamArray = data.getTeamNames(); teamIdArray = data.getTeamIds(); if( teamArray.length > 0 ) { homeCombo = new JComboBox(teamArray); awayCombo = new JComboBox(teamArray); } } catch(Exception ex) { JOptionPane.showMessageDialog(this, ex.getMessage()); } // Create group layout GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); // Set horizontal group layout layout.setHorizontalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(dateLabel) .addComponent(teamLabel) .addComponent(goalsLabel) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(addButton) .addComponent(clearButton))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(monthCombo) .addComponent(dayCombo)) .addComponent(homeLabel) .addComponent(homeCombo) .addComponent(goalHomeField) .addComponent(overtimeCheckBox) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(deleteButton) .addComponent(retrieveButton))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(yearCombo) .addComponent(awayLabel) .addComponent(awayCombo) .addComponent(goalAwayField) .addComponent(shootoutCheckBox) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(standingsButton) .addComponent(closeButton))) ); // Make all the components listed below the same size. layout.linkSize(SwingConstants.HORIZONTAL, homeCombo, awayCombo, addButton, retrieveButton, clearButton, standingsButton, deleteButton, closeButton); layout.linkSize(SwingConstants.HORIZONTAL, dayCombo, yearCombo); layout.linkSize(SwingConstants.HORIZONTAL, goalHomeField, goalAwayField); layout.linkSize(SwingConstants.HORIZONTAL,monthCombo); // Set vertical group layout layout.setVerticalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(dateLabel) .addComponent(monthCombo) .addComponent(dayCombo) .addComponent(yearCombo)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(homeLabel) .addComponent(awayLabel)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(teamLabel) .addComponent(homeCombo) .addComponent(awayCombo)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(goalsLabel) .addComponent(goalHomeField) .addComponent(goalAwayField)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(shootoutCheckBox) .addComponent(overtimeCheckBox)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addGap(5, 5, 40) .addComponent(addButton) .addComponent(deleteButton) .addComponent(standingsButton)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(clearButton) .addComponent(retrieveButton) .addComponent(closeButton)))) ); // Display the JFrame this.pack(); this.setVisible(true); } public static void main(String[] args) { new GameTracker(); } private class GUIListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { if( e.getSource() == retrieveButton ) { // Obtain the teamID from one of the team fields int teamIndex = homeCombo.getSelectedIndex(); if( teamIdArray[teamIndex] == 0 ) teamIndex = awayCombo.getSelectedIndex(); if( teamIdArray[teamIndex] == 0 ) JOptionPane.showMessageDialog(GameTracker.this, "No team is selected!"); else { String month = monthCombo.getSelectedItem().toString(); int day = Integer.parseInt( dayCombo.getSelectedItem().toString() ); int year = Integer.parseInt( yearCombo.getSelectedItem().toString() ); if( data.findGame(month, year, day, teamIdArray[teamIndex]) ) { // Find the correct home team int teamID = data.getHomeTeamId(); teamIndex = 0; for(; teamIndex < teamIdArray.length; teamIndex ++) { if( teamIdArray[teamIndex] == teamID ) break; } homeCombo.setSelectedIndex(teamIndex); // Find the correct away team teamID = data.getAwayTeamId(); teamIndex = 0; for(; teamIndex < teamIdArray.length; teamIndex ++) { if( teamIdArray[teamIndex] == teamID ) break; } awayCombo.setSelectedIndex(teamIndex); goalHomeField.setText("" + data.getHomeScore()); goalAwayField.setText("" + data.getAwayScore()); } else JOptionPane.showMessageDialog(GameTracker.this, "No matching game found!"); } } else if( e.getSource() == clearButton ) { // Clear the GUI's fields monthCombo.setSelectedItem(" "); dayCombo.setSelectedItem(" "); yearCombo.setSelectedItem(" "); homeCombo.setSelectedItem(" "); awayCombo.setSelectedItem(" "); goalHomeField.setText(" "); goalAwayField.setText(" "); } else if( e.getSource() == standingsButton ) { // Display the team standings new Standings( data.getConnection() ); } } catch(Exception ex) { JOptionPane.showMessageDialog(GameTracker.this, ex.getMessage()); } } } }
-
I don't see where you've added an ActionListener to a JButton anywhere in your (lots of) code. Buttons won't know that you want them listened to unless you give them a listener via the addActionListener(ActionListener al) method. Please review the Swing tutorials on JButtons and on creating an ActionListener for the gory details.
Much luck!Last edited by Fubarable; 11-24-2010 at 12:45 AM.
- 11-24-2010, 01:06 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Glassfish 3 listening to ActiveMQ
By jean_bergstrom in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 10-05-2010, 04:06 PM -
Listening rows in JTable ??
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-10-2010, 04:45 PM -
Listening for variables?
By martypapa in forum New To JavaReplies: 6Last Post: 02-09-2010, 09:14 AM -
MDB listening
By nipunalk in forum Advanced JavaReplies: 5Last Post: 01-20-2010, 05:10 AM -
Listening to a Printer
By Mosd in forum New To JavaReplies: 0Last Post: 08-12-2008, 12:21 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks