0

I'm building a very small Java Applet example. Normally, I program in C# instead of Java. Don't blame me for not using a layoutmanager, I know things could be more efficient. My problem is that I can't display the received XML values which are saved in the ArrayList called "weerAttribuutValueLabelList" on a label or textfield. When I inspect the ArrayList all the values received from the XML file are present.

It must be a small stupid thing which differs from C# or something like that I think. I hope someone can help me out, I already tried a lot to find the cause, but now I'm running out of ideas. Thank you very much.

The codeblock:

import java.awt.*; 
import java.applet.*; 
import java.awt.event.*; 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.net.*;
import java.io.*;
import java.util.ArrayList;

public class ReadXml extends Applet implements ActionListener 
{
    NodeList xmlValueList;
    ArrayList<Label> weerAttribuutLabelList;
    ArrayList<Label> weerAttribuutValueLabelList;

    int weerAttribuutLabelMargin = 20;
    int weerAttribuutValueLabelMargin = 20;

public void init()
{
    // Location for every generated label
    weerAttribuutLabelList = new ArrayList <Label>();
    weerAttribuutValueLabelList = new ArrayList <Label>();

    // Generate the static labels with defined text label at first
    staticLabel();

    // Download the xml, put the xml values in a label and save them in the ArrayList
    readXmlFile();

    labelAligning();    
}

// Every static label gets a defined text label from the array
public void staticLabel()
{
    String[] regentHetInXmlNames = {"Datum:", "Tijd:", "Temperatuur (°C):", "Trend (/uur):", "Max. temperatuur:", "Min. temperatuur:", "Gevoelstemperatuur:", "Min. gevoelstemp.:", "Dauwpunt:", "Trend (/uur):", "Max. dauwpunt:", "Min. dauwpunt:", "Luchtvochtigheid:", "Trend (/uur):", "Max. luchtvochtigheid:", "Min. luchtvochtigheid:", "Neerslag:", "Luchtdruk:", "Trend (/uur):", "Max. luchtdruk:", "Min. luchtdruk", "Windsnelheid:", "Windsnelheid (10 min):", "Max. windstoot:", "Windrichting:", "UV:", "Max. UV:"};

    for(String name : regentHetInXmlNames)
    {
        weerAttribuutLabelList.add(new Label(name));
    }
}

// Read xml
public void readXmlFile()
{
    try 
    {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document regenthetinXml = dBuilder.parse(new URL("http://regenthet.in/data/regenthetin.xml").openStream());

        regenthetinXml.getDocumentElement().normalize();

        String[] regentHetInXmlNodes = {"stationDate", "stationTime", "outsideTemp", "tempChangeLastHour", "hiOutsideTemp", "lowOutsideTemp", "windchill", "lowWindchill", "outsideDewPt", "dewChangeLastHour", "hiDewpoint", "lowDewpoint", "outsideHumidity", "humChangeLastHour", "hiHumidity", "lowHumidity", "dailyRain", "pressure", "pressChangeLastHour", "hiPressure", "lowPressure", "windSpeed", "wind10Avg", "hiWindSpeed", "windDirection", "uv", "hiUV"};

        xmlValueList = regenthetinXml.getElementsByTagName("station_almelo");
        Node nNode = xmlValueList.item(0);

        Element eElement = (Element) nNode;
        for(int i = 0; i <= 26; i++)
        {
            weerAttribuutValueLabelList.add(new Label(eElement.getElementsByTagName(regentHetInXmlNodes[i]).item(0).getTextContent()));
            //System.out.println(eElement.getElementsByTagName(regentHetInXmlNodes[i]).item(0).getTextContent());
        }         
    }

    catch(Exception e)
    {}
}

public void labelAligning()
{
    // Visual default aspects
    setLayout(null);
    setBackground(Color.lightGray);

    for(Label item : weerAttribuutLabelList)
    {
        item.setBounds(20,weerAttribuutLabelMargin,150,20);
        add(item);
        weerAttribuutLabelMargin = weerAttribuutLabelMargin + 20;
        //System.out.println(item);
    }

    for(Label item : weerAttribuutValueLabelList)
    {
        item.setBounds(250,weerAttribuutValueLabelMargin,150,20);
        add(item);
        weerAttribuutValueLabelMargin = weerAttribuutValueLabelMargin + 20;
        System.out.println(item);
    }
}

public void actionPerformed(ActionEvent event)
{          
}
}

Image, for clarifying

Wietse-0803
  • 125
  • 1
  • 4
  • 1
    You ignoring the exceptions generated by the reading process. The likely issue is your applet doesn't have permissions to access other domains – MadProgrammer Jan 25 '16 at 23:13
  • Once you've fixed that 'ignoring information' problem pointed out by @MadProgrammer (& fixed the lack of layouts, so the applet has a good chance of working on the next machine), be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. – Andrew Thompson Jan 28 '16 at 04:59
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jan 28 '16 at 05:00

1 Answers1

0

Your desired layout calls for two columns, so at least you could use a GridLayout(26,2) or similar and add the labels. See what happens and then you may use other layouts as you learn.

gpasch
  • 2,672
  • 3
  • 10
  • 12
  • I just installed Neatbeans and it's working properly now without any change, I tried in BlueJ before (stupid me), that's is asking for trouble ofcourse. Thanks for the lay-out tip by the way. – Wietse-0803 Jan 25 '16 at 23:21