0

I m trying to create JLabels in a loop, but it seems that something is wrong. It don' t appear any label that is in the loop.What am i missing? Do i have to set another panel or i can use the same that i have?

 public class searchFrame extends JFrame {

JLabel bsearch= new JLabel ("Search by name: ");
JTextField txsearch = new JTextField(25);
JButton bgo = new JButton("Go");
JLabel allsearch= new JLabel ("All files");
JLabel result=new JLabel ();
JPanel panel = new JPanel();

searchFrame(){
super("Search frame");
setSize(260,400);
setLocation(500,280);
panel.setLayout (null); 

bsearch.setBounds(10,40,100,20);
txsearch.setBounds(120,40,70,20);
bgo.setBounds(195,40,55,20);
allsearch.setBounds(10,70,80,20);
result.setBounds(10,100,80,20);

panel.add(bsearch);
panel.add(txsearch);
panel.add(bgo);
panel.add(allsearch);
panel.add(result);

getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionsearch();
mouseactionlabel();
}

public void actionsearch(){
bgo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
    String path1="C:/Documents and Settings/giannis/Desktop/Server/";
    String fileName = "";
            try{
            fileName = txsearch.getText();
            File directory = new File(path1);
                        File[] listOfFiles = directory.listFiles();
                        int c=0;
                        for (File file : listOfFiles ) {
                                String name = file.getName();

                                    if (name.equals(fileName)) {
                                    result.setText(fileName);
                                    long size=file.length();
                                    long modified=file.lastModified();                                  
                                     c=1;
                                        System.out.println("File found..");
                                        System.out.println("File size"+size);
                                        System.out.println("File modified"+modified);
                                    }

                                    }if (c!=1){
                                        System.out.println("File not found..");
                                        result.setText("Not found");
                                    }}
        catch(Exception e){
            e.printStackTrace();
        }                              

}
});
}

void mouseactionlabel(){
allsearch.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent arg0) {
    String path1="C:/Documents and Settings/giannis/Desktop/Server/";

            try{

                File directory = new File(path1);
                File[] listOfFiles = directory.listFiles();
                int c1 = directory.listFiles().length;
                System.out.println(c1);
                JLabel[] labels = new JLabel[c1];

                for (File file : listOfFiles ) {

                for (int i = 0 ;i < c1; i++)
                {     
                    for(int v = 120; v <= 300; v += 20){
                        String name = file.getName();
                        labels[i] = new JLabel();
                        labels[i].setBounds(10,v,80,20);
                        labels[i].setText(name);                        
                        panel.add(labels[i]);


                }}
                                    }

                                    }
        catch(Exception e){
            e.printStackTrace();
        }
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
});
}
}
question_1
  • 102
  • 1
  • 10
  • 1
    You haven't initialized the elements in the array, so they're all `null`, giving you the exception. This is a common mistake I see when beginners first start using arrays of objects. – gparyani Jul 28 '13 at 20:03
  • 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 29 '13 at 02:48

1 Answers1

5

you are getting NullPointerException because you haven't initialized the JLabel before calling setBounds method on it. So, Change this :

String name = file.getName();
labels[i].setBounds(10,v,80,20);

to:

String name = file.getName();
labels[i]= new JLabel();
labels[i].setBounds(10,v,80,20);

And to get rid of ArrayIndexOutOfBoundException change:

for (int i = 1 ;i <= c1; i++)

to:

for (int i = 0 ;i < c1; i++)

And, as a side note. Never use setBounds for positioning the components in swing. Use the appropriate layouts instead. There are lot of Layouts available with swing. Look at A Visual Guide to Layout Managers to know how to use these layouts.

Mac
  • 1,711
  • 3
  • 12
  • 26
  • I ve change it as you said, but now i receive java.lang.ArrayIndexOutOfBoundsException:. Why? The c1=2 – question_1 Jul 28 '13 at 20:13
  • You are getting this exception because you are trying to access the element at that index which is not existing in the array. Any array starts with index `0` and ends with index `lengh - 1`. Look at my update. – Mac Jul 28 '13 at 20:16
  • You are right! Is it because of setBounds that i don't get the labels in my gui? – question_1 Jul 28 '13 at 20:22
  • It might be..Have you added the `panel` to the underlying container? And what layout have you set for the `panel` ? – Mac Jul 28 '13 at 20:23
  • if i understand what you say, i used this JPanel panel = new JPanel(); panel.setLayout (null); getContentPane().add(panel); I have to say that i use this panel to present and other jlabels,jtextfields and buttons. Did i get right what you asked? – question_1 Jul 28 '13 at 20:41
  • Ok..But Have you added this panel to the `JFrame` that you are using to display? – Mac Jul 28 '13 at 20:42
  • 1
    Then remove setbounds and dont change the layout of panel to `null` if you have done that.!! – Mac Jul 28 '13 at 20:58
  • I set null in the start of the program and i tried to remove setBounds, but nothing. If i don t have setBounds then theoretically one label in each loop will appear, right? – question_1 Jul 28 '13 at 21:05
  • 1
    **Don't set the layout to null. Remove the following line from your code:** `panel.setLaout(null)`. If you *don't* set the layout `null` and then dont have setBounds then yes label in each loop will appear. – Mac Jul 28 '13 at 21:08
  • *"There are lot of Layouts available with swing."* Nitpick: Most of the layouts we use are in the AWT package. +1 for mentioning layouts at all. – Andrew Thompson Jul 29 '13 at 02:50