Results 1 to 14 of 14
  1. #1
    aserothbw is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default JTextField not showing properly in GridBagLayout

    The gui I am working on consists of a series of JLabels and JTextField. When I run the gui, all of the objects display in the right places, but for some reason, the JTextFields are only appearing as small lines. when you type something into the box, the text filed expands to fit what is typed. In my previous (albeit limited) experience with GridBagLayouts, the JTextField came out in the proper size, and I am at a lost to explain why this one is acting differently from the others. Again, this particular gui is still a work in progess.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class recMod {
    
    	JTextField [] recData = new JTextField[8];
    	JButton [] recButtons = new JButton[3];
    	
    	public recMod() {
    		
    		JLabel [] recLabel = new JLabel[8];
    		JFrame frame = new JFrame("Record Modify");
    		JPanel panel = new JPanel();
    		
    		panel.setLayout(new GridBagLayout());
    		
    		recLabel[0] = new JLabel("First Name:");
    		recData[0] = new JTextField("");
    		addItem(panel, recLabel[0], 0, 0, 1, 1, GridBagConstraints.WEST);
    		addItem(panel, recData[0], 1, 0, 1, 1, GridBagConstraints.CENTER);
    		
    		
    		recLabel[1] = new JLabel("Last Name:");
    		recData[1] = new JTextField("");
    		addItem(panel, recLabel[1], 3, 0, 1, 1, GridBagConstraints.CENTER);
    		addItem(panel, recData[1], 4, 0, 1, 1, GridBagConstraints.EAST);
    		
    		
    		recLabel[2] = new JLabel("Vendor:");
    		recData[2] = new JTextField("");
    		addItem(panel, recLabel[2], 0, 1, 1, 1, GridBagConstraints.WEST);
    		addItem(panel, recData[2], 1, 1, 1, 1, GridBagConstraints.CENTER);
    		
    		
    		recLabel[3] = new JLabel("Vendor Location Code:");
    		recData[3] = new JTextField("");
    		addItem(panel, recLabel[3], 3, 1, 1, 1, GridBagConstraints.CENTER);
    		addItem(panel, recData[3], 4, 1, 1, 1, GridBagConstraints.WEST);		
    		
    		
    		recLabel[4] = new JLabel("user Email Address:");
    		recData[4] = new JTextField("");
    		addItem(panel, recLabel[4], 0, 2, 1, 1, GridBagConstraints.WEST);
    		addItem(panel, recData[4], 1, 2, 2, 1, GridBagConstraints.WEST);
    		
    		
    		recLabel[5] = new JLabel("Username:");
    		recData[5] = new JTextField("");
    		addItem(panel, recLabel[5], 0, 3, 1, 1, GridBagConstraints.WEST);
    		addItem(panel, recData[5], 1, 3, 1, 1, GridBagConstraints.CENTER);
    		
    		
    		recLabel[6] = new JLabel("Password:");
    		recData[6] = new JTextField("");
    		addItem(panel, recLabel[6], 3, 3, 1, 1, GridBagConstraints.CENTER);
    		addItem(panel, recData[6], 4, 3, 1, 1, GridBagConstraints.WEST);
    		
    		
    		recLabel[7] = new JLabel("user code:");
    		recData[7] = new JTextField("");
    		addItem(panel, recLabel[7], 0, 4, 1, 1, GridBagConstraints.WEST);
    		addItem(panel, recData[7], 1, 4, 1, 1, GridBagConstraints.WEST);
    
    		
    		frame.add(panel);
    		frame.setVisible(true);
    		frame.setSize(500,400);
    		
    	}
    	
    	private void addItem(JPanel p, JComponent c, int x, int y,
    			int width, int height, int align) {
    		
    		GridBagConstraints gbc = new GridBagConstraints();
    		
    		gbc.gridx = x;
    		gbc.gridy = y;
    		gbc.gridwidth = width;
    		gbc.gridheight = height;
    		gbc.weightx = 0.0;
    		gbc.weighty = 0.0;
    		gbc.insets = new Insets(5,5,5,5);
    		gbc.anchor = align;
    		gbc.fill = GridBagConstraints.HORIZONTAL;
    		p.add(c, gbc);
    		
    	}
    	
    	public static void main(String args[]) {
    		
    		new recMod();
    		
    	}
    }
    Last edited by Fubarable; 09-04-2009 at 10:24 PM. Reason: code tags added for readability

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by sun tutorial
    note: We encourage you to specify the number of columns for each text field. If you do not specify the number of columns or a preferred size, then the field's preferred size changes whenever the text changes, which can result in unwanted layout updates.
    . .

  3. #3
    aserothbw is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    That is actually the first thing i tried. When I first noticed the problem, I tried to set the column size of each textfield when it is created (ie recData[0] = new JTextField("", 10)), but there is no change in the gui when run.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Change your
    Java Code:
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    to some values then.

  5. #5
    aserothbw is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    changing the values for the weights (for example, 50 or 100) expands the textfields to about 2 characters long, and locks them to that size. It also creates huge gaps between the components in a row, and the row themselves.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Part of your problem may be in your setting the size of the JFrame rather than letting the layout managers find the best size via the pack() method. If your size is too small in the horizontal direction, it may compress your JTextFields to nothingness.

    e.g.,
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.HashMap;
    import java.util.Map;
    
    public class recMod {
      public enum RecFieldNames {
        FIRST_NAME("First Name:"),
        LAST_NAME("Last Name:"),
        VENDOR("Vendor:"),
        VENDOR_LOC_CODE("Vendor Location Code:"),
        USER_EMAIL("User Email Address:"),
        USER_NAME("Username:"),
        PASSWORD("Password:"),
        USER_CODE("User Code:");
    
        private String name;
        private RecFieldNames(String name) {
          this.name = name;
        }
        
        public String getName() {
          return name;
        }
      }
      
      private static final int FIELD_COLS = 7;
      private Map<RecFieldNames, JTextField> recFieldMap = 
        new HashMap<RecFieldNames, JTextField>();
      //JButton[] recButtons = new JButton[3];
    
      public recMod() {
    
        JFrame frame = new JFrame("Record Modify");
        JPanel panel = new JPanel();
    
        panel.setLayout(new GridBagLayout());
    
        addLabelField(panel, RecFieldNames.FIRST_NAME, 0, 0, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(panel, RecFieldNames.LAST_NAME, 2, 0, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.EAST);
        addLabelField(panel, RecFieldNames.VENDOR, 0, 1, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(panel, RecFieldNames.VENDOR_LOC_CODE, 2, 1, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.WEST);
        addLabelField(panel, RecFieldNames.USER_EMAIL, 0, 2, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.WEST);
        addLabelField(panel, RecFieldNames.USER_NAME, 0, 3, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(panel, RecFieldNames.PASSWORD, 2, 3, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.WEST);
        addLabelField(panel, RecFieldNames.USER_CODE, 0, 4, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.WEST);
    
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel);
        //frame.getContentPane().setPreferredSize(new Dimension(500, 400));
        ((JComponent) frame.getContentPane()).
            setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
      
      public String getFieldText(RecFieldNames rfn) {
        return recFieldMap.get(rfn).getText();
      }
    
      private void addLabelField(JPanel p, RecFieldNames recFieldNames, int x,
          int y, int width, int height, int labelAlign, int fieldAlign) {
    
        GridBagConstraints gbc = new GridBagConstraints();
        JLabel label = new JLabel(recFieldNames.getName());
        JTextField textField = new JTextField(FIELD_COLS);
        recFieldMap.put(recFieldNames, textField);
        
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        int midInset = (x == 2) ? 40 : 5;
        gbc.insets = new Insets(5, midInset, 5, 5);
        gbc.anchor = labelAlign;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        p.add(label, gbc);
    
        gbc.gridx = x + 1;
        gbc.anchor = fieldAlign;
        gbc.insets = new Insets(5, 5, 5, 5);
        p.add(textField, gbc);
      }
    
      public static void main(String args[]) {
    
        new recMod();
    
      }
    }
    Last edited by Fubarable; 09-04-2009 at 11:11 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Playing some more with this code...
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.HashMap;
    import java.util.Map;
    
    public class RecordModify {
      public enum RecFieldNames {
        FIRST_NAME("First Name:"),
        LAST_NAME("Last Name:"),
        VENDOR("Vendor:"),
        VENDOR_LOC_CODE("Vendor Location Code:"),
        USER_EMAIL("User Email Address:"),
        USER_NAME("Username:"),
        PASSWORD("Password:"),
        USER_CODE("User Code:");
    
        private String name;
        private RecFieldNames(String name) {
          this.name = name;
        }
        
        public String getName() {
          return name;
        }
      }
      
      private static final int FIELD_COLS = 7;
      private Map<RecFieldNames, JTextField> recFieldMap = 
        new HashMap<RecFieldNames, JTextField>();
      private JPanel mainPanel = new JPanel();
      private JButton getDataBtn = new JButton("Get Data");
      //JButton[] recButtons = new JButton[3];
    
      public RecordModify() {
        mainPanel.setLayout(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
        addLabelField(mainPanel, RecFieldNames.FIRST_NAME, 0, 0, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(mainPanel, RecFieldNames.LAST_NAME, 2, 0, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.EAST);
        addLabelField(mainPanel, RecFieldNames.VENDOR, 0, 1, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(mainPanel, RecFieldNames.VENDOR_LOC_CODE, 2, 1, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.WEST);
        addLabelField(mainPanel, RecFieldNames.USER_EMAIL, 0, 2, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.WEST);
        addLabelField(mainPanel, RecFieldNames.USER_NAME, 0, 3, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.CENTER);
        addLabelField(mainPanel, RecFieldNames.PASSWORD, 2, 3, 1, 1,
            GridBagConstraints.CENTER, GridBagConstraints.WEST);
        addLabelField(mainPanel, RecFieldNames.USER_CODE, 0, 4, 1, 1,
            GridBagConstraints.WEST, GridBagConstraints.WEST);
    
        JLabel blankLabel = new JLabel(" ");
        addItem(mainPanel, blankLabel, 0, 5, 4, 1, GridBagConstraints.CENTER);
        addItem(mainPanel, getDataBtn, 0, 6, 4, 1, GridBagConstraints.CENTER);
    
      }
      
      public String getFieldText(RecFieldNames rfn) {
        return recFieldMap.get(rfn).getText();
      }
      
      public void setFieldText(RecFieldNames rfn, String text) {
        recFieldMap.get(rfn).setText(text);
      }
      
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      public void getDataAddListener(ActionListener al) {
        getDataBtn.addActionListener(al);
      }
    
      private void addLabelField(JPanel p, RecFieldNames recFieldNames, int x,
          int y, int width, int height, int labelAlign, int fieldAlign) {
    
        JLabel label = new JLabel(recFieldNames.getName());
        JTextField textField = new JTextField(FIELD_COLS);
        recFieldMap.put(recFieldNames, textField);
        
        addItem(p, label, x, y, width, height, labelAlign);
        addItem(p, textField, x + 1, y, width, height, fieldAlign);
      }
    
      private void addItem(JPanel p, Component component, int x, int y, 
          int width, int height, int align) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbc.weightx = 1.0;
        gbc.weighty = 0.0;
        int midInset = (x == 2) ? 40 : 5;
        gbc.insets = new Insets(5, midInset, 5, 5);
        gbc.anchor = align;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        p.add(component, gbc);
      }
      
      private static void createAndShowGUI() {
        final RecordModify recMod = new RecordModify();
        recMod.getDataAddListener(new ActionListener() {
          public void actionPerformed(ActionEvent arg0) {
            for (RecFieldNames recFNames : RecFieldNames.values()) {
              String fName = recFNames.getName();
              String text = recMod.getFieldText(recFNames);
              System.out.printf("%22s %-10s%n", fName, text);
            }
          }
        });
        
        JFrame frame = new JFrame("Record Modify");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(recMod.getMainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }

  8. #8
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default corrected code

    I JUST PLAYED A BIT WITH UR PIECE OF CODE .
    1>set size of textfield not more than 10 > coz frame size is small .increase both if u want .
    2> i played with co-ordinates of ur addItem METHOD of gridBagLayout and for these size and placement of labels and textfields u need to increase a bit frame size .. .. everything is cool now .. ur correct code is below .. copy it .n compile..
    goes here--------


    import javax.swing.*;
    import java.awt.*;

    public class recMod {

    JTextField [] recData = new JTextField[8];
    JButton [] recButtons = new JButton[3];

    public recMod() {

    JLabel [] recLabel = new JLabel[8];
    JFrame frame = new JFrame("Record Modify");
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());

    recLabel[0] = new JLabel("First Name:");
    recData[0] = new JTextField(10);
    addItem(panel, recLabel[0], 0, 0, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[0], 2, 0, 2, 1, GridBagConstraints.WEST);


    recLabel[1] = new JLabel("Last Name:");
    recData[1] = new JTextField(10);
    addItem(panel, recLabel[1], 4, 0, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[1], 6, 0, 2, 1, GridBagConstraints.WEST);


    recLabel[2] = new JLabel("Vendor:");
    recData[2] = new JTextField(10);
    addItem(panel, recLabel[2], 0, 2, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[2], 2, 2, 2, 1, GridBagConstraints.CENTER);


    recLabel[3] = new JLabel("Vendor Location Code:");
    recData[3] = new JTextField(10);
    addItem(panel, recLabel[3], 4, 2, 2, 1, GridBagConstraints.CENTER);
    addItem(panel, recData[3], 6, 2, 2, 1, GridBagConstraints.WEST);


    recLabel[4] = new JLabel("User Email Address:");
    recData[4] = new JTextField(10);
    addItem(panel, recLabel[4], 0, 4, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[4], 2, 4, 2, 1, GridBagConstraints.WEST);


    recLabel[5] = new JLabel("Username:");
    recData[5] = new JTextField(10);
    addItem(panel, recLabel[5], 0, 6, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[5], 2, 6, 2, 1, GridBagConstraints.CENTER);


    recLabel[6] = new JLabel("Password:");
    recData[6] = new JTextField(10);
    addItem(panel, recLabel[6], 4, 6, 2, 1, GridBagConstraints.CENTER);
    addItem(panel, recData[6], 6, 6, 2, 1, GridBagConstraints.WEST);


    recLabel[7] = new JLabel("user code:");
    recData[7] = new JTextField(10);
    addItem(panel, recLabel[7], 0, 8, 2, 1, GridBagConstraints.WEST);
    addItem(panel, recData[7], 2, 8, 2, 1, GridBagConstraints.WEST);


    frame.add(panel);
    frame.setVisible(true);
    frame.setSize(600,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    }

    private void addItem(JPanel p, JComponent c, int x, int y,
    int width, int height, int align) {

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = width;
    gbc.gridheight = height;
    gbc.weightx = 0.0;
    gbc.weighty = 0.0;
    gbc.insets = new Insets(5,5,5,5);
    gbc.anchor = align;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    p.add(c, gbc);

    }

    public static void main(String args[]) {

    new recMod();

    }
    }

  9. #9
    aserothbw is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    Do the textfields have to be a uniform size when using gridbag? Originally, I was going to try and set the textfields to sizes based on the data coming in/out.

  10. #10
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    In gridbag layout all components like button, textfield occupies a particular cell .. depending upon the co-ordinates we give .. so expanding it at the time of using is a bit of complexity .. i guess ..

    anyways the textfiled can accomodate enough amount of text inside it irrespective of its visible size ..
    So i think this can get ur work done ..

  11. #11
    aserothbw is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    from a function standpoint it wouldn't really matter, that is true. I ask more from a asthetics point of view. Some of the entries, like the location code and user type, are only 3-6 characters long, so it doesn't really make sense to give them really big textfield. I guess i'll play around with it and see what I can do.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  13. #13
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    okay .. actually i took u in a wrong way , u asked like u want textfields of different size ...
    and i thot tht u want to increase or decrease the size of fields automatically at the time of enetering data into it .. "this was not the case" ...

    u can definately have different textfileds of different sizes " small ones 4 ur location code ans all tht"

    our MODERATER "Fuberable" posted above those codes .. so take it and compile it .. yo ...

  14. #14
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Fubarable, once again your posts helped me. Had trouble with textfield not being big enougth. added pack() voila. worked.

Similar Threads

  1. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  2. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 02:06 PM
  3. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  4. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •