PDA

Click to See Complete Forum and Search --> : [RESOLVED] JAVA 6 GroupLayout


newmember
Sep 27th, 2008, 01:59 AM
Hello,
could anyone please tell me what is wrong with this code
code compiles but throw exception at runtime.

import java.awt.*;
import javax.swing.*;
public class TestGroup extends JFrame{
JTextField UserText, PasswordText ;
JLabel UserLabel, PasswordLabel ;
JPanel pnl ;
public static void main( String[] args ) {
TestGroup test = new TestGroup();
test.start();
}

void start(){
this.getContentPane().setLayout( new FlowLayout() ) ;

UserText = new JTextField() ;
PasswordText = new JTextField() ;
pnl = new JPanel() ;

GroupLayout lout = new GroupLayout( pnl ) ;
pnl.setLayout( lout ) ;
lout.setAutoCreateGaps( true ) ;
lout.setAutoCreateContainerGaps( true ) ;
GroupLayout.SequentialGroup hgroup = lout.createSequentialGroup() ;
hgroup.addGroup( lout.createParallelGroup().addComponent(UserLabel).addComponent( PasswordLabel ) ) ;
hgroup.addGroup( lout.createParallelGroup().addComponent( UserText).addComponent( PasswordText ) );

lout.setHorizontalGroup( hgroup ) ;



GroupLayout.SequentialGroup vgroup = lout.createSequentialGroup() ;

vgroup.addGroup( lout.createParallelGroup().addComponent( UserLabel ).addComponent( UserText ) ) ;
vgroup.addGroup( lout.createParallelGroup().addComponent( PasswordLabel ).addComponent( PasswordText ) );

lout.setVerticalGroup( vgroup ) ;

this.getContentPane().add( pnl ) ;
this.setSize( 500, 500 ) ;
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
this.setVisible( true );
}
}






Error :

Exception in thread "main" java.lang.IllegalArgumentException: Component must be
non-null
at javax.swing.GroupLayout$ComponentSpring.<init>(Unknown Source)
at javax.swing.GroupLayout$ComponentSpring.<init>(Unknown Source)
at javax.swing.GroupLayout$Group.addComponent(Unknown Source)
at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source)
at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source)
at javax.swing.GroupLayout$Group.addComponent(Unknown Source)
at javax.swing.GroupLayout$ParallelGroup.addComponent(Unknown Source)
at TestGroup.start(TestGroup.java:24)
at TestGroup.main(TestGroup.java:9)

ComputerJy
Sep 27th, 2008, 06:35 AM
You forgot to initialize the components PasswordLabel and UserLabel before using them

newmember
Sep 27th, 2008, 02:19 PM
oooooooooops silly mistake

thank you very much, my focus was on the grouplayout thats why i could not see that. it was running well if i comment that grouplayout portion so i thought i have done something wrong there only.

thanks again.