1

I have all of the labels working correctly but the userLabel[3] is not positioning properly No matter what I do, the label "Color:" always shows up on the frame with a x-coordinate of 0 and a y-coordinate that is half way down the frame.

    JLabel[] userLabel = new JLabel[4];
    for(int p = 0; p < userLabel.length; p++){
        userLabel[p] = new JLabel();
        userLabel[p].setSize(100,50);
        frameSetUp.add(userLabel[p]);
    }
    userLabel[0].setText("Width of Frame:");
    userLabel[1].setText("Height of Frame:");
    userLabel[2].setText("# OF Balls:");
    userLabel[3].setText("Color:");

    userLabel[0].setLocation(10,35);
    userLabel[1].setLocation(10,85);
    userLabel[2].setLocation(10,135);
    userLabel[3].setLocation(0,0); //no matter what coordinates I change this too, it wont reposition

Image: [IMG]http://i41.tinypic.com/23jfo9l.png[/IMG] http://i41.tinypic.com/23jfo9l.png

mKorbel
  • 109,525
  • 20
  • 134
  • 319
JavaNewb
  • 87
  • 6

2 Answers2

5
  1. Don't use setLocation, setBounds, null layouts or absolute positioning.
  2. Instead use the layout managers including perhaps nested JPanels, each using its own layout manager to achieve pleasing easy to maintain GUI's.
  3. For more help, show a picture of what you're trying to achieve, what you actually are achieving, and post a minimal working example, code that is small, that compiles and runs, and shows us your problem.

e.g.,

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class InputForm extends JPanel {
   private static final int COLUMNS = 10;
   private static final int GAP = 3;
   private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
   private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
   private String[] labelTexts;
   private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();

   public InputForm(String[] labelTexts) {
      this.labelTexts = labelTexts;
      setLayout(new GridBagLayout());
      for (int i = 0; i < labelTexts.length; i++) {
         String text = labelTexts[i];
         JTextField field = new JTextField(COLUMNS);
         fieldMap.put(text, field);

         addLabel(text, i);
         addTextField(field, i);
      }
   }

   public String[] getLabelTexts() {
      return labelTexts;
   }

   private void addTextField(JTextField field, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 1;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = TEXTFIELD_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(field, gbc);
   }

   private void addLabel(String text, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 0;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = LABEL_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(new JLabel(text), gbc);
   }

   public String getFieldText(String key) {
      String text = "";
      JTextField field = fieldMap.get(key);
      if (field != null) {
         text = field.getText();
      }
      return text;
   }

   private static void createAndShowGui() {
      String[] labelTexts = new String[] { "Width of Frame:",
            "Height of Frame:", "# OF Balls:", "Color:" };
      InputForm inputForm = new InputForm(labelTexts);

      int result = JOptionPane.showConfirmDialog(null, inputForm, "Input Form",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         for (String text : labelTexts) {
            System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
         }
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Which will display like so:

enter image description here

The beauty of this code, is if you wish to add another field, say a line thickness field, and want to add it so that it is second to last, then the only change needed to the code would be to change this:

  String[] labelTexts = new String[] { "Width of Frame:",
        "Height of Frame:", "# OF Balls:", "Color:" };

to this:

  String[] labelTexts = new String[] { "Width of Frame:",
        "Height of Frame:", "# OF Balls:", "Line Thickness:", "Color:" };

Which results in:

enter image description here

No need to have to calculate how to change the Color label or JTextField's locations as the layout manager does all the hard work for you.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Updated with picture! + this is part of a project and I haven't used layout managers at all and I don't have enough time to learn them since this project is due very soon – JavaNewb Jan 18 '14 at 18:59
  • @JavaNewb: Note -- I can't see your images due to firewall restrictions. If you want it done right, use the layout managers, it won't take all that long to learn the basics, and the benefits are large. Please have a look at this tutorial: [A Visual Guide to Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). If all you want is a quick and dirty solution, then I'll leave it to others to give to you for I'll take no part in this. – Hovercraft Full Of Eels Jan 18 '14 at 19:01
  • @JavaNewb: for example. – Hovercraft Full Of Eels Jan 18 '14 at 19:58
-1

Finally got the answer try increasing the size of the JLabel array by 1 and run it will work fine

import javax.swing.JFrame;
import javax.swing.JLabel;


public class Labelss{

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setBounds(50, 50, 700, 550);
        JLabel[] userLabel = new JLabel[6];
        for(int p = 0; p < userLabel.length; p++){
            userLabel[p] = new JLabel();
        }
        userLabel[0].setBounds(10,35,100,50);
        userLabel[1].setBounds(10,85,100,50);
        userLabel[2].setBounds(10,135,100,50);
        userLabel[3].setBounds(10,185,100,50);
        userLabel[4].setBounds(10,235,100,50);
        userLabel[0].setText("Width of Frame:");
        userLabel[1].setText("Height of Frame:");
        userLabel[2].setText("# OF Balls:");
        userLabel[3].setText("Color:");
        userLabel[4].setText("Stack overflow:");


        for(int p = 0; p < userLabel.length; p++){
            frame.add(userLabel[p]);
        }

        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }


}
praveen_mohan
  • 455
  • 2
  • 7
  • 15