Hi, and happy new year.
I have this problem with my java learning.
I want to create a GUI application which will read files on a directory and display they're names in the JFrame title and in a label in the frame.
There are 2 classes: Start and ReadDirectory as below;
HTML Code:
//class Start
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
public class Start extends JFrame implements ActionListener {
private JPanel north, centre;
private JLabel label, label1;
private JTextField box;
private String f;
private JButton go;
private File folder;
private File[] listOfFiles;
public static void main( String[] args){
JFrame frame = new Start();
frame.setTitle("Start_JAVA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350,120);
frame.setVisible(true);
} //end main
public Start(){
centre = new JPanel();
label1 = new JLabel("Enter folder's name ");
box = new JTextField("",20);
go = new JButton("Go");
centre.add(label1); centre.add(box);centre.add(go);
go.addActionListener(this); // listen for button click
add(centre, BorderLayout.CENTER);
}//end constructor
public void actionPerformed(ActionEvent ev) {
String entry = box.getText();
folder = new File("C:\\Users\\Alti\\Desktop\\Altin\\be" + entry);
listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
f = listOfFiles[i].toString();
new ReadDirectory(f);
}//end if
}//end for
}//end actionPerformed
}//END_______________________________________
and the
HTML Code:
//class ReadDirectory
import java.awt.*;
import javax.swing.*;
public class ReadDirectory extends JPanel{
public ReadDirectory(String str) {
JFrame frame = new JFrame(Start.f);
frame.setSize(320, 170);
Container contentPane = frame.getContentPane();
contentPane.add(new ReadDirectory ());
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public ReadDirectory() {}
public void paint(Graphics g) {
super.paint(g);
g.setFont(new Font("Serif", Font.BOLD, 14));
g.drawString(Start.f,32,25);
}
}
My problem... I can see the file's name in the Frame's title, but the label is showing the last file's name???
No idea where the problem is. Any help will be appreciated.
Thank you.