My problem is i need to read the image from a file and then display it onto the gui by getting it from another class. My teacher said something about pathname, but i don't know. Please check my codes below and answer me. I am new to java and its quite difficult for me to understand, very urgent.
Thank you in advance
These are my codes:
---The gui to display the image
Code:public class DisplayCD extends javax.swing.JFrame {
/** Creates new form DisplayCD */
@SuppressWarnings("static-access")
public DisplayCD() {
try {
initComponents();
ReadFromFile display = new ReadFromFile();
display.readFile();
image.setIcon(display.passedCD[0].getImage());
Title.setText(display.passedCD[currentCD].getTitle());
singer.setText(display.passedCD[currentCD].getSinger());
} catch (IOException ex) {
Logger.getLogger(DisplayCD.class.getName()).log(Level.SEVERE, null, ex);
}
}
--- The class which gets and sets the arrays from readfile
-----Reading from file codeCode:public class CD {
Icon image;
String title;
String singer;
double price;
public CD (Icon image,String title, String singer, double price,){
this.image=image;
this.title=title;
this.singer = singer;
}
public CD (){
}
public void createTrackArray(int size){
track=new String[size];
time=new String[size];
}
public Icon getImage() {
return image;
}
public void setImage(Icon image) {
this.image = image;
}
public void setTitle (String title){
this.title = title;
}
public String getTitle (){
return title;
}
}
Moderator Edit: Code tags addedCode:public class ReadFromFile {
String line = "";
public static CD[] passedCD;
int remain;
int trackSize = 0;
// public static void main (String[]args) throws IOException{
public void readFile() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("CD.txt"));
//read 1st line to indicate number of cd
int count = Integer.parseInt(br.readLine());
CD CDArray[] = new CD[count];
String tempArray[] = new String[count];
// create array to hold CD
for (int i = 0; i < count; i++) {
tempArray[i] = br.readLine();
// System.out.println("cd info---------------->" + tempArray[i]);
}
for (int j = 0; j < count; j++) {
CD temp = new CD();
String CDInfo = tempArray[j];
StringTokenizer st = new StringTokenizer(CDInfo, ",");
while (st.hasMoreTokens()) {
String a = st.nextToken();
temp.setTitle(a);
// System.out.println("CD title----->" + temp.getTitle());
temp.setSinger(st.nextToken());
// System.out.println("CD singer---->" + temp.getSinger());
a = st.nextToken();
double d = Double.parseDouble(a);
temp.setPrice(d);
// System.out.println("CD price------>" + temp.getPrice());
temp.setCategory(st.nextToken());
// System.out.println("CD cat-------->" + temp.getCategory());
temp.setDiscount(st.nextToken());
// System.out.println("CD discount---->" + temp.getDiscont());
a = st.nextToken();
Icon i = new ImageIcon(a);
temp.setImage(i);
trackSize = st.countTokens() / 2;
temp.setNumSong(trackSize);
// System.out.println("=============> trackSize" + trackSize);
temp.createTrackArray(trackSize);
for (int k = 0; k < trackSize; k++) {
String track = st.nextToken();
// System.out.println("Next token----->" + track);
temp.setTrackEach(k, track);
String time = st.nextToken();
// System.out.println("Next time----->" + time);
temp.setTimeEach(k, time);
}
}// readin before the tracks
CDArray[j] = temp;
remain = trackSize;
}
passedCD = CDArray;
// System.out.println("!!!" + passedCD[0].title);
}
public static CD[] passCD(CD[] array) {
return array;
}
}

