Results 1 to 4 of 4
  1. #1
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Creating an array to store JLabel names

    So instead of creating five JLable objects individually and then adding them using add() which takes alot of space all up, I thought of creating strings to store the labels inside and then indexing through a string array and attaching each string to each label. When I try run I get an Exception. Thoughts please!

    package GUI;

    import java.awt.Container;
    import java.awt.FlowLayout;

    import javax.swing.*;

    public class MainFrame extends JFrame
    {
    public MainFrame()
    {
    //super(); Can use empty constructor and then set title of application.
    //setTitle("Main Frame"); In other words, long way.
    super("Test Frame 1");// Short way
    }

    public void createUserDetailForm()
    {
    setSize(500,700);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel [] labels = new JLabel[5];
    String [] strings = {"Name","Age","Sex","Address","Email"};
    /*JLabel nameLabel = new JLabel("Name:");
    JLabel ageLabel = new JLabel("Age:");
    JLabel sexLabel = new JLabel(":");
    JLabel addressLabel = new JLabel(":");
    JLabel emailLabel = new JLabel(":");*/
    for(int i = 0; i < labels.length; i++)
    {
    labels[i].setName(strings[i]);
    }
    FlowLayout flow = new FlowLayout();
    setLayout(flow);
    for(int i = 0; i > labels.length; i++)
    {
    add(labels[i]);
    }
    setVisible(true);
    }
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Creating an array to store JLabel names

    Hi alex,

    There are a couple of issues with your code.
    • Firstly, please wrap your code in [code] tags as it makes it easier to read.
    • You have defined an array of labels but you have not instantiated any. You still need to create new instances using the keyword 'new'.
    • There is an issue with the second for loop. You are currently looping whilst 'i' is greater than labels.length.


    Regards.

  3. #3
    alex.start is offline Member
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    9
    Rep Power
    0

    Default Re: Creating an array to store JLabel names

    Ahhh so annoying! Ok I have rearranged my code, no more array for labels and all I want is to layout my JLabels row by row, as long as I have rows. I have looked for the past 2 hours and no solution. I don't want to use grids as it is not the design I am going for. I want to learn to create desktop applications and applets in Java and to help me with my code I am re-designing interfaces.

    This is what I am trying to re-create:
    Google Image Result for http://undocumentedmatlab.com/images/StaticButton_Java_Frame.png

    [code]
    public void createUserDetailForm()
    {
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//exit when X is pressed
    FlowLayout flo = new FlowLayout();//create FlowLayout object to arrange components in container
    FlowLayout flo2 = new FlowLayout();//

    JLabel name_Label = new JLabel("Name:");
    JLabel age_Label = new JLabel("Age:");
    JLabel sex_Label = new JLabel("Sex:");
    JLabel address_Label = new JLabel("Address:");
    JLabel email_Label = new JLabel("Email:");
    add(name_Label);
    add(text1.text_input);
    setLayout(flo);
    add(age_Label);
    add(sex_Label);
    add(address_Label);
    add(email_Label);
    setVisible(true);
    }
    [code]
    Creating an array to store JLabel names-window1.jpg


    Any or all help is very much appreciated.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Creating an array to store JLabel names

    There is nothing to stop you from using arrays to define your labels, you just need to ensure these are initialized.

    As for the layout, have a look at GridBag layout. It provides much more flexibility and control over the layout compared to Grid.
    How to Use GridBagLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    Regards.

Similar Threads

  1. Replies: 2
    Last Post: 03-10-2012, 12:26 AM
  2. create and array with variable names
    By jmrire in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 08:56 PM
  3. Creating objects with automatic names
    By kez1304 in forum New To Java
    Replies: 3
    Last Post: 06-14-2011, 06:32 PM
  4. Creating abjects with different names
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-26-2007, 12:14 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
  •