Results 1 to 3 of 3
  1. #1
    m_patten2 is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default Checkboxes and MS Access DB

    Hello Everyone,

    I'm trying to get my program to display whether the Hockey game was won during Overtime or a Shootout. I'm having an issue (brainfart) on how to get the CheckBoxes to be enabled or disabled when accessing an Access DB. If you notice in my code these two lines are commented out:

    //shootoutCheckBox.setSelectedItem( data.getShootout() );
    //overtimeCheckBox.setSelectedItem( data.getOvertime() );

    These are the lines I'm trying to figure out but am having a rough time figuring it out. Any help is greatly appreciated.

    Main File

    Java Code:
    else 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() );
    						//int overtime = Integer.parseInt( overtimeCheckBox.isSelected().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());
    							//shootoutCheckBox.setSelectedItem( data.getShootout() );
    							//overtimeCheckBox.setSelectedItem( data.getOvertime() );
    						}
    						else
    							JOptionPane.showMessageDialog(GameTracker.this, "No matching game found!");
    					}
    Data File

    Java Code:
    public int getOvertime() throws SQLException
    	{
    		if( results == null )
    			return 0;
    		else
    			return results.getInt("Overtime");
    	}
    	
    	public int getShootout() throws SQLException
    	{
    		if( results == null )
    			return 0;
    		else
    			return results.getInt("Shootout");
    	}
    
    // getTeamNames method (to populate team comboboxes)
    	public String[] getTeamNames() throws SQLException
    	{
    		int count = 0;
    		Statement teamsStmt = conn.createStatement();
    		ResultSet teamsRslts = teamsStmt.executeQuery("SELECT COUNT(*) AS NumTeams FROM Teams");
    		if( teamsRslts.next() )
    			count = teamsRslts.getInt("NumTeams");
    		String [] names = new String[count+1];
    		names[0] = " ";
    		teamsRslts = teamsStmt.executeQuery("SELECT TeamName FROM Teams");
    		int idx = 1;
    		while( teamsRslts.next() )
    			names[idx++] = teamsRslts.getString("TeamName");
    		teamsRslts.close();
    		teamsStmt.close();
    		return names;
    	}
    	
    	public int[] getTeamIds() throws SQLException
    	{
    		int count = 0;
    		Statement teamsStmt = conn.createStatement();
    		ResultSet teamsRslts = teamsStmt.executeQuery("SELECT COUNT(*) AS NumTeams FROM Teams");
    		if( teamsRslts.next() )
    			count = teamsRslts.getInt("NumTeams");
    		int [] ids = new int[count+1];
    		ids[0] = 0;
    		teamsRslts = teamsStmt.executeQuery("SELECT TeamID FROM Teams");
    		int idx = 1;
    		while( teamsRslts.next() )
    			ids[idx++] = teamsRslts.getInt("TeamID");
    		teamsRslts.close();
    		teamsStmt.close();
    		return ids;
    	}

  2. #2
    m_patten2 is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default

    A better explanation:

    how can I enable the checkbox as checked or unchecked based on the value I get from database?

  3. #3
    m_patten2 is offline Member
    Join Date
    Sep 2010
    Posts
    9
    Rep Power
    0

    Default Solved

    I was able to figure it out. So I thought I would post the code so others can benefit.

    Main File
    Java Code:
    ( 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() );
    						//int overtime = Integer.parseInt( overtimeCheckBox.isSelected().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() );
    							[COLOR="red"]shootoutCheckBox.setSelected( data.getShootout() );
    							overtimeCheckBox.setSelected( data.getOvertime() );[/COLOR]
    						}
    						else
    							JOptionPane.showMessageDialog(GameTracker.this, "No matching game found!");
    					}
    Data File
    Java Code:
    public boolean getOvertime() throws SQLException
    	{
    		[COLOR="red"]if( results == null )
    			return false;
    		else
    			return results.getBoolean("Overtime");[/COLOR]
    	}
    	
    	public boolean getShootout() throws SQLException
    	{
    		[COLOR="Red"]if( results == null )
    			return false;
    		else
    			return results.getBoolean("Shootout");[/COLOR]
    	}

Similar Threads

  1. Java help...Checkboxes
    By university123 in forum New To Java
    Replies: 32
    Last Post: 10-25-2010, 12:39 PM
  2. ComboBox with CheckBoxes
    By heba.farouk in forum AWT / Swing
    Replies: 14
    Last Post: 06-09-2010, 11:03 AM
  3. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 08:01 PM
  4. How to use Mnemonic for CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:45 PM
  5. How to use Swing CheckBoxes
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:44 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
  •