Initializing Array of JLabels

This one's got me scratching my head. The critical lines of code are:
     final JLabel[] labelTable = new JLabel[3][9];
     labelTable[0][0] = new JLabel("Item Name");And then there are several more lines like the second. The problem I'm having is that, on every line like the second, I get two errors: one complaining that there isn't a closing ] immediately after the very first opening one, and then another error that I just assume is caused by the first error FUBARing the line's syntax.
Why on earth would the compiler not want a value in the array element brackets? I did just create the array, right? It knows labelTable is an array, or it should...
I've tried removing the "final", and I've tried removing the " = new JLabel[3][9]", and neither fixes the problem. Any ideas?

There's not a lot more than what I've already provided...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Gui implements ActionListener {
     final JLabel[][] labelTable = new JLabel[9][3];
     labelTable[0][0] = new JLabel("Item Name");
     labelTable[0][1] = new JLabel("Cost");
     labelTable[0][2] = new JLabel("Reserved By:");
     /* What follows will be replaced by a file read when I'm good and ready. */
     labelTable[1][0] = new JLabel("Book about Cheez-Its");
     labelTable[1][1] = new JLabel("$$");
     labelTable[1][2] = new JLabel("Santa Claus");
     labelTable[2][0] = new JLabel("Amazing Electronic Gadget");
     labelTable[2][1] = new JLabel("$$$");
     labelTable[2][2] = new JLabel("none");
     labelTable[3][0] = new JLabel("Ye Holye Thumbtack");
     labelTable[3][1] = new JLabel("$");
     labelTable[3][2] = new JLabel("Clarence Thomas");Those are lines 7 through 26. The lines before 7 are a documentation comment. The lines after 26 are standard, straight-out-of-the-Tutorial methods for implementing the rest of the GUI (look'n'feel String, initLookAndFeel(), createAndShowGUI()...) and also setting a couple rows of buttons. The code for the buttons compiles fine. I just get 24 errors corresponding to those twelve initializations of labelTable[x][y].

Similar Messages

  • Array of JLabel HELP

    how would I create an array of JLabels?

    If you are not sure of how many object you want to place within the array, ArrayLists can grow dynamically. Here is how to use it:
    java.util.ArrayList arraylist = new java.util.ArrayList();
    //To Add Your Class:
    YourClass yourClass = new YourClass();
    arraylist.add(yourClass);
    //To Use Your Class:
    YourClass yourClass = (YourClass)arrayList.get(0);

  • Using array create JLabel

    i have one problem about using addKeyListener, i'm using array to create JLabel but once i've compile it, it came out a message as i shown, so where is my problem, hope someone can fix it for me and explain it for me, thank you
    ---------- Capture Output ----------
    "C:\Program Files\Java\jdk1.6.0_03\bin\javac.exe" Login.javaLogin.java:78: not a statement
    inputTextName[1]KeyPressed( event );
    ^
    Login.java:78: ';' expected
    inputTextName[1]KeyPressed( event );
    ^
    2 errors
    Terminated with exit code 1.---------- Capture Output ----------
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Login extends JFrame
         //JLabel[] lblName = {"lblName1","lblName2"};
         JLabel[] labelName;
         //JTextField[] txtfldName = {input1,input2};
         JTextField[] inputTextName;
         //JLabel[] array = new JLabel[veryLargeNumber];
         JButton[] btnName = new JButton[3];
    public void userInterface()
         this.setBackground(Color.blue);
         this.setTitle("Log in");
         this.setSize(285,130);
         this.setVisible(true);
         Container contentPane = getContentPane();
         contentPane.setLayout(null);
         labelName[0] = new JLabel();
         labelName[0].setText("ID: ");
         labelName[0].setBounds(16, 16, 130, 21);
         this.add(labelName[0]);     
         inputTextName[0] = new JTextField();
         inputTextName[0].setText(" ");
         inputTextName[0].setBounds(50, 16, 150, 21);
         inputTextName[0].setHorizontalAlignment(JTextField.LEFT);
         this.add(inputTextName[0]);
         labelName[1] = new JLabel();
         labelName[1].setText("Password: ");
         labelName[1].setBounds(16, 48, 104, 21);
         this.add(labelName[1]);
         inputTextName[1] = new JTextField();
         inputTextName[1].setText(" ");
         inputTextName[1].setBounds(50, 48, 150, 21);
         inputTextName[1].setHorizontalAlignment(JTextField.LEFT);
         this.add(inputTextName[1]);
         btnName[0] = new JButton();
         btnName[0].setText("login");
         btnName[0].setBounds(120,80,65,20);
         this.add(btnName[0]);
         btnName[1] = new JButton();
         btnName[1].setText("exit");
         btnName[1].setBounds(190,80,65,20);
         this.add(btnName[1]);
         inputTextName[1].addKeyListener(
             new KeyAdapter() // anonymous inner class
                // method called when user types in cartonsJTextField
                public void keyPressed( KeyEvent event )
                  inputTextName[1]KeyPressed( event );
             } // end anonymous inner class
          ); // end call to addKeyListener
    //call function
         public Login()
              userInterface();          
         public static void main (String args[])
              Login application = new Login();
               application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
         }Edited by: slamgundam on Oct 24, 2007 11:48 PM

    slamgundam wrote:
    the purpose is try to get the text in the inputTextField
    inputTextName[1].addKeyListener(
        new KeyAdapter()
             // method called when user types in cartonsJTextField
             public void keyPressed( KeyEvent event )
                   String str = inputTextName[1].getText();
                   System.out.println(str);
    );

  • Possible to make arrays of JLabels?

    I would like to make arrays of JLabels, Iconimages, and Jpanels, but I can't find how to do it.
    Is it eaven possible?
    How can I proceed instead...?

    Why not????
    Infact you can make an array of any premitive type or of any object... Just you need to use this syntax...
    dataType[] arrVarName = new dataType[size];
    for example,
    JLabel[] lblArr = new JLabel[10];
    This will create the new array of JLabel type of 10 elements size... You can initialize each element like this:
    for(int i = 0; i < lblArr.length; i++){
    lblArr[i] = new JLabel("Hello " + i);
    and you can print each element like this:
    for(int i = 0; i < lblArr.length; i++){
    System.out.println(lblArr.getText());
    Okay... Still if you are having any doubt you are always welcome

  • Printing an array to JLabel

    Let me preface this post by saying that I am a novice programmer. I realize my code my be subpar ..to say the least :)
    I wrote a small practice program that when run, prints 5 sets of random numbers (ex. A3KRM-QP4S7-5GMBR-OY68B-Q8GBZ).
    That part works fine on the command line, however I wanted to incorporate swing and display the output on a form. This is where I'm running into a problem. I'm trying to place output from a character array into a string and then print that to a label. The result is its printing a bunch of squares (ASCII?).
    Here is where I'm creating my sets of random numbers, in the method "myGenerator()".
    class keygen
    //initialize random number generator
    Random rand = new Random();
    //initialize arrays          
    char data[] = new char[36];
    char key[] = new char[5];
    //initialize integers and strings          
    char keys[] = new char[30];
    int a = 0, n = 35, q = 65, z = 48;
    //CONSTRUCTOR
    keygen()
    //main method
    public char[] myGenerator()      
    //Populate charater array with numbers and letters
    for (int x = 0; x <=25; x++)
    data[x] = ((char)q);
    q++;          
    for(int x = 26; x <=35; x++)
    data[x] = ((char)z);
    z++;
    //Print 5 groups of random characters
    do
    for (int b = 0;  b < 5; b++)
    System.out.print(data[rand.nextInt(n+1)]);
    f++;
    System.out.print("-");
    a++;
    f++;
    }     while (a < 4);
    do
    System.out.print(data[rand.nextInt(n+1)]);
    a++;
    f++;
    }     while (a < 10);
    return keys;
    }Here is where I'm trying to add the output to the main form in a label (in the frmMain() constructor).
    class frmMain extends JFrame
    //add main menu bar to JFrame
    private jmbMainMenu mmb;
    private JFrame _mainFrame;
    private JLabel mylabel = new JLabel();
    void init()
    setTitle("SandBox");
    setJMenuBar(mmb = new jmbMainMenu() );
    setSize (500,300);
    setVisible(true);
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e)
    System.exit(0);
    frmMain()
    //Labels
    setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));
    String str1 = new String(new keygen().myGenerator());
    mylabel.setText(str1);          
    this.add(mylabel);
    }My next step is to add a "Generate" button that displays a new random code when clicked, but I need to find out and understand what I'm doing wrong here first. Feel free to rip apart the code, and I'll appreciate any feedback.
    Thanks!

    I do have a main, I just didn't add it in my first post,
    public class keyUtil
         public static void main (String[] args)
         new frmMain().init();
    }I forgot to take out the "f" variable. I was using it earlier and forgot to remove it from the code. I can also see how my loop is confusing.
    Mel,
    Your absolutely right. Originally I tried doing something like this,
    do
         for (int b = 0;  b < 5; b++)
              keys[f] = (data[rand.nextInt(n+1)]);
              f++;
              keys[f] = ("-");
              a++;
              f++;
    }     while (a < 4);
    do
              keys[f] = (data[rand.nextInt(n+1)]);
              a++;
              f++;
    }     while (a < 10);
         return keys;Thats where the "f" came from. "f" was originally initialized to 0. I thought this would put a new character into each element of the "keys" array, but it didn't seem to work. I will go back and try this again.
    Edited by: jd3cker on Jun 14, 2009 3:40 PM
    Edited by: jd3cker on Jun 14, 2009 3:45 PM

  • Initializing array of Object T

    Is there any way to initialize an array using the class tokens?
    In my case I want to create a new T[100]; without (initializing any specific elements that is) - it does not seem to function very well...

    Not really, because T isn't known at run time (erasure) so you can't create an array of a specific type this way.
    If you've got a class object for T you can use Array.newInstance() and cast it to
    T[]. (Though you'll get an "unchecked cast" warning).

  • Initializing array of user-defined class

    I have a need to make an array of my own classes, simply to make a database organized.
    First, I defined my class at the start of the program:
    public class FBase
    String FName = "";
    String FDate = "";
    int FNameLength = 0;
    int FDateLength = 0;
    int status = 0; // value of 0, 1, 2
    } FExample = New FBase;
    This gives me one instance of the class, FExample.
    Now I can store data thus:
    FExample.FName = "Jones";
    FExample.FDate = "1934";
    FExample.FNameLength = 5;
    FExample.FDateLength = 4;
    FExample.status = 2;
    What I would like is an array of such instances of this class.
    I have followed the standard procedure for initializing an array, but my efforts are not accepted.
    In place of the line above initializing one instance in FExample, I have tried -- both at the top of the program and in public void init() -- the following code:
    FBase[] Example = New FBase[200];
    FBase Example[] = New FBase[200];
    FBase[] Example;
    Example = new FBase[200];
    None of these are accepted. How do I do this?

    Alright -- I found one problem and solved it, but I still have another problem, pretty much the same problem I thought I was facing at the start, but now a little better defined.
    At first, my class did not work even with one instantiation, let alone when I tried to instantiate an array of the class. I dound that I did not have a constructor, and I need a constructor if I am to create an instance of the class (I am learning ...). So, the class now looks like this:
    public class FBase
    String FName = "";
    String FDate = "";
    public FBase(){} // constructor
    Now the following line of code works:
    FBase FExample = new FBase();
    I am able to use FExample.FName and FExample.FDate as proper variables.
    My problem still exists when I try to instantiate an array of the class. In place of the above line of code, I have tried this:
    FBase[] FExample = new FBase[200];
    The compiler accepts it, but now FExample[0].FName and FExample[0].FDate are not recognized as proper variables, and nothing goes on when I try to use them in the program.
    Any ideas?
    Edited by: vanhoey on Jun 27, 2008 12:38 PM
    Edited by: vanhoey on Jun 27, 2008 12:40 PM

  • Initializing array elements on a static initialization block

    Hello,
    I have this class:
    class Foo{
        public static Wing[] flights = new Wing[20]; //initialize with null Wing references
        static { //static initialization block - begin
            for(Wing w : flights){ //for each Wing slot on the array,
                w = new Wing();      //create a Wing object and refer to it
            } // static init block - endThe code above creates a Wing array, and the static init block should create 20 Wing objects on the array slots, but the objects are lost when I try to use it in the main method on the same class Foo:
        public static void main(String[] args){
             System.out.println(flights[3].name); //will throw NullPointerException -> Array elements have not been initialized  
        }How is that possible? The init code for(Wing w : flights) looks like has created copies of the objects, and not just references to the same object. If I change the static init block to the "old-fashioned for loop"
    for (int i=0; i < flights.length; i++){
    flights[i] = new Wing();then it works.
    But I think that the problem is not on the kind of for loop itself, because if I use same static initialization statement, with for (Wing w : flights) on the main method, instead of in a separate init block, the array gets populated with solid objects.
    Any ideas of what I am doing wrong?
    Java version: 5.0

    I think I got it. I am reseting the reference to point to a new Object in the heap instead of the array slot. :-P

  • Initializing array constructor to 0

    I have created a stats class, but I am not sure how to initialize the array constructor to be all zeros. It is a single interger array that can hold 50 values.

    int[] array = new int[50];Each array element will be initialized to zero.
    The Java� Tutorial - Arrays

  • Initializing arrays with non-default values

    Hi,
    I have a large array (300+ elements) and want it to be filled with all -1 at the start and not with 0.
    A couple of (bad) ideas of mine:
    - using an expression "array[0] = -1, array[1] = -1, ..." (a lot of typing work)
    - using a for loop with an expression "array[Locals.k] = -1" (very slow)
    - getting the array from "somewhere else" like e.g. a LabVIEW VI (cumbersome)
    Is there some expression syntax I can use?

    You can use SetElements(Locals.d,-1)  where d is the array.

  • Initializing array of objects

    This is my beginning effort to initialize an array of objects, but the compiler messages seem to lead me astray. What is the easiest way to do this, or can't it be done in a manner similar to this?
    Do I have to do it from a program loop or something?
    private static void testWidNum() {
    class testPair { double dbl; int nsf; }
    testPair[] testNums = new testPair[] { {999999999.,5}, {9999.,5} };
    This should be an easy answer, compared to the one to which I didn't get any reponses.
    dewayne

    Probably I may write it like this.
    public class SomeClass
      private void testWidNum() {
        testPair[] testNums = new testPair[]{
                                       new testPair(9999, 5),
                                       new testPair(9999, 5),
                                       new testPair(9999, 5)
    class testPair{
        testPair(double _dbl, int _nsf)
            dbl = _dbl;
            nsf = _nsf;
       double dbl; int nsf;
    }

  • Initializing arrays?

    Hello!
    In a VI have an empty 2D-array (3 columns, x rows),
    in which I want to insert a new row. The first time
    I start the VI, the row is correctly inserted. When
    I start the VI again, the array-insertion-VI doesn't
    insert anything. Ideas if I have to initialize
    the target array in any way?
    Thanks in advance, Michael.

    Hi,
    if the array is a front panel control/indicator you have to fill in some data in that area (even if it's 0.00) to "activate" those cells for future use.
    To be sure that everything it's done properly, initialize the array with default values for a specific number of rows and columns before any other operation.

  • How to insert a 2D array into the end of X axis of the initial array?

    Hi, friends, I have a question and look for an advice. I have several input data, each of which has 4 rows and unknow columns. Each time I receive the input, I need to combine it to all of the previous ones by adding it to the end of X axis of the previous combined data. I must do it because, later, I need to run my model and my model read input for each column of 4 rows. How should I do it? Also, should I initiate an array before reading all of the inputs? The inputs are F32 format. Thank you very much for any of your advice.

    Building a 2D array row-by-row is easy. You either use "build array" if you need to update the indicator during the run, or you simply autoindex at the loop  boundary (FOR or WHILE).
    Two possibilities are shown in the attached image. If your arrays are getting large and you use a while loop, there are possibilities that are more memory efficient. You would initialize a 2D array (e.g. filled with NaNs) of an upper boundary of the final expected size, then replace rows as you go.
    (Notice that if you use autoindexing on a FOR loop, the final size is known from the beginning so memory can be allocated efficiently)
    Message Edited by altenbach on 11-01-2006 09:19 AM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    AppendRows.png ‏5 KB

  • Initializing Array in a Unique Manner

    Hi All,
    I'm new on this and have tried for several hours how to do the following "simple" thing without any success. I'm using a cDAQ 9171 with a NI 9263 analog output module.
    My Problem: I need to start the following program when Start button is pushed. Please, in order of priority I'm giving you what I have to do.
    1. Analog output from an array that haves fixed values. Let it be 5 ; 4 ; 3 ; 2 ; 3 ; 4 . So I need to stay 1min in each value and then repeat the whole array for  10 times more.
    I Tried with the attached project, but it only generated a 1V signal, even when the appended array was changing...I don't know what happened.
    2. I wanted to create automatically the array i mentioned before. But it gave me so much headache that i left it.
    Thanks in advance for your help.
    Solved!
    Go to Solution.
    Attachments:
    v0.3.zip ‏24 KB

    Take advantage of auto-indexing tunnels to break out the data in your array.  And if you use 1 channel 1 sample rather than N channels 1 sample, you won't need to take that scalar value and build it back into an array.
    Attachments:
    fte v0.3MOD.vi ‏37 KB

  • URGENT - initializing array

    how can i copy the values from a fime into an array ?

    Oh I see. File.
    That depends on the data type of the array... do you want to store Strings, int, Objects...?

Maybe you are looking for

  • Thin driver and Oracle 8i

    Hi, Helpful suggestion needed, I have around 7 LIVE sites running on a single server .the database we are using is oracle 8i .When the 7th site was not launched everything was working fine .But when we launched the 7th site .the 7th site is very slow

  • Hash mismatch error when downloading OS X Mountain Lion

    I have an updated system for mac pro (bought first week of June 2012). Was trying to download OS X Mountain Lion several times but I keep getting the "can't download app hash mismatch" error notification. Would appreciate helpful tips!

  • Impact of revoking APEX_040000 view and privileges from public ?

    Forum... We are in an integration scenario where we do not want to have a user connecting through SQL see the apex product database objects to which apex has granted public access show up. ( As per the "Granted Priviliges" of the Apex documentation -

  • SRM setup config

    Hi, I'm currenct trying to setup the config for SRM as AddOn to ECC.  However, when I get to the step for initial replication and attempt to replicate categories, I get a short dump with the following error: Class PRODCAT does not exist Long text of

  • Accidentally changed the path of the home directory and now I can not get access my profile.

    before the way was /valumes/macintosh hd/igor but then i decided  to remove a space between macintosh and HD then I restarted my mac and after reloading I can not get access my profile. Thanks for the helping.