Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: GridBagLayout help

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default GridBagLayout help

    How do I layout a frame window so that I have a text field as the first object, two buttons centered underneath text field next, and three labels one right next to each other starting in the bottom left corner underneath the buttons?

    This is what I have so far:

    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
     
    public class TextEditButtons extends Frame implements ActionListener
    {
         int wrdCnt = 0;
         long inSize;
         long outSize;
         char inAry[];
         char outAry[];
         String inStr = new String();
         String outStr = new String();
         String shrtstWrd;
         String lngstWrd;
         StringTokenizer st1;
         StringTokenizer st2;
         StringTokenizer st3;
         TextArea txt;
         Button save;
         Button quit;
         Label wrdCntLab;
         Label shrtstWrdLab;
         Label lngstWrdLab;
         File inTxt;
         File outTxt;
         InputStream inTxtStrm;
         OutputStream outTxtStrm;
         GridBagLayout gbl;
         GridBagConstraints gbc;
     
         public TextEditButtons()
         {
              super("Text Edit - Button version");    
              txt = new TextArea(20, 50);
              save = new Button("Save");
              quit = new Button("Quit");
              wrdCntLab = new Label();
              shrtstWrdLab = new Label();
              lngstWrdLab = new Label();
     
              gbc = new GridBagConstraints();
              setLayout(gbl = new GridBagLayout());
     
     
              /*add(save);
              add(quit);
              add(wrdCntLab);
              add(shrtstWrdLab);
              add(lngstWrdLab);*/
     
              gbc.weightx = 0;
              gbc.ipadx = 0;
              gbc.anchor = gbc.FIRST_LINE_START;
     
              gbl.setConstraints(txt, gbc);
     
              add(txt);
     
              gbc.weightx = 0;
              gbc.ipadx = 0;
              gbc.anchor = gbc.LINE_START;
     
              gbl.setConstraints(save, gbc);
              gbl.setConstraints(quit, gbc);
     
              add(save);
              add(quit);
     
              try
              {
                   inTxt = new File("TextEdit.java");
                   inTxtStrm = new FileInputStream(inTxt);
                   inSize = inTxt.length();
              }
              catch (FileNotFoundException fnfe1)
              {
                   txt.setText("Input file not found");
              }
     
              try
              {
                   outTxtStrm = new FileOutputStream("outputtext.txt");
              }
              catch (FileNotFoundException fnfe2)
              {
                   txt.setText("Output file not found");
              }
     
              try
              {
                   inAry = new char [(int) inSize];
     
                   for (int inAryInd = 0; inAryInd < inSize; inAryInd++)
                   {
                       inAry[inAryInd] = (char) inTxtStrm.read();
                   }
              }
              catch (IOException ioe)
              {
                   txt.setText("I/O error");
              }
     
              for (int inAryInd = 0; inAryInd < inSize; inAryInd++)
              {
                   inStr += inAry[inAryInd];
              }
              txt.setText(inStr);
     
              addWindowListener(new GenericWindowClosing());          
     
              save.addActionListener(this);
              quit.addActionListener(this);
     
              st1 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
              st2 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
              st3 = new StringTokenizer (inStr, ",;:.?! \t\n()[]{}+-*/\\\"");
         }
     
     
         private int wordCount (StringTokenizer st1)
         {
              while (st1.hasMoreTokens())
              {
                    wrdCnt++;
                    st1.nextToken();
              }
     
              return wrdCnt;
         }
     
         private String shortestWord (StringTokenizer st2)
         {
              shrtstWrd = st2.nextToken();
     
              while (st2.hasMoreTokens())
              {
                    String nxtWrd = st2.nextToken();
                    if(nxtWrd.length() < shrtstWrd.length())
                    {
                         shrtstWrd = nxtWrd;
                         st3.nextToken();
                    }
              }
     
              return shrtstWrd;
         }
     
         private String longestWord (StringTokenizer st3)
         {
              lngstWrd = st3.nextToken();
     
              while (st3.hasMoreTokens())
              {
                    String nxtWrd = st3.nextToken();
                    if(nxtWrd.length() > lngstWrd.length())
                    {
                         lngstWrd = nxtWrd;
                         st3.nextToken();
                    }
              }
     
              return lngstWrd;
         }
     
         public static void main (String args[])
         {
              int wc;
              String sw;
              String lw;
     
              TextEditButtons teb = new TextEditButtons();
     
              teb.setSize(new Dimension(400,500));
              teb.setVisible(true);
     
              wc = teb.wordCount(teb.st1);
              sw = teb.shortestWord(teb.st2);
              lw = teb.longestWord(teb.st3);
              teb.wrdCntLab.setText("Word Count: " + wc);
              teb.shrtstWrdLab.setText("Shortest word: " + sw);
              teb.lngstWrdLab.setText("Longest word: " + lw);
         }
     
         public void actionPerformed(ActionEvent ae)
         {
               String actionStr = ae.getActionCommand();
     
               if (actionStr.equals("Save"))
               {
                    outStr = txt.getText();
                    outAry = outStr.toCharArray();
     
                    for (int outAryInd = 0; outAryInd < outStr.length(); outAryInd++)
                    {
                         try
                         {
                              outTxtStrm.write(outAry[outAryInd]);
                         }
                         catch (Exception e)
                         {
     
                         }
                    }
               }
               if (actionStr.equals("Quit"))
               {
                    System.exit(0);
               }
         }
    }

    The buttons end up beside the text field and the labels are out of the frame window.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: GridBagLayout help

    Quote Originally Posted by mjpam View Post
    How do I layout a frame window so that I have a text field as the first object, two buttons centered underneath text field next, and three labels one right next to each other starting in the bottom left corner underneath the buttons?

    This is what I have so far:

    <snip largely irrelevant code/>

    The buttons end up beside the text field and the labels are out of the frame window.
    That's because you haven't set a gridx/gridy/colspan. Recommended reading: the GridBagLayout API and How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    By the way, the usual programming idiom when adding components to a container that has a GridBagLayout is to add the component using the overload of add(...) that takes two parameters.
    container.add(component, gbc);
    Not by first setting the constraints and then adding the component, as in your code.

    db
    Last edited by Darryl.Burke; August 19th, 2010 at 06:33 PM.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: GridBagLayout help

    Quote Originally Posted by mjpam View Post
    How do I layout a frame window so that I have a text field as the first object, two buttons centered underneath text field next, and three labels one right next to each other starting in the bottom left corner underneath the buttons?

    This is what I have so far:

    <snip largely irrelevant code/>

    The buttons end up beside the text field and the labels are out of the frame window.
    That's because you haven't set a gridx/gridy/colspan. Recommended reading: the GridBagLayout API and How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    By the way, the usual programming idiom when adding components to a container that has a GridBagLayout is to add the component using the overload of add(...) that takes two parameters.
    container.add(component, gbl);
    Not by first setting the constraints and then adding the component, as in your code.

    When posting code on a forum, you should post only code relevant to the problem. In this case, the construction of the GUI, sans any program logic which is just clutter when seeking help with a layout problem.

    db

Similar Threads

  1. [SOLVED] how can i resize JTextField, JButton using GridBagLayout,... TNX
    By sny in forum AWT / Java Swing
    Replies: 0
    Last Post: March 4th, 2010, 08:57 AM
  2. [SOLVED] Problem with GridBagLayout
    By lumpy in forum AWT / Java Swing
    Replies: 3
    Last Post: February 28th, 2010, 11:58 AM