Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java array help

Name: Anonymous 2010-07-30 21:13

hey /prog/ just wondering if anyone could give me a hand with some java code. Basically I am trying to place labels from an array of JLabels into a JPanel using .add, but it doesn't want to work. I have never used an array of JLabels before, so i am certain that i fucked up somewhere.

Code:

public void setProp() {

        for (int i = 0; i < label.length; i++) {
            label[i] = new JLabel("");
            label[i].setPreferredSize(labelDim);
            label[i].setIcon(new ImageIcon(this.getClass().getResource("blank.gif")));
            label[i].setEnabled(true);
            label[i].setVisible(false);
            pnlMap.add(label[i]);

        }
    }


also blank.gif is just a black square and the dimensions are set to match the icon dimensions.

any input would be appreciated

Name: Anonymous 2010-07-31 3:02

>>23
Okay, start with the very basic: do what people are suggesting and setBounds(int x, int y, int width, int height) on each JLabel object as you create it.

Also, and this is a big also, add a finally in method openText to make sure the BufferedReader gets closed.
public void openText()
{
   String row = "";
   BufferedReader fileRead = null;
   try
   {
      fileRead = new BufferedReader(new FileReader("test.txt"));
      row = fileRead.readLine();
      for(int b = 0; b < 144; b++)
      {
         String string = row.substring(b, b+1);
         numArray[b] = Integer.parseInt(string);
      }
   }
   catch(Exception ex)
   {
      JOptionPane.showMessageDialog(null, ex);
   }
   finally
   {
      try
      {
         if(fileRead != null) fileRead.close();
      }
      catch(Exception e) { }
      fileRead = null;
   }
}

It's just good practice to make sure your streams get to close properly after you're done with them, no matter how temporary they are.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List