Hi, I'm having trouble with my Java code. It's meant to read in from a csv file and update the elements on the GUI to reflect this e.g. as it reads in one line, images and text appear on the GUI reflecting the data.
Anyway, to get the GUI to work I needed to create another class and call the methods from there.
I can get the data (string) to display doing this, but I can't get the images (JLabels) to update.
The main class is:
public class GuiDisplay extends javax.swing.JFrame implements SensorListener
{
public ImageIcon[] imgList;
public String strLine;
public String imageDir;
public ImageIcon[] iconList;
public ImageIcon[] imgLarge;
public int dataCounter;
public String data;
/** Creates new form GuiDisplay */
public GuiDisplay()
{
initComponents();
initVariables();
System.out.println("InitComp called...");
SensorListenerRd SensorListener = new SensorListenerRd();
SensorListener.addListener(this);
Thread t = new Thread(SensorListener);
t.start();
}
@SuppressWarnings("unchecked")
/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
GuiDisplay guiDisplay = new GuiDisplay();
guiDisplay.setVisible(true);
guiDisplay.pack();
SimulatedData simulatedData = new SimulatedData(guiDisplay);
}
});
}
public ImageIcon[] loadImages(String imageDir, String[] imageFiles)
{
ImageIcon[] imageList = new ImageIcon[imageFiles.length];
for (int i = 0; i < imageFiles.length; i++)
{
imageList[i] = new ImageIcon(imageDir + imageFiles[i]);
}
return imageList;
}
public ImageIcon[] resizeIcons(ImageIcon[] imageList, int b, int l)
{
ImageIcon[] iconList = new ImageIcon[imageList.length];
for (int i = 0; i < imgList.length; i++)
{
iconList[i] = new ImageIcon(getSizedImage(imgList[i].getImage(), b, l));
}
return iconList;
}
public Image getSizedImage(Image srcImg, int b, int l)
{
BufferedImage resizedImg = new BufferedImage(b, l, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, b, l, null);
g2.dispose();
return resizedImg;
}
public void updateImg(JLabel picLabel, String string, ImageIcon imageIcon)
{
eventsTextArea.setText(string);
pic1Label.setIcon(imageIcon);
pic1Label.revalidate();
pic1Label.repaint();
//same for labels 2, 3 and 4
eventsTextArea.revalidate();
eventsTextArea.repaint();
}
private void updateIcons(JPanel panel, int dataCounter, String string, ImageIcon imageIcon)
{
JLabel dataItem = new JLabel();
dataItem.setText(dataCounter + ". " + string);
dataItem.setIcon(imageIcon);
dataItem.setVerticalTextPosition(JLabel.TOP);
dataItem.setHorizontalTextPosition(JLabel.CENTER);
dataItem.setBorder(BorderFactory.createLoweredBevelBorder());
panel.add(dataItem);
panel.revalidate();
panel.repaint();
}
public void sensorUpdate(SensorListenerRd sensorReceiver, int sensorID, String eventType, String eventTime)
{
try
{
String strFile = "C:\\directoryfile\\package\\images\\Data.csv";
BufferedReader br = new BufferedReader(new FileReader(strFile));
String Line = null;
int row = 0;
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
while ((strLine = br.readLine()) != null)
{
lineNumber++;
st = new StringTokenizer(strLine, ",");
while (st.hasMoreTokens())
{
wait(3000);
tokenNumber++;
System.out.println("[Sensor Event ID: " + sensorID + " Type: " + eventType
+ ", Token : ]" + st.nextToken());
eventsTextArea.append("[Sensor Event ID: " + sensorID + " Type: " + eventType + "]\n");
}
}
}
catch (Exception e)
{
System.out.println("Exception while reading csv file: " + e);
//The device ID would vary for each picture
if (eventType.compareToIgnoreCase("Alarm") == 0)
{
++dataCounter;
data = data + dataCounter + (" - Got Event" + ", EventTime = " + eventTime + ", SensorID = " + sensorID + ", EventType = " + eventType + "\n");
switch (sensorID)
{
case 35032: //2117, 2094, 35032
updateImg(pic4Label, "Opened fridge", imgLarge[0]);
updateIcons(guiDisplayPanel, dataCounter, "fridge", iconList[0]);
break;
case 2068: //2117
updateImg(pic1Label, "Took a cup", imgLarge[1]);
updateIcons(guiDisplayPanel, dataCounter, "cup", iconList[1]);
break;
// etc }
}
else
{
data = data + ("Got Event" + ", EventTime = " + eventTime + ", SensorID = " + sensorID + ", EventType = " + eventType + "\n");
}
eventsTextArea.setText(data);
}
}
private void getSimulatedData()
{
BufferedReader br = null;
try
{
String strFile = "C:\\directory\\package\\images\\TestData.csv";
br = new BufferedReader(new FileReader(strFile));
String Line = null;
int row = 0;
StringTokenizer st = null;
int lineNumber = 0;
int tokenNumber = 0;
eventsTextArea.setText("Your name");
while ((strLine = br.readLine()) != null)
{
System.out.println("Hello...");
lineNumber++;
System.out.println("Hi!");
st = new StringTokenizer(strLine, ",");
while (st.hasMoreTokens())
{
System.out.println("Hello.");
//wait(1000);
Thread.sleep(3000);
System.out.println("Hola");
tokenNumber++;
String sensorType = st.nextToken();
System.out.println("Token: " + sensorType);
eventsTextArea.setText("[Token: " + sensorType + "]\n");
int sensorID = Integer.parseInt(st.nextToken());
System.out.println("Token: " + sensorID);
eventsTextArea.append("[Token: " + sensorID + "]\n");
String eventType = st.nextToken();
System.out.println("Token: " + eventType);
eventsTextArea.append("[Token: " + eventType + "]\n");
String eventTime = st.nextToken();
System.out.println("Token: " + eventTime);
eventsTextArea.append("[Token: " + eventTime + "]\n");
String eventDate = st.nextToken();
System.out.println("Token: " + eventDate);
eventsTextArea.append("[Token: " + eventDate + "]\n");
if (eventType.compareToIgnoreCase("Alarm") == 0)
{
++dataCounter;
data = data + dataCounter + (" - Got Event" + ", EventTime = " + eventTime + ", SensorID = " + sensorID + ", EventType = " + eventType + "\n");
switch (sensorID)
{
case 35032: //2117, 2094, 35032
updateImg(pic4Label, "Opened fridge", imgLarge[0]);
updateIcons(guiDisplayPanel, dataCounter, "fridge", iconList[0]);
break;
case 2068: //2117
updateImg(pic1Label, "Took a cup", imgLarge[1]);
updateIcons(guiDisplayPanel, dataCounter, "cup", iconList[1]);
//etc
}
}
else
{
data = data + ("Got Event" + ", EventTime = " + eventTime + ", SensorID = " + sensorID + ", EventType = " + eventType + "\n");
}
eventsTextArea.setText(data);
System.out.println("Number of Lines read: " + dataCounter);
}
}
}
catch (InterruptedException ex)
{
Logger.getLogger(GuiDisplay.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(GuiDisplay.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void initVariables() {
data = ""; //store sensor information for display
dataCounter = 0; //counts number of sensor data
// insert image location here
imageDir = "C:\\directory\\package\\images\\";
String[] imageFiles = {"door_open.jpg", "door_closed.jpg", "coffee.jpg", "teabags.jpg",
"kettle.jpg", "cup.jpg", "fridge.jpg", "kettlecuppour.jpg",
"microwave.jpg", "milk.jpg", "oven.jpg", "sugar.jpg", "cupboard.jpg"
};
imgList = loadImages(imageDir, imageFiles);
iconList = resizeIcons(imgList, 56, 56);
imgLarge = new ImageIcon[imageFiles.length];
}
public void setEventsText(String string) {
eventsTextArea.setText(string);
}
The other class deals with the "simulatedData". The data gets displayed to the eventsTextArea fine, however when I try to create a method in here and call it from the other class, it tells me for instance on
case 2088: //2088, 24152, 2087
updateImg(pic2Label, "Took a cup", imgLarge[2]);
updateIcons(guiDisplayPanel, dataCounter, "kettle", iconList [2]);
that pic2Label (the name of my picture label on the GUI) and guiDisplayPanel (the name of the JPanel) can't be found.
Any help on this would be greatly appreciated.
Thank you.