Results 1 to 3 of 3
Thread: Checkboxes and MS Access DB
- 11-25-2010, 10:49 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
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
Data FileJava 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!"); }
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; }
- 11-25-2010, 11:15 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
A better explanation:
how can I enable the checkbox as checked or unchecked based on the value I get from database?
- 11-26-2010, 01:56 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
Solved
I was able to figure it out. So I thought I would post the code so others can benefit.
Main File
Data FileJava 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!"); }
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
-
Java help...Checkboxes
By university123 in forum New To JavaReplies: 32Last Post: 10-25-2010, 12:39 PM -
ComboBox with CheckBoxes
By heba.farouk in forum AWT / SwingReplies: 14Last Post: 06-09-2010, 11:03 AM -
CheckBoxes and JTables
By lakshayghai in forum AWT / SwingReplies: 1Last Post: 03-16-2010, 08:01 PM -
How to use Mnemonic for CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:45 PM -
How to use Swing CheckBoxes
By Java Tip in forum javax.swingReplies: 0Last Post: 06-27-2008, 07:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks