Hello Everyone,
I am trying to create a GroupLayout and everytime the program goes to execute it crashes. I am not sure as to why but it is related to my GroupLayout. Below is my code and the errors I'm presented with. Don't mind the JDBC stuff either.
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.sql.*;
import javax.swing.*;
public class GameTracker extends JFrame
{
private static final long serialVersionUID = 11;
private static final double FR_WIDTH_FACTOR = 0.4;
private static final double FR_HEIGHT_FACTOR = 0.47;
// 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 final static String[] HOME = { "Home" };
private final static String[] AWAY = { "Away" };
public GameTracker()
{
// Make connection to database
JDBC();
// 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);
// First Group
JLabel dateLabel = new JLabel("<< Date >>(month/day/year)");
JComboBox monthCombo = new JComboBox(MONTH);
JComboBox dayCombo = new JComboBox(DAY);
JComboBox yearCombo = new JComboBox(YEAR);
// Second Group
JLabel homeLabel = new JLabel("HOME");
JLabel awayLabel = new JLabel("AWAY");
JLabel teamLabel = new JLabel("<< Team >>");
JComboBox homeCombo = new JComboBox(HOME);
JComboBox awayCombo = new JComboBox(AWAY);
// Third Group
JLabel goalsLabel = new JLabel("Goals");
JTextField goalOneField = new JTextField();
JTextField goalTwoField = new JTextField();
// Fourth Group
JCheckBox overtimeCheckBox = new JCheckBox("Overtime?");
JCheckBox shootoutCheckBox = new JCheckBox("Shootout?");
// Fifth Group
JButton addButton = new JButton("Add");
JButton clearButton = new JButton("Clear");
JButton deleteButton = new JButton("Delete");
JButton retrieveButton = new JButton("<< Retrieve >>");
JButton standingsButton = new JButton("Standings");
JButton closeButton = new JButton("Close");
// Remove redundant default border of check boxes
overtimeCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
shootoutCheckBox.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
// 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()
.addComponent(dateLabel)
.addComponent(teamLabel)
.addComponent(goalsLabel)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(monthCombo)
.addComponent(homeLabel)
.addComponent(homeCombo)
.addComponent(goalOneField)
.addComponent(overtimeCheckBox))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(dayCombo)
.addComponent(yearCombo))
.addComponent(awayLabel)
.addComponent(awayCombo)
.addComponent(goalTwoField)
.addComponent(shootoutCheckBox))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(addButton)
.addComponent(retrieveButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(clearButton)
.addComponent(standingsButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(deleteButton)
.addComponent(closeButton)))
);
// Make all the components listed below the same size.
layout.linkSize(SwingConstants.HORIZONTAL, monthCombo, homeCombo, awayCombo, addButton,
retrieveButton, clearButton, standingsButton, deleteButton, closeButton);
// Set vertical group layout
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(dateLabel)
.addComponent(monthCombo)
.addComponent(dayCombo)
.addComponent(yearCombo))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(homeLabel)
.addComponent(awayLabel))))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(teamLabel)
.addComponent(homeCombo)
.addComponent(awayCombo))))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(goalsLabel)
.addComponent(goalOneField)
.addComponent(goalTwoField))))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(addButton)
.addComponent(clearButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(deleteButton)
.addComponent(retrieveButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(standingsButton)
.addComponent(closeButton))))
);
}
public static void main(String[] args)
{
new GameTracker().setVisible(true);
}
// JDBC Connection
private void JDBC()
{
Connection conn = null;
Statement stmt = null;
ResultSet results = null;
try
{
// Step 1 - Load the database driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2 - Get a connection
conn = DriverManager.getConnection("jdbc:odbc:NHLDataSource");
// Step 3 - Create a statement object
stmt = conn.createStatement();
// Step 4 - Execute a query
results = stmt.executeQuery("SELECT GameID FROM Games");
// Step 5 - Process the results (display all fields in all records)
while( results.next() )
{
System.out.println( results.getInt(1) );
}
}
catch(ClassNotFoundException ex)
{
System.out.println(ex.getMessage());
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
finally
{
try
{
// Step 6 - Close the ResultSet object
if( results != null )
results.close();
// Step 7 - Close the Statement object
if( stmt != null )
stmt.close();
// Step 9 - Close the Connection object
if( conn != null )
conn.close();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
}
Quote:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JCheckBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,bor der=javax.swing.border.EmptyBorder@313a53d,flags=2 96,maximumSize=,minimumSize=,preferredSize=,defaul tIcon=,disabledIcon=,disabledSelectedIcon=,margin= javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,ro lloverEnabled=true,rolloverIcon=,rolloverSelectedI con=,selectedIcon=,text=Shootout?] is not attached to a vertical group
at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at GameTracker.main(GameTracker.java:158)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JCheckBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,bor der=javax.swing.border.EmptyBorder@313a53d,flags=2 96,maximumSize=,minimumSize=,preferredSize=,defaul tIcon=,disabledIcon=,disabledSelectedIcon=,margin= javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=false,paintFocus=true,pressedIcon=,ro lloverEnabled=true,rolloverIcon=,rolloverSelectedI con=,selectedIcon=,text=Shootout?] is not attached to a vertical group
at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
