Results 1 to 3 of 3
Thread: GroupLayout Issue
- 11-10-2010, 09:14 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
GroupLayout Issue
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.
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.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()); } } } }[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)
-
I'm no expert on GroupLayout and in fact avoid it since it was built to be used and modified by GUI-creating programs such as NetBeans Matisse, and not for direct fiddling by humans, but one thing I can confidently recommend is that you separate out your database code from your GUI. Get them in separate classes and test them separately.
As for layouts, consider nesting JPanels that use a simpler layouts such as BorderLayout, BoxLayout, GridLayout, and yes even GridBagLayout. Also consider using MigLayout.
Luck!
- 11-10-2010, 10:50 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
GroupLayouts is the direction I'm taking...
I appreciated your input but I already know how to work with the other layouts. I would like to learn how to work with GroupLayouts, and I use Eclipse. :)
Anyways I was able to fix the error I was getting previously. Forgot to add in one of the combo boxes to the verticle.
The issue I'm having now is that the buttons are staying to the right side and seem to be piling on top of one another.
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.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() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .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, homeCombo, awayCombo, addButton, retrieveButton, clearButton, standingsButton, deleteButton, closeButton); layout.linkSize(SwingConstants.HORIZONTAL, dayCombo, yearCombo); // Set vertical group layout layout.setVerticalGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(dateLabel) .addComponent(monthCombo) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .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(shootoutCheckBox) .addComponent(overtimeCheckBox)))) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(addButton) .addComponent(retrieveButton) .addComponent(closeButton))) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(deleteButton) .addComponent(clearButton) .addComponent(standingsButton)))) ); } 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()); } } } } /* .addComponent(dateLabel) .addComponent(monthCombo) .addComponent(dayCombo) .addComponent(yearCombo) .addComponent(homeLabel) .addComponent(awayLabel) .addComponent(teamLabel) .addComponent(homeCombo) .addComponent(awayCombo) .addComponent(goalsLabel) .addComponent(goalOneField) .addComponent(goalTwoField) .addComponent(overtimeCheckBox) .addComponent(shootoutCheckBox) .addComponent(addButton) .addComponent(clearButton) .addComponent(deleteButton) .addComponent(retrieveButton) .addComponent(standingsButton) .addComponent(closeButton) */
Similar Threads
-
Repaint the entire JFrame (GroupLayout)
By Willi in forum AWT / SwingReplies: 13Last Post: 12-19-2009, 10:11 PM -
Anyone know how GroupLayout works?
By ProgrammingPup in forum Advanced JavaReplies: 5Last Post: 12-01-2009, 11:12 PM -
Doubt with GroupLayout
By emylyano3 in forum AWT / SwingReplies: 1Last Post: 06-28-2009, 02:16 AM -
help about class GroupLayout
By MaHa in forum NetBeansReplies: 2Last Post: 09-07-2008, 11:25 PM -
[SOLVED] Alignment in GroupLayout
By nanou in forum AWT / SwingReplies: 8Last Post: 09-03-2008, 03:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks