Grid Bag Layout

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Compiled Code)
at java.awt.Container.add(Container.java:245)
at Login.addC(Login.java:37)
at Login.(init)(Login.java:28)
at Login.main(Login.java:44)
I get this error and it hangs all the time...what's wrong with it? I'm new to Java and this simple program is really giving me problems
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame
implements ActionListener{
public Login(){
setTitle("HyperSmart Login");
Container contentPane = getContentPane();
GridBagLayout gblLogin = new GridBagLayout();
GridBagConstraints gbcLogin = new GridBagConstraints();
contentPane.setLayout(gblLogin);
gbcLogin.fill = GridBagConstraints.BOTH;
gbcLogin.weightx = 0;
gbcLogin.weighty = 100;
JLabel lblUserName = new JLabel("Username");
JTextField txtUserName = new JTextField();
JLabel lblPassword = new JLabel("Password");
JPasswordField txtPassword = new JPasswordField("", 10);
addC(lblUserName, gbcLogin, 0, 0, 1, 1);
addC(txtUserName, gbcLogin, 0, 1, 3, 1);
addC(lblPassword, gbcLogin, 1, 0, 1, 1);
addC(txtPassword, gbcLogin, 1, 1, 3, 1);
addC(OKButton, gbcLogin, 2, 2, 1, 1);
public void addC(Component c, GridBagConstraints gbcLogin,
int x, int y, int w, int h){
gbcLogin.gridx = x;
gbcLogin.gridy = y;
gbcLogin.gridwidth = w;
gbcLogin.gridheight =h;
getContentPane().add(c, gbcLogin);
public void actionPerformed(ActionEvent e){
public static void main(String[] args){
Login frmLogin = new Login();
frmLogin.show();
JButton OKButton;

you get nullPointer because OKButton is null.
after
JPasswordField txtPassword = new JPasswordField("", 10);
just write OKButton = new JButton("OK");

Similar Messages

  • Setting Grid Bag Layout Constraints

    Hi any body can tell me whether the following gridbag constraints are correct or wrong in the java class.
    My problem is that the two butons (cancel and submit) are not displayed i am using Eclipse Ide Visual editor 2.1.
    Please any body can help me out if there are any changes to be done .
    * Created on May 25, 2004
    * To change the template for this generated file go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    package demo;
    import java.awt.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    * @author kv5459
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    public class DataBase extends JFrame {
         private javax.swing.JPanel jContentPane = null;
         JPanel XXqqds_GridField2;
         JPanel XXqqds_GridField3;
         JLabel XXqqds_TextGraphic4;
         JTextField XXqqds_DataField5;
         JLabel XXqqds_TextGraphic6;
         JTextField XXqqds_DataField7;
         JPanel XXqqds_GridField8;
    JButton XXqqds_PushButton9;
         JButton XXqqds_PushButton10;
         * This is the default constructor
         public DataBase() {
              super();
              initialize();
         * This method initializes this
         * @return void
         private void initialize() {
              this.setSize(500, 375);
              this.setContentPane(getJContentPane());
              setTitle("");
              setLocale(java.util.Locale.getDefault());
              setResizable(true);
              setState(Frame.NORMAL);
              setSize(500,375);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
         * This method initializes jContentPane
         * @return javax.swing.JPanel
         private javax.swing.JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new javax.swing.JPanel();
                   jContentPane.setLayout(null);
              XXqqds_GridField2= new JPanel();
              XXqqds_GridField2.setBackground(getContentPane().getBackground());
              XXqqds_GridField2.setForeground(getContentPane().getForeground());
              XXqqds_GridField2.setLayout(new GridBagLayout());
              jContentPane.add(XXqqds_GridField2);
              XXqqds_GridField3= new JPanel();
              XXqqds_GridField3.setBackground(XXqqds_GridField2.getBackground());
              XXqqds_GridField3.setForeground(XXqqds_GridField2.getForeground());
              XXqqds_GridField3.setLayout(new GridBagLayout());
              XXqqds_GridField3.setBounds(new Rectangle(0,0,175,39));
              GridBagConstraints c1 = new GridBagConstraints();
              c1.gridx = 0;
              c1.gridy = 0;c1.gridwidth = 2;
              c1.anchor = java.awt.GridBagConstraints.CENTER;
              c1.weightx=0;c1.weighty=0;
              c1.fill = java.awt.GridBagConstraints.NONE;
              XXqqds_GridField3.setPreferredSize(new java.awt.Dimension(205,192));
              c1.ipadx = 0;c1.ipady = 0;
              XXqqds_TextGraphic4= new JLabel();
              XXqqds_TextGraphic4.setText("Name");
              XXqqds_TextGraphic4.setBackground(XXqqds_GridField3.getBackground());
              XXqqds_TextGraphic4.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_TextGraphic4.setBounds(new Rectangle(13,2,49,15));
              GridBagConstraints c2 = new GridBagConstraints();
              c2.gridx=0;
              c2.gridy=0;
              c2.gridwidth=1;c2.gridheight=1;
              c2.anchor = java.awt.GridBagConstraints.NORTHWEST;
              c2.weightx = 0.0D;c2.weighty = 1.0D;
              c2.ipadx=0;c2.ipady=0;
              c2.fill = java.awt.GridBagConstraints.NONE;
              c2.insets = new java.awt.Insets(10,20,0,0);
              XXqqds_DataField5= new JTextField();
              XXqqds_DataField5.setBackground(new Color(255,255,255));
              XXqqds_DataField5.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_DataField5.setBounds(new Rectangle(77,0,122,19));
              XXqqds_DataField5.setHorizontalAlignment(SwingConstants.LEADING);
              GridBagConstraints c3 = new GridBagConstraints();
              c3.gridx=1;
              c3.gridy=0;
              c3.gridwidth=1;c3.gridheight=1;
              c3.anchor = java.awt.GridBagConstraints.NORTH;
              c3.weightx=0;c3.weighty = 0.0D;
              c3.ipadx = 0;c3.ipady=0;
              c3.insets = new java.awt.Insets(10,0,0,5);
              XXqqds_DataField5.setPreferredSize(new java.awt.Dimension(117,20));
              XXqqds_GridField2.setSize(275, 192);
              XXqqds_GridField2.setLocation(96, 15);
              c3.fill=GridBagConstraints.HORIZONTAL;
              XXqqds_TextGraphic6= new JLabel();
              XXqqds_TextGraphic6.setText("Password");
              XXqqds_TextGraphic6.setBackground(XXqqds_GridField3.getBackground());
              XXqqds_TextGraphic6.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_TextGraphic6.setBounds(new Rectangle(0,24,77,15));
              GridBagConstraints c4 = new GridBagConstraints();
              c4.gridx=0;
              c4.gridy = 0;
              c4.gridwidth=1;c4.gridheight=1;
              c4.anchor = java.awt.GridBagConstraints.NORTHWEST;
              c4.weightx=0;c4.weighty=0;
              c4.ipadx=0;c4.ipady=0;
              c4.fill=GridBagConstraints.NONE;
              c4.insets = new java.awt.Insets(40,20,0,0);
              XXqqds_DataField7= new JTextField();
              XXqqds_DataField7.setBackground(new Color(255,255,255));
              XXqqds_DataField7.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_DataField7.setBounds(new Rectangle(77,22,123,19));
              XXqqds_DataField7.setHorizontalAlignment(SwingConstants.LEADING);
              GridBagConstraints c5 = new GridBagConstraints();
              c5.gridx=1;
              c5.gridy=0;
              c5.gridwidth=1;c5.gridheight=1;
              c5.anchor = java.awt.GridBagConstraints.NORTH;
              c5.weightx=0;c5.weighty=0;
              c5.ipadx=0;c5.ipady=0;
              //c.fill=GridBagConstraints.VERTICAL;
         //     c.fill=GridBagConstraints.HORIZONTAL;
              XXqqds_GridField8= new JPanel();
              XXqqds_GridField8.setBackground(XXqqds_GridField2.getBackground());
              XXqqds_GridField8.setForeground(XXqqds_GridField2.getForeground());
              XXqqds_GridField8.setLayout(new GridBagLayout());
              XXqqds_GridField8.setBounds(new Rectangle(28,39,118,22));
              GridBagConstraints c6 = new GridBagConstraints();
              c6.gridx=0;
              c6.gridy = 0;
              c6.anchor = java.awt.GridBagConstraints.SOUTH;
              c6.weightx=0;c6.weighty = 1.0D;
         //     c.fill=GridBagConstraints.BOTH;
    XXqqds_GridField8.setPreferredSize(new java.awt.Dimension(205,42));
              c6.ipadx=0;c6.ipady=0;
    XXqqds_DataField7.setPreferredSize(new java.awt.Dimension(117,20));
    XXqqds_PushButton9= new JButton();
              XXqqds_PushButton9.setBackground(XXqqds_GridField8.getBackground());
              XXqqds_PushButton9.setForeground(XXqqds_GridField8.getForeground());
              XXqqds_PushButton9.setBounds(new Rectangle(68,0,67,22));
    GridBagConstraints c7 = new GridBagConstraints();
    c1.gridheight = 1;
    c5.insets = new java.awt.Insets(40,0,0,0);
              c7.gridx=0;
              c7.gridy=0;
              c7.gridwidth=1;c7.gridheight=1;
              c7.anchor = java.awt.GridBagConstraints.WEST;
              c7.weightx = 1.0D;c7.weighty = 0.0D;
              c7.ipadx=0;c7.ipady=0;
              //c.fill=GridBagConstraints.BOTH;
    c7.insets = new java.awt.Insets(0,20,0,0);
    XXqqds_GridField8.add(XXqqds_PushButton9, c7);
              XXqqds_PushButton9.setText("Submit");
              XXqqds_PushButton9.setPreferredSize(new java.awt.Dimension(74,20));
              XXqqds_GridField3.add(XXqqds_TextGraphic4, c2);
              XXqqds_GridField3.add(XXqqds_DataField5, c3);
              XXqqds_GridField3.add(XXqqds_TextGraphic6, c4);
              XXqqds_GridField3.add(XXqqds_DataField7, c5);
              XXqqds_GridField2.add(XXqqds_GridField3, c1);
              XXqqds_GridField2.add(XXqqds_GridField8, c6);
         /*     XXqqds_PushButton9= new JButton();
              XXqqds_PushButton9.setBackground(XXqqds_GridField8.getBackground());
              XXqqds_PushButton9.setForeground(XXqqds_GridField8.getForeground());
              XXqqds_PushButton9.setBounds(new Rectangle(68,0,67,22));
    GridBagConstraints c7 = new GridBagConstraints();
              c7.gridx = 0;
              c7.gridy = 0;
              c7.gridwidth=1;c7.gridheight=1;
              c7.anchor = java.awt.GridBagConstraints.WEST;
              c7.weightx = 1.0D;c7.weighty = 0.0D;
              c7.ipadx=0;c7.ipady=0;
         //     c7.fill=GridBagConstraints.BOTH;
    c7.insets = new java.awt.Insets(0,20,0,0);
    XXqqds_PushButton9.setPreferredSize(new java.awt.Dimension(74,22));
    XXqqds_PushButton9.setText("Submit");
              XXqqds_GridField8.add(XXqqds_PushButton9, c7);
    XXqqds_PushButton10= new JButton();
              XXqqds_PushButton10.setBackground(XXqqds_GridField8.getBackground());
              XXqqds_PushButton10.setForeground(XXqqds_GridField8.getForeground());
              XXqqds_PushButton10.setBounds(new Rectangle(0,0,68,22));
    GridBagConstraints c8 = new GridBagConstraints();
              c8.gridx = 1;
              c8.gridy = 0;
              c8.gridwidth=1;c8.gridheight=1;
              c8.anchor = java.awt.GridBagConstraints.EAST;
              c8.weightx=0;c8.weighty=0;
              c8.ipadx=0;c8.ipady=0;
              //c.fill=GridBagConstraints.BOTH;
    c8.insets = new java.awt.Insets(0,0,0,15);
    XXqqds_DataField7.setPreferredSize(new java.awt.Dimension(117,20));
              XXqqds_PushButton10.setText("cancel");
              XXqqds_PushButton10.setPreferredSize(new java.awt.Dimension(74,22));
              XXqqds_GridField8.add(XXqqds_PushButton10, c8);
              return jContentPane;
         public static void main(String a[])
         new DataBase();

    Hi EvilEdna,
    First of all thanks for the suggession. Now i am sending properly indented code.
    My problem is PushButton9 And PushButton10 are not displayed properly.Please check the constraints of
    buttons 9 & 10.And i am using eclipse Visual editor IDE(2.1).
    * Created on May 25, 2004
    * To change the template for this generated file go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    package demo;
    import java.awt.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JMenuBar;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    * @author kv5459
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    public class DataBase extends JFrame {
         private javax.swing.JPanel jContentPane = null;
    JPanel XXqqds_GridField2;
         JPanel XXqqds_GridField3;
    JLabel XXqqds_TextGraphic4;
         JTextField XXqqds_DataField5;
         JLabel XXqqds_TextGraphic6;
         JTextField XXqqds_DataField7;
    JPanel XXqqds_GridField8;
    JButton XXqqds_PushButton9;
    JButton XXqqds_PushButton10;
         * This is the default constructor
         public DataBase() {
              super();
              initialize();
         * This method initializes this
         * @return void
         private void initialize() {
              this.setSize(500, 375);
              this.setContentPane(getJContentPane());
              setTitle("");
              setLocale(java.util.Locale.getDefault());
              setResizable(true);
              setState(Frame.NORMAL);
              setSize(500,375);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
         * This method initializes jContentPane
         * @return javax.swing.JPanel
         private javax.swing.JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new javax.swing.JPanel();
                   jContentPane.setLayout(null);
              /* JPanel */     
              XXqqds_GridField2= new JPanel();
              XXqqds_GridField2.setBackground(getContentPane().getBackground());
              XXqqds_GridField2.setForeground(getContentPane().getForeground());
              XXqqds_GridField2.setLayout(new GridBagLayout());
              jContentPane.add(XXqqds_GridField2);
    /* JPanel */     
    XXqqds_GridField3= new JPanel();
              XXqqds_GridField3.setBackground(XXqqds_GridField2.getBackground());
              XXqqds_GridField3.setForeground(XXqqds_GridField2.getForeground());
              XXqqds_GridField3.setLayout(new GridBagLayout());
              XXqqds_GridField3.setBounds(new Rectangle(0,0,175,39));
              GridBagConstraints c1 = new GridBagConstraints();
              c1.gridx = 0;
              c1.gridy = 0;
    c1.gridwidth = 2;
    c1.gridheight = 1;
              c1.anchor = java.awt.GridBagConstraints.CENTER;
              c1.weightx=0;c1.weighty=0;
              c1.fill = java.awt.GridBagConstraints.NONE;
              XXqqds_GridField3.setPreferredSize(new java.awt.Dimension(205,192));
              c1.ipadx = 0;c1.ipady = 0;
    XXqqds_GridField2.add(XXqqds_GridField3, c1);
    /* JLabel */
              XXqqds_TextGraphic4= new JLabel();
              XXqqds_TextGraphic4.setText("Name");
              XXqqds_TextGraphic4.setBackground(XXqqds_GridField3.getBackground());
              XXqqds_TextGraphic4.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_TextGraphic4.setBounds(new Rectangle(13,2,49,15));
              GridBagConstraints c2 = new GridBagConstraints();
              c2.gridx=0;
              c2.gridy=0;
              c2.gridwidth=1;c2.gridheight=1;
              c2.anchor = java.awt.GridBagConstraints.NORTHWEST;
              c2.weightx = 0.0D;c2.weighty = 1.0D;
              c2.ipadx=0;c2.ipady=0;
              c2.fill = java.awt.GridBagConstraints.NONE;
              c2.insets = new java.awt.Insets(10,20,0,0);
    XXqqds_GridField3.add(XXqqds_TextGraphic4, c2);
    /* JTextField */
              XXqqds_DataField5= new JTextField();
              XXqqds_DataField5.setBackground(new Color(255,255,255));
              XXqqds_DataField5.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_DataField5.setBounds(new Rectangle(77,0,122,19));
              XXqqds_DataField5.setHorizontalAlignment(SwingConstants.LEADING);
              GridBagConstraints c3 = new GridBagConstraints();
              c3.gridx=1;
              c3.gridy=0;
              c3.gridwidth=1;c3.gridheight=1;
              c3.anchor = java.awt.GridBagConstraints.NORTH;
              c3.weightx=0;c3.weighty = 0.0D;
              c3.ipadx = 0;c3.ipady=0;
              c3.insets = new java.awt.Insets(10,0,0,5);
              XXqqds_DataField5.setPreferredSize(new java.awt.Dimension(117,20));
              c3.fill=GridBagConstraints.HORIZONTAL;
    XXqqds_GridField3.add(XXqqds_DataField5, c3);
    /* JLabel */
              XXqqds_TextGraphic6= new JLabel();
              XXqqds_TextGraphic6.setText("Password");
              XXqqds_TextGraphic6.setBackground(XXqqds_GridField3.getBackground());
              XXqqds_TextGraphic6.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_TextGraphic6.setBounds(new Rectangle(0,24,77,15));
              GridBagConstraints c4 = new GridBagConstraints();
              c4.gridx=0;
              c4.gridy = 0;
              c4.gridwidth=1;c4.gridheight=1;
              c4.anchor = java.awt.GridBagConstraints.NORTHWEST;
              c4.weightx=0;c4.weighty=0;
              c4.ipadx=0;c4.ipady=0;
              c4.fill=GridBagConstraints.NONE;
              c4.insets = new java.awt.Insets(40,20,0,0);
    XXqqds_GridField3.add(XXqqds_TextGraphic6, c4);
              /* JTextField */
    XXqqds_DataField7= new JTextField();
              XXqqds_DataField7.setBackground(new Color(255,255,255));
              XXqqds_DataField7.setForeground(XXqqds_GridField3.getForeground());
              XXqqds_DataField7.setBounds(new Rectangle(77,22,123,19));
              XXqqds_DataField7.setHorizontalAlignment(SwingConstants.LEADING);
              GridBagConstraints c5 = new GridBagConstraints();
              c5.gridx=1;
              c5.gridy=0;
              c5.gridwidth=1;c5.gridheight=1;
              c5.anchor = java.awt.GridBagConstraints.NORTH;
              c5.weightx=0;c5.weighty=0;
              c5.ipadx=0;c5.ipady=0;
    c5.insets = new java.awt.Insets(40,0,0,0);
    XXqqds_DataField7.setPreferredSize(new java.awt.Dimension(117,20));
         XXqqds_GridField3.add(XXqqds_DataField7, c5);
    /* JPanel */
    XXqqds_GridField8= new JPanel();
              XXqqds_GridField8.setBackground(XXqqds_GridField2.getBackground());
              XXqqds_GridField8.setForeground(XXqqds_GridField2.getForeground());
              XXqqds_GridField8.setLayout(new GridBagLayout());
              XXqqds_GridField8.setBounds(new Rectangle(28,39,118,22));
              GridBagConstraints c6 = new GridBagConstraints();
              c6.gridx=0;
              c6.gridy = 0;
              c6.anchor = java.awt.GridBagConstraints.SOUTH;
              c6.weightx=0;c6.weighty = 1.0D;
         XXqqds_GridField8.setPreferredSize(new java.awt.Dimension(205,42));
              c6.ipadx=0;c6.ipady=0;
    XXqqds_GridField2.add(XXqqds_GridField8, c6);
    /* Submit Button */
    XXqqds_PushButton9= new JButton();
              XXqqds_PushButton9.setBackground(XXqqds_GridField8.getBackground());
              XXqqds_PushButton9.setForeground(XXqqds_GridField8.getForeground());
              XXqqds_PushButton9.setBounds(new Rectangle(68,0,67,22));
    GridBagConstraints c7 = new GridBagConstraints();
    c7.gridx=0;
              c7.gridy=0;
              c7.gridwidth=1;c7.gridheight=1;
              c7.anchor = java.awt.GridBagConstraints.WEST;
              c7.weightx = 1.0D;c7.weighty = 0.0D;
              c7.ipadx=0;c7.ipady=0;
              c7.insets = new java.awt.Insets(0,20,0,0);
              XXqqds_PushButton9.setText("Submit");
              XXqqds_PushButton9.setPreferredSize(new java.awt.Dimension(74,20));
              XXqqds_GridField8.add(XXqqds_PushButton9, c7);
         /* Cancel Button */
    XXqqds_PushButton10= new JButton();
              XXqqds_PushButton10.setBackground(XXqqds_GridField8.getBackground());
              XXqqds_PushButton10.setForeground(XXqqds_GridField8.getForeground());
              XXqqds_PushButton10.setBounds(new Rectangle(0,0,68,22));
    GridBagConstraints c8 = new GridBagConstraints();
    c8.gridx = 1;
              c8.gridy = 0;
              c8.gridwidth=1;c8.gridheight=1;
              c8.anchor = java.awt.GridBagConstraints.EAST;
              c8.weightx=0;c8.weighty=0;
              c8.ipadx=0;c8.ipady=0;
         c8.insets = new java.awt.Insets(0,0,0,15);
    XXqqds_PushButton10.setText("cancel");
              XXqqds_PushButton10.setPreferredSize(new java.awt.Dimension(74,22));
    XXqqds_GridField8.add(XXqqds_PushButton10, c8);
              return jContentPane;
         public static void main(String a[])
         new DataBase();

  • Grid abck layout problem using swing components

    JScrollPane jsp1 = new JScrollPane(table1,k,i);
    JPanel ja = new JPanel();
    JCheckBox c1 = new JCheckBox();
    JCheckBox c2 = new JCheckBox();
    JCheckBox c3 = new JCheckBox();
    JLabel j = new JLabel("Button");
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    ja.setLayout(gbl);
    gbc.gridwidth = 10;
    gbc.gridheight = 10;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(j,gbc);
    add(j);
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(c2,gbc);
    add(c2);
    gbc.gridx = 2;
    gbc.gridy = 2;
    gbl.setConstraints(c3,gbc);
    add(c3);
    gbc.gridx = 3;
    gbc.gridy = 3;
    gbl.setConstraints(jsp1,gbc);
    add(jsp1);
    this my code for grid bag layout but when i run this code it is showing only empty panel..no components..
    of course iam not posting entire code of tis class..because it is lenghty..
    regards,
    sam

    Well, you set JPanel ja's layout to GridBagLayout, but it appears your adding the components to the parent, eg. add(j) should be ja.add(j) if you want it on JPanel ja. This is true for all your component adds.
    By the you can combine the setConstraints( ) and add calls: ja.add(j, bgc);
    Hope that helps.
    Cheers
    DB

  • Creating a grid-like layout

    Previously, I posted a problem with trying to wrap tables within tables, referencing this code.
    <!-- ... -->
    <tableLayout>
      <contents>
        <rowLayout>
          <contents>
            <bc4j:viewObjectScope name="CustomArrayDataGraphsView">
              <bc4j:rowStamp>
                <cellFormat>
                  <contents>
                    <tableLayout>
                      <contents>
                        <rowLayout>
                          <contents>
                            <link
                                  styleClass="OraHeaderSub"
                                  targetFrame="display">
                              <boundAttribute
                                              name="text">
                                <bc4j:attrValue name="AnalysisParam"/>
                              </boundAttribute>
                              <boundAttribute
                                              name="destination">
                                <bc4j:ordProperty name="url"/>
                              </boundAttribute>
                              <boundAttribute
                                              name="shortDesc">
                                <bc4j:attrValue name="Description"/>
                              </boundAttribute>
                            </link>
                          </contents>
                        </rowLayout>
                        <rowLayout>
                          <contents>
                            <bc4j:attrScope name="Graph">
                              <contents>
                                <html:object
                                             width="300"
                                             height="200">
                                  <boundAttribute
                                                  name="data">
                                    <bc4j:ordProperty name="url"/>
                                  </boundAttribute>
                                  <boundAttribute
                                                  name="type">
                                    <bc4j:ordProperty name="contentType"/>
                                  </boundAttribute>
                                  <rawText>
                                    <boundAttribute
                                                    name="text">
                                      <bc4j:attrValue name="Description"/>
                                    </boundAttribute>
                                  </rawText>
                                </html:object>
                              </contents>
                            </bc4j:attrScope>                                                                                                                                  
                          </contents>
                        </rowLayout>
                      </contents>
                    </tableLayout>
                  </contents>
                </cellFormat>
              </bc4j:rowStamp>
            </bc4j:viewObjectScope>
          </contents>
        </rowLayout>
      </contents>
    </tableLayout>
    <!-- ... -->The above code renders a single row of elements which are tables themselves. Using the <cellFormat> inside the <viewObjectScope> avoids the "new line" rendering of the internal tables of title/images. However, the internal tables do not wrap.
    This is an unfortunate side-effect of html tables. So, I need to basically make a sizeable grid (similarly to a GridLayout for all you AWT/Swing folks out there) layout that can render a specified number of columns per table, creating new rows as necessary. Something like...
    <gridLayout columns="5">
      <contents>
        <bc4j:viewObjectScope                                 name="CustomArrayDataGraphsView">
          <bc4j:rowStamp>
            <cellFormat>
              <contents>
                <!-- render something per row -->
              </contents>
            </cellFormat>
          </bc4j:rowStamp>
        </bc4j:viewObjectScope>
      </contents>
    </gridLayout>This custom layout will render a maximum of five columns of data per table row, where each column is actually a row of information from the view object.
    Is this possible using UIX components or should I start coding?
    Thanks,
    joe

    Also, displaying a bunch of "inline"ed tables would be really useful. I can't seem to do this from within a <header>, <hideShowHeader> tag, even with explicitly coding:
      <bc4j:viewObjectScope name="CustomArrayDataGraphsView">
        <bc4j:rowStamp>
          <bc4j:attrScope name="Graph">
            <contents>
                  <html:table style="display: inline">
                    <html:tr>
                      <html:td>
                        <styledText text="Title"/>
                      </html:td>
                    </html:tr>
                    <html:tr>
                      <html:td>
                        <styledText text="Image"/>
                      </html:td>
                    </html:tr>
                  </html:table>
    <!--
                  <tableLayout styleClass="display: inline" width="300">
                    <contents>
                      <rowLayout>
                        <contents>
                          <styledText text="Title"/>
                        </contents>
                      </rowLayout>
                      <rowLayout>
                        <contents>
                          <styledText text="Image"/>
                        </contents>
                      </rowLayout>
                    </contents>
                  </tableLayout>
            </contents>
          </bc4j:attrScope>
        </bc4j:rowStamp>
      <!-- render a "No Records Found" message -->
        <contents>
          <styledText
                      text="No Records Found"
                      styleClass="OraErrorText">
            <boundAttribute
                            name="rendered">
              <if>
                <comparison
                            type="lessThan">
                  <bc4j:viewObjectProperty name="estimatedRowCount"/>
                  <fixed
                         javaType="int">1</fixed>
                </comparison>
                <fixed
                       javaType="boolean">true</fixed>
                <fixed
                       javaType="boolean">false</fixed>
              </if>
            </boundAttribute>
          </styledText>
        </contents>
      </bc4j:viewObjectScope>Apparently, the header tags do something which effects the display of "inline"ed elements, like the tables in the previous example. Any suggestions? I know taking out of the header works great, but, the <hideShowHeader> functionality is particularly necessary.
    I have a time constraint on this... gotta be done by today, so if you have any other later minute suggestions besides creating a grid-like layout, let me know!
    Thanks,
    Joe

  • ALV Grid save layout

    Hi all,
    in a programm there is a ALV grid of type cl_gui_alv_grid.
    When I run the programm, I can change the layout (hide columns etc) but I can't
    save the changes I make. What do I have to do to activate the saving of the layout?
    Which field do I have to set to activate it or what can already be set which disables the showing of the save possibility?
    Greetings Fred.

    Hi Fred
    U need to transfer the name of the report and the user to the grid class in order to allow to save the layout:
    DATA: LT_VARIANT  TYPE DISVARIANT.
    LT_VARIANT-REPORT   = SY-REPID.
    LT_VARIANT-USERNAME = SY-UNAME.
    CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY
           EXPORTING
                     IS_VARIANT                      = LT_VARIANT
                     I_SAVE                             = 'A'
                     IS_LAYOUT                       = LT_LAYOUT
                     IT_TOOLBAR_EXCLUDING = GT_NO_CODES
           CHANGING 
                     IT_OUTTAB                  = T_OUTPUT
                     IT_FIELDCATALOG      = GT_FIELDCAT.
    The user has to have the authorizations to manage the layout.
    Max

  • Grid Report layout

    Hi
    I'm trying to create a tabular report with gridlines.  The report goes over multiple pages and has numerous columns all of different widths and heights depending upon the row content.
    I have tried to follow the tutorials however each time my rectangles overlap each other and I don't get clean grid lines. alterntively the rectangles are apart from one another.
    How is a grid easily achieved using this software?  Am I doing something wrong? Surely it's not meant to be this difficult?

    Hi Mark,
    thanks for reply, I was making example on
    http://apex.oracle.com/pls/otn/f?p=43048:22:2659745156195172
    using debug grid layout and put into REGION_05
    ...< div id = "munkyDiv" > #REGION_POSITION_O5# < / div>
    in this way I can add 1,2,3 or more reports as tab and grid.
    Simple example as first step is basic layout (integration ExtJS and Apex)>
    http://apex.oracle.com/pls/otn/f?p=43048:15
    username/pass EXTJS/EXTJS
    regards
    Gordan
    Edited by: useruseruser on Aug 8, 2010 2:51 PM
    Edited by: useruseruser on Aug 8, 2010 2:52 PM

  • Disable resizing of columns in ALV grid after layout is fixed

    Hello Experts!
    I needed to decrease size of some of my columns so I used set_fixed_table_layout( abap_true ).
    This worked fine, but when I scroll on the column header , I get a possibility to resize columns by changing the width of culumns at run-time.
    Is it possible to disable this functionality?

    Fixed table layout is a nice way of handling sizing if you wish to eliminate horizontal scrolling. If you want to have specific widths, and disallow re-sizing, why use fixed table layout? Maybe set the columns widths manually in the code.

  • Multiple components in a gridbag layout cell

    Is it possible to have two components in one cell of a grid bag layout? Say, a textfield box and some text to its immediate right? If so, can someone post an example? Thanksmuch.

    Put the textfield and a label with the text in a panel. Then add that panel to your gridbag cell.

  • Java- Problem in tabbing order using Border layout

    Though i have added components in particular order to borderlayout Panel,Order of tabbing is not in the order of components added?can any one say why is happens because the tabbing occurs naturally in the order the components are added to the panel . how to solve this problem ? Please.
    Arun

    I think that the border layout defines it's own tab order.
    I think the grid bag layout does, also.
    You could, however, try to duplicate the border layout using :
    a) A box layout
    b) A spring layout
    Or, you could take a look at Focus Managers. That will allow you to set an arbitrary tab order independent of the component order or the default tab orders of the layouts.

  • Layout and resize

    Although I tried many things, I can't get out of this :
    I have a java app. with a jSplitPane. On the right side, I have a JPanel hosting another jPanel with an image background.
    What I'd like is that when moving the divider, my inner Panel (the one with the image) doesn't resize. It should be just displayed at the center of the other one.
    Now, I used BorderLayout for the 'container' and added the second one like this :
                panel1.add(panel2, java.awt.BorderLayout.CENTER);But when I move the divider, the iner panel extends to the borders (the image is stretched).
    I tried FlowLayout, but then nothing at all is displayed (??)
    Any idea ?
    O, BTW, the inner panel uses a grid bag Layout. Could it have an influence on its container ?

    Too many panels you are having. Try none:
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
    public class SplitExample {
         public static void main(String[] args) throws MalformedURLException {
              JLabel left = new JLabel("LEFT");
              URL url = new URL("http://weblogs.java.net/jag/bio/JagHeadshot-small.jpg");
              JLabel right = new JLabel(new ImageIcon(url));
              JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
              JFrame f = new JFrame("SplitExample");
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              f.getContentPane().add(sp);
              f.pack();
              f.setLocationRelativeTo(null);
              f.setVisible(true);
    }

  • GridBag Layout - Auto Adjustment

    I am using grid bag layout , I have the weightx to 1 and fill to horizontal. All I want is that my component occupy space horizontally.
    I have set Preffered , Min and Max sizes.
    There are empty space with a width of a 1 or 2 pixels occasionaly show up on left and right side of the grid.
    The GridBad also adjusts the size of all the cells when I am just typing in cell.
    regards,
    Irfan

    If you're using a GridBagLayout for a one-cell layout, you're working too hard: use BorderLayout instead.
    If you have multiple cells, then make sure that only the cell you want to expand has weightx != 0. weightx is used to apportion excess space in the layout: if multiple columns are used and have non-zero weightx, then excess width will be assigned to each column by the ratio (column's weightx / total weightx for all columns).
    You should avoid setting the different sizes of components in general and text components specifically. Use setColumns() instead, to give the component a hint. The text components use the columns value and their font to determine the width of the component (with some fudge factor involed as well).
    If the layout manager is adjusting the size of the component, it is because the component is reporting a size change to its container. Without the columns value set for a text component, I believe that it is using its current contents to guess at the number of characters it is to allow room for, and as you type more characters, this computation changes (and thus changes the size). It might be being reported to the container as a size change or as a property change, but there is definitely something being reported to the parent on every keystroke. Look at the source for the various JText components: I may be mis-remembering, but I believe that they do take quite a bit more responsibility for their own sizes, and don't necessarily simply accept the size you give them as final.

  • Layout of JRadioButtons

    I have been trying to get a JDialog to properly format 3 lines, using GridBagLayout. Finidng decent GridBag tutorials online is nigh impossible and the few books I have, do a poor job of explaining it.
    I am trying to get a Jlabel on the first line, no real problems there.
    The next line, I need a ButtonGroup of JRadioButtoms. The number of radio buttons that wil need to be created will vary.
            int arg;
            int i=0;
            gbc.weighty=1;
            gbc.anchor=GridBagConstraints.EAST;
            for(;i<buttons.length-1;i++)
                arg = i+1;
                Report.write(arg);
                buttons[i] = new JRadioButton(Integer.toString(arg));
                buttons.addActionListener(this);
    this.getContentPane().add(buttons[i]);
    group.add(buttons[i]);
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbc.gridheight = GridBagConstraints.RELATIVE;
    this.getContentPane().add(buttons[i],gbc);
    arg = i+1;
    Report.write(arg);
    buttons[i] = new JRadioButton(Integer.toString(arg));
    buttons[i].addActionListener(this);
    this.getContentPane().add(buttons[i]);
    group.add(buttons[i]);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbag.setConstraints(buttons[i], gbc);
    this.getContentPane().add(buttons[i],gbc);
    The first button shows up, roughly in the center of the JDialog, the second, is on the far right, and the rest are not shown, unless it gets resized.
    The last line is 2 buttons, no problem there.
    Could someone please point me in the right direction, either with a decent explaination of both GridBagLayout and ButtonGroup, or show me some simple code to get me started? Thanks.

    if i have the code i can help u on that.
    Any way here is an example for u.
    slUpper and the slLower are two sliders.
    One in left and one in right.
    private void addSeasonSliders(double leftGain, double rightGain)
    this.sliderPanel.removeAll();
    if(leftGain > 1 && rightGain > 1) return;
    this.sliderPanel.add( slLower, new GridBagConstraints(0, 0, 1, 1, leftGain, 0
    ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    this.sliderPanel.add(slUper, new GridBagConstraints(1, 0, 1, 1, rightGain, 0.0
    ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    in this code leftGain and the rightGain are the ratios that should taken from the empty space to fill the sliders.
    ex. rightGain 0.5 makes the slUper slider filled with half of the empty space available in right side and left side.
    first four parameters for the grid bag layout defigns the grid positions(0,1 and 1,0 here) and the ammount of column and row spans.(1,1 here).
    then we have the propotion of the empty space taken to fill the controlers visible area. I have set it dinamically. using two variables leftGain and rightGain.
    next one is the same . but for horizontall empty space propotion to fill.
    then the anchor position. in this case we have WEST and EAST. thin we have to set the filling direction.
    it is horizontal in this case.
    then we have insets. insets are use to position each controler in the grid of the gridbag. 0,0,0,0 makes no spaces from the grid borders.
    then we have the ammound of paddings to in the xaxis and y axis of the gridbag.
    Beter u cut and paste this code that i have used in one of my programing modules. u may have to define some variables and controlers. try changing different values and undertand the grid bag layout .Then u can do what ever u imagin. grid bag layout is realy powerful.

  • Need help displaying layouts

    I'm new to Java swing but have read the tutorials.. maybe I'm just not very fast learner. However I need to display some things, but having problems doing so in JavaSwing.
    Basically my display needs to be like this
    Name: <textfield>
    Address: <textfield>
    Are you married? <yes option> <no Option>
    My problem is the yes and no options. If I make the Grid 3 columns then the textfields are displayed in the 2nd column rather than spreading across the 2nd and 3rd columns. The other thing is I want all the Labels (name, address, are you married) to be on one left column while the other items are in another column. I guess this is kinda like a nest panel or something? I'm not really sure. Are there examples of how to do something like this? I learn best by examples.

    Swinging :) is kinda like a top-down * bottom-up approach (mix of both). So you gotta think from the top and implement from the bottom. (We can call this design-down, implement-up neh? :)
    1. Essentially, you need three rows, with two columns.
    2. First column contains three rows of the same size.
    3. Second column contains three rows too.
    a. First and second rows of second column span the entire row.
    c. Last row is split into two.
    Make a label panel for holding the labels such as "Name" and "Address". Make this a grid panel of 3 rows and 1 column. Call this LPANEL
    Now make another panel, this also with three rows and one column using a grid layout. Call this the FPANEL.
    Now, add a textfield to the first two rows.
    For the last, create a new panel with a grid layout with size 1x2 (1 row, 2 columns). Call this the OPANEL. Add it to the FPANEL last. So, now you have two panels. How do you ensure they line up?
    Set the same margin sizes (and insets) for all components/grid layouts.
    Finally, make a MPANEL (main panel) which uses grid bag layout. You want your FPANEL to expand to fill the MPANEL but the LPANEL should be constant no? So we use gridbaglayout and add LPANEL with normal constraints (read up on the constrainst and how to use gridbaglayout :). Add FPANEL with gridbagconstraints that cause it to fill the remainder of the space ("remainder" is the key here).
    Voila!
    Your are done :)
    Now, I would make your life easier by recommending you use netBeans for desiging UI's or Eclipse. But, a year of swinging by hand will give u enuf expertise that when u do start using netBeans etc, you will have be able to get the maximum out of them.
    tell me if u got it right!
    -vijai.

  • Inner Frames, GridBag Layout, and resizing

    Hey,
    I'm developing a online gaming lobby in Java, and at the top of my lobby I have a list of players, with red or green lights indicating whether or not that player is ready to play. Once all are ready, the host can start a countdown that takes the players into the game.
    For the ready/not ready lights, I used JRadioButtons because they provide a good model for what I wanted (a sort of on/off deal). What I did was set them to be enabled when that slot is empty, but have a grey "disabled/slot empty" icon for both the selected and not selected states. Then, when a player joins, I disable the Radio Button and have the "player ready" and "player not ready" pictures set for the disabled selected and disabled-not-selected properties of the radio button.
    To recap, when the slot is empty, the radio button is enabled, but looks disabled and has the same pictures for both selected and not selected. When the slot is filled the radio button is disabled but has enabled-looking pictures for 'ready' and 'not ready' states, selected and not selected respectively.
    It will make more sense with a picture:
    http://img151.imageshack.us/img151/4620/lobbyiz7.png
    I did it this way, with sort of logically backwards JRadioButtons, because I didn't want the users of the lobby to be able to click the radio buttons and change their state, thus seemingly (and logically, client-side) changing the ready status of other players.
    Anyway, I have two questions. As you can see in the screen shot, all the player names and their associated "lights" are clustered to the center. Originally, the lights and text were all flush with the edge of the separators above and below them. I am using a grid bag layout with inner panels, which all also use grid bag layouts. In the player list panel (the top one that we've been discussing), when players join, the text next to the JRadioButtons goes from "Waiting for player to join" to "Freddie" or something like that. Once you get enough players to join, the player list contracts into the center because the text in the buttons becomes smaller. Is there a way to make the radio buttons stay where they are, no matter the length of the text, with a grid bag layout?
    Also, you can see from the screenshot that the disabled radio buttons (the slots that are filled with players) have the proper pictures next to them, but they still used the disabled font, which is kind of greyed out. Is there a way to make it so that the disabled font is not grey?

    j.law wrote:
    That's the thing, I am using a grid bag layout for everything, including the inner panels and the outer, main frame. The problem is that the inner panels resize and crunch down.Then as recommended above, simply don't do that. Study the layout tutorials and code with them. It's that simple.
    Also, your "radiobutton" are not behaving as radiobuttons as they are not clickable, ... so don't use radiobuttons. Use a JLabel perhaps and swap the icons as necessary.

  • New to java and having issues trying to modify sample code.

    i was trying to edit the following code to add about 10+ more labels and textfields and save the information to the contacts.dat in the code. it currently displays all the fields i entered, but it only saves the first 7 fields information?? not sure why. also i was trying to just line the fields up using a flowlayout but it just errors. anyone have any suggestions?
    <source code below this line>
    ====================START OF CODE ======================
    // cm.java
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    // =========================================================
    // Class: cm
    // This class drives the contact manager. It contains the
    // main method which gets called as soon as this application
    // begins to run.
    // =========================================================
    class cm extends Frame implements ActionListener
    // Container of contact objects (one object per business
    // contact).
    private Vector contacts = new Vector (100);
    // List of names component. (Must specify java.awt in
    // front of List to distinguish the List class in the
    // java.awt package from the List class in the java.util
    // package.)
    private java.awt.List names = new java.awt.List ();
    // delete and edit button components.
    private Button delete;
    private Button edit;
    // Default constructor.
    public cm ()
    // Assign Contact Manager to title bar of frame window.
    super ("Customer Manager Version 0.001 BY Pebkac");
    // Add a listener that responds to window closing
    // events. When this event occurs (by clicking on the
    // close box in the title bar), save contacts and exit.
    addWindowListener (new WindowAdapter ()
    public void windowClosing
    (WindowEvent e)
    saveContacts ();
    System.exit (0);
    // Place an empty label in the north part of the frame
    // window. This is done to correct an AWT positioning
    // problem. (One thing that you'll come to realize as
    // you work with the AWT is that there are lots of bugs.)
    Label l = new Label ();
    add ("North", l);
    // Place the names component in the center part of the
    // frame window.
    add ("Center", names);
    // Create a panel object to hold four buttons.
    Panel p = new Panel ();
    Button b;
    // Add an add button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (b = new Button ("add"));
    b.addActionListener (this);
    // Add a delete button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (delete = new Button ("delete"));
    delete.addActionListener (this);
    // The delete button should be disabled until there is at
    // least one contact to delete.
    delete.setEnabled (false);
    // Add an edit button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (edit = new Button ("edit"));
    edit.addActionListener (this);
    // The edit button should be disabled until there is at
    // least one contact to edit.
    edit.setEnabled (false);
    // Add a quit button to the Panel object and register
    // the current cm object as a listener for button events.
    p.add (b = new Button ("quit"));
    b.addActionListener (this);
    // Add the panel object to the frame window container.
    add ("South", p);
    // Set the background of the frame window container to
    // pink (to give a pleasing effect).
    setBackground (Color.pink);
    // Set the size of the frame window container to 400
    // pixels horizontally by 200 pixels vertically.
    setSize (400, 200);
    // Do not allow the user to resize the frame window.
    setResizable (false);
    // Load all contacts.
    loadContacts ();
    // Make sure that the frame window is visible.
    setVisible (true);
    public void actionPerformed (ActionEvent e)
    if (e.getActionCommand ().equals ("delete"))
    delete ();
    else
    if (e.getActionCommand ().equals ("quit"))
    saveContacts ();
    System.exit (0);
    else
    if (e.getActionCommand ().equals ("add"))
    add ();
    else
    edit ();
    public Insets getInsets ()
    // Return an Insets object that describes the number of
    // pixels to reserve as a border around the edges of the
    // frame window.
    return new Insets (10, 10, 10, 10);
    public static void main (String [] args)
    // Create a new cm object and let it do its thing.
    new cm ();
    private void delete ()
    // Obtain index of selected contact item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot edit
    // a contact if no contact item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Remove the contact item from the names component.
    names.remove (index);
    // Remove the Contact object from the contacts Vector
    // object.
    contacts.remove (index);
    // If there are no more contacts ...
    if (contacts.size () == 0)
    delete.setEnabled (false);
    edit.setEnabled (false);
    else
    // Make sure that the first contact item in the names
    // list is highlighted.
    names.select (0);
    private void add ()
    // Create an add data entry form to enter information
    // for a new contact.
    DataEntryForm def = new DataEntryForm (this, "add");
    // If the bOk Boolean flag is set, this indicates the user
    // exited the form by pressing the Ok button.
    if (def.bOk)
    // Create a Contact object and assign information from
    // the form to its fields.
    Contact temp = new Contact ();
    temp.fname = new String (def.fname.getText ());
    temp.lname = new String (def.lname.getText ());
    temp.haddress = new String (def.haddress.getText ());
    temp.maddress = new String (def.maddress.getText ());
    temp.phone = new String (def.phone.getText ());
    temp.wphone = new String (def.wphone.getText ());
    temp.cphone = new String (def.cphone.getText ());
    temp.email = new String (def.email.getText ());
    temp.bdate = new String (def.bdate.getText ());
    temp.comments = new String (def.comments.getText ());
    // Add a new contact item to the names component.
    names.add (temp.lname + ", " + temp.fname);
    // Add the Contact object to the contacts Vector
    // object.
    contacts.add (temp);
    // Make sure that the delete and edit buttons are
    // enabled.
    delete.setEnabled (true);
    edit.setEnabled (true);
    // Destroy the dialog box.
    def.dispose ();
    // Make sure that the first contact item in the names list
    // is highlighted.
    names.select (0);
    // ===========================================================
    // Load all contacts from contacts.dat into the contacts
    // Vector object. Also, make sure that the last name/first
    // name from each contact is combined into a String object and
    // added into the names component - as a contact item.
    // ===========================================================
    private void loadContacts ()
    FileInputStream fis = null;
    try
    fis = new FileInputStream ("contacts.dat");
    DataInputStream dis = new DataInputStream (fis);
    int nContacts = dis.readInt ();
    for (int i = 0; i < nContacts; i++)
    Contact temp = new Contact ();
    temp.fname = dis.readUTF ();
    temp.lname = dis.readUTF ();
    temp.haddress = dis.readUTF ();
    temp.maddress = dis.readUTF ();
    temp.phone = dis.readUTF ();
    temp.wphone = dis.readUTF ();
    temp.cphone = dis.readUTF ();
    temp.email = dis.readUTF ();
    temp.bdate = dis.readUTF ();
    temp.comments = dis.readUTF ();
    names.add (temp.lname + ", " + temp.fname);
    contacts.add (temp);
    if (nContacts > 0)
    delete.setEnabled (true);
    edit.setEnabled (true);
    catch (Exception e)
    finally
    if (fis != null)
    try
    fis.close ();
    catch (Exception e) {}
    // Make sure that the first contact item in the names list
    // is highlighted.
    names.select (0);
    // ========================================================
    // Save all Contact objects from the contacts Vector object
    // to contacts.dat. The number of contacts are saved as an
    // int to make it easy for loadContacts () to do its job.
    // ========================================================
    private void saveContacts ()
    FileOutputStream fos = null;
    try
    fos = new FileOutputStream ("contacts.dat");
    DataOutputStream dos = new DataOutputStream (fos);
    dos.writeInt (contacts.size ());
    for (int i = 0; i < contacts.size (); i++)
    Contact temp = (Contact) contacts.elementAt (i);
    dos.writeUTF (temp.fname);
    dos.writeUTF (temp.lname);
    dos.writeUTF (temp.haddress);
    dos.writeUTF (temp.maddress);
    dos.writeUTF (temp.phone);
    dos.writeUTF (temp.wphone);
    dos.writeUTF (temp.cphone);
    dos.writeUTF (temp.email);
    dos.writeUTF (temp.bdate);
    dos.writeUTF (temp.comments);
    catch (Exception e)
    MsgBox mb = new MsgBox (this, "CM Error",
    e.toString ());
    mb.dispose ();
    finally
    if (fos != null)
    try
    fos.close ();
    catch (Exception e) {}
    private void edit ()
    // Obtain index of selected contact item from the names
    // component.
    int index = names.getSelectedIndex ();
    // If no item was selected, index is -1. We cannot edit
    // a contact if no contact item in the names component was
    // selected - because we would have nothing to work with.
    if (index != -1)
    // Obtain a reference to the Contact object (from the
    // contacts Vector object) that is associated with the
    // index.
    Contact temp = (Contact) contacts.elementAt (index);
    // Create and display an edit entry form.
    DataEntryForm def = new DataEntryForm (this, "edit",
    temp.fname,
    temp.lname,
    temp.haddress,
    temp.maddress,
    temp.phone,
    temp.wphone,
    temp.cphone,
    temp.email,
    temp.bdate,
    temp.comments);
    // If the user pressed Ok...
    if (def.bOk)
    // edit the contact information in the contacts
    // Vector object.
    temp.fname = new String (def.fname.getText ());
    temp.lname = new String (def.lname.getText ());
    temp.haddress = new String (def.haddress.getText ());
    temp.maddress = new String (def.maddress.getText ());
    temp.phone = new String (def.phone.getText ());
    temp.wphone = new String (def.wphone.getText ());
    temp.cphone = new String (def.cphone.getText ());
    temp.email = new String (def.email.getText ());
    temp.bdate = new String (def.bdate.getText ());
    temp.comments = new String (def.comments.getText ());
    // Make sure the screen reflects the edit.
    names.replaceItem (temp.lname + ", " + temp.fname,
    index);
    // Destroy the dialog box.
    def.dispose ();
    // Make sure that the first contact item in the names
    // list is highlighted.
    names.select (0);
    // ========================================================
    // Class: Contact
    // This class describes the contents of a business contact.
    // ========================================================
    class Contact
    public String fname;
    public String lname;
    public String haddress;
    public String maddress;
    public String phone;
    public String wphone;
    public String cphone;
    public String email;
    public String bdate;
    public String comments;
    // ==========================================================
    // Class: DataEntryForm
    // This class provides a data entry form for entering contact
    // information.
    // ==========================================================
    class DataEntryForm extends Dialog implements ActionListener
    // bOk is a boolean flag. When true, it indicates that
    // the Ok button was pressed to terminate the dialog box
    // (as opposed to the Cancel button).
    public boolean bOk;
    // The following components hold the text that the user
    // entered into the visible text fields.
    public TextField fname;
    public TextField lname;
    public TextField haddress;
    public TextField maddress;
    public TextField phone;
    public TextField wphone;
    public TextField cphone;
    public TextField email;
    public TextField bdate;
    public TextField comments;
    public void actionPerformed (ActionEvent e)
    // If the user pressed the Ok button, indicate this
    // by assigning true to bOk.
    if (e.getActionCommand ().equals ("Ok"))
    bOk = true;
    // Destroy the dialog box and return to the point
    // just after the creation of the DataEntryForm object.
    dispose ();
    public DataEntryForm (Frame parent, String title)
    // Call the other constructor. The current constructor
    // is used for add operations. The other constructor
    // is used for edit operations.
    this (parent, title, "", "", "", "", "", "", "", "", "", "");
    public DataEntryForm (Frame parent, String title,
    String fname, String lname,
    String haddress, String maddress,
    String phone,String wphone,
    String cphone,String email,
    String bdate,String comments)
    // Initialize the superclass layer.
    super (parent, title, true);
    // Choose a grid bag layout so that components can be more
    // accurately positioned. (It looks nicer.)
    setLayout (new GridBagLayout ());
    // Add appropriate first name, last name, phone, wphone, and
    // email components to the current DataEntryForm container.
    // (Remember, DataEntryForm is a subclass of Dialog.
    // Dialog is a container. Therefore, DataEntryForm
    // inherits the ability to be a container.)
    addComponent (this, new Label ("First Name: "),0, 0, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.fname = new TextField (20);
    addComponent (this, this.fname, 1, 0, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.fname.setText (fname);
    addComponent (this, new Label ("Last Name: "), 0, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.lname = new TextField (20);
    addComponent (this, this.lname, 1, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.lname.setText (lname);
    addComponent (this, new Label ("Home Address: "), 0, 2, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.haddress = new TextField (20);
    addComponent (this, this.haddress, 1, 2, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.haddress.setText (haddress);
    addComponent (this, new Label ("Mailing Address: "), 0, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.maddress = new TextField (20);
    addComponent (this, this.maddress, 1, 3, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.maddress.setText (maddress);
    addComponent (this, new Label ("Home Number: "), 0, 4, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.phone = new TextField (20);
    addComponent (this, this.phone, 1, 4, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.phone.setText (phone);
    addComponent (this, new Label ("Work Number: "), 0, 5, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.wphone = new TextField (20);
    addComponent (this, this.wphone, 1, 5, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    if (title.equals ("edit"))
    this.wphone.setText (wphone);
    addComponent (this, new Label ("Cell Number: "), 0, 6, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.cphone = new TextField (20);
    addComponent (this, this.cphone, 1, 6, 1, 1,
    GridBagConstraints.WEST,
    GridBagConstraints.WEST);
    addComponent (this, new Label ("Email Address: "), 0, 7, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.email = new TextField (20);
    addComponent (this, this.email, 1, 7, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    addComponent (this, new Label ("Birth Date: "), 0, 8, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.bdate = new TextField (20);
    addComponent (this, this.bdate, 1, 8, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    addComponent (this, new Label ("Comments: "), 2, 0, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    this.comments = new TextField (20);
    addComponent (this, this.comments, 2, 1, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    addComponent (this, new Label (""), 0, 9, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    addComponent (this, new Label (""), 1, 9, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.WEST);
    Button b;
    // Add an Ok button to this container.
    addComponent (this, b = new Button ("Ok"), 0, -9, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    b.addActionListener (this);
    // Add a Cancel button to this container.
    addComponent (this, b = new Button ("Cancel"), 1, -9, 1, 1,
    GridBagConstraints.NONE,
    GridBagConstraints.CENTER);
    b.addActionListener (this);
    // Set the background of the frame window container to
    // pink (to give a pleasing effect).
    setBackground (Color.pink);
    // Set the size of the dialog window to 250 pixels
    // horizontally by 200 pixels vertically.
    setSize (450, 500);
    // Do not allow users to resize the dialog window.
    setResizable (false);
    // Make sure that the dialog window is visible.
    setVisible (true);
    private void addComponent (Container con, Component com,
    int gridx, int gridy,
    int gridw, int gridh, int fill,
    int anchor)
    // Get the current layout manager. It is assumed to
    // be a GridBagLayout object.
    LayoutManager lm = con.getLayout ();
    // Create a GridBagConstraints object to make it
    // possible to customize component positioning.
    GridBagConstraints gbc = new GridBagConstraints ();
    // Assign the x and y grid positions.
    gbc.gridx = gridx;
    gbc.gridy = gridy;
    // Assign the number of grid blocks horizontally and
    // vertically that are occupied by the component.
    gbc.gridwidth = gridw;
    gbc.gridheight = gridh;
    // Specify the component's resize policy (fill) and
    // the direction in which the component is positioned
    // when its size is smaller than available space (anchor).
    gbc.fill = fill;
    gbc.anchor = anchor;
    // Set the new constraints that the grid bag layout
    // manager will use.
    ((GridBagLayout) lm).setConstraints (com, gbc);
    // Add the component to the container.
    con.add (com);
    // ===========================================================
    // Class: MsgBox
    // This class displays a message box to the user. The message
    // is usually an error message. The user must press the Ok
    // button to terminate the message box.
    // ===========================================================
    class MsgBox extends Dialog implements ActionListener
    public void actionPerformed (ActionEvent e)
    // Terminate the dialog box in response to the user
    // pressing the Ok button.
    dispose ();
    public MsgBox (Frame parent, String title, String msg)
    // Initialize the superclass layer.
    super (parent, title, true);
    // Store the msg argument in a Label object and add
    // this object to the center part of the dialog window.
    Label l = new Label (msg);
    add ("Center", l);
    // Create a Button object and add it to the south part
    // of the dialog window.
    Button b = new Button ("Ok");
    add ("South", b);
    // Make the current object a listener to events that
    // occur as a result of the user pressing the Ok
    // button.
    b.addActionListener (this);
    // Make sure that the Ok button has the focus.
    b.requestFocus ();
    // Do not allow users to resize the dialog window.
    setResizable (false);
    // Allow the layout manager to choose an appropriate
    // size for the dialog window.
    pack ();
    // Make sure that the dialog window is visible.
    setVisible (true);
    ====================END OF CODE =======================

    You should first start by formatting the code before
    posting. I lost my interest as I browsed thorugh the
    code.
    Read here -
    http://forum.java.sun.com/help.jspa?sec=formatting
    ...and its way too much code to expect anyone to read. Post a short excerpt of the part you are having trouble with.

Maybe you are looking for