Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Weather Panel Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

 

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.*;
import java.util.Properties;
import java.util.Collection;
import java.util.Iterator;

public class WeatherPanel {
    private JEditorPane htmlPane;

    public WeatherPanel() {
        htmlPane = createHtmlPanel();
    }

    public void displayWeather(String html, Collection weather){
        String result = html;
        try {
            VelocityContext context = createContext(weather);
            result = processString(context, html);
        } catch (Exception e){
            e.printStackTrace();
        }
        htmlPane.setText(result);
    }

    public void displayWeatherByFile(String fileName, Collection weather){
        displayWeather(readFile(fileName), weather);
    }

    private JEditorPane createHtmlPanel() {
        JEditorPane editorPane = new JEditorPane();
        HTMLEditorKit editorKit = new HTMLEditorKit();
        editorKit.install(editorPane);
        editorPane.setEditorKit(editorKit);
        editorPane.setEditable(false);
        return editorPane;
    }

    public Component getComponent() {
        return new JScrollPane(htmlPane);
    }

    private String readFile(String fileName) {
        StringBuffer htmlBuffer = new StringBuffer();

        try {
            InputStream inputStream = WeatherPanel.class.getResourceAsStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            while (true){
                String line = reader.readLine();
                if (line != null){
                    htmlBuffer.append(line);
                } else {
                    break;
                }
            }
        } catch (IOException iox){
            iox.printStackTrace();
        }
        return htmlBuffer.toString();
    }

    private String processString(VelocityContext context, String htmlText) throws Exception {
        StringWriter writer = new StringWriter();
        Properties properties = new Properties();
        Velocity.init(properties);
        Velocity.evaluate(context,
                writer,
                null,
                htmlText);
        return writer.getBuffer().toString();
    }

    private VelocityContext createContext(Collection weatherCollection) {
        VelocityContext context = new VelocityContext();
        int index = 1;

        for (Iterator iterator = weatherCollection.iterator(); iterator.hasNext();) {
            Weather weather = (Weather) iterator.next();
            String day = "DAY" + index;
            context.put(day, weather.getDay());
            context.put(day + "_TEMP", weather.getTemperature());
            context.put(day + "_HUMIDITY", weather.getHumidity());
            context.put(day + "_PRESSURE", weather.getPressure());
            index++;
        }

        return context;
    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */

 
 

Code:


import com.jonathansimon.swing.hacks.velocityhtml.WeatherPanel;

import javax.swing.*;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Jonathan Simon
 * Date: Mar 3, 2005
 * Time: 3:41:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class WeatherPanelSimulator {

    public WeatherPanelSimulator() {
        JFrame frame = new JFrame("Weather Panel Simulator");
        frame.setBounds(200,200, 500, 350);

        Weather weather1 = new Weather(new BigDecimal("82"), new BigDecimal("40.0"), new BigDecimal(1), "Monday");
        Weather weather2 = new Weather(new BigDecimal("75"), new BigDecimal("65.0"), new BigDecimal(1), "Tuesday");
        Weather weather3 = new Weather(new BigDecimal("85"), new BigDecimal("43.0"), new BigDecimal(1), "Wednessday");

        ArrayList list = new ArrayList();
        list.add(weather1);
        list.add(weather2);
        list.add(weather3);


        WeatherPanel weatherPanel = new WeatherPanel();
        weatherPanel.displayWeatherByFile("html/today.html", list);

        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(weatherPanel.getComponent(), BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();

    }


    public static void main(String[] args) {
        new WeatherPanelSimulator();
    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */


Code:


import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
import java.awt.*;
import java.io.*;
import java.util.Properties;
import java.math.BigDecimal;

public class VelocityHtmlPanel {
    private JEditorPane htmlPane;

    public VelocityHtmlPanel() {
        htmlPane = createHtmlPanel();

        String htmlText = readFile("html/today.html");
        String htmlTextPostTemplate = runTemplateAgainstHtml(htmlText);

        htmlPane.setText(htmlTextPostTemplate);


    }

    private JEditorPane createHtmlPanel() {
        JEditorPane editorPane = new JEditorPane();
        HTMLEditorKit editorKit = new HTMLEditorKit();
        editorKit.install(editorPane);
        editorPane.setEditorKit(editorKit);
        editorPane.setEditable(false);
        return editorPane;
    }

    private String runTemplateAgainstHtml(String htmlText) {
        String result = htmlText;
        try {

            Weather weather = new Weather(new BigDecimal("86.9"), new BigDecimal("68"), new BigDecimal("5"));
            VelocityContext context = createContext(weather);
            result = processString(context, htmlText);
        } catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }

    public Component getComponent() {
        return new JScrollPane(htmlPane);
    }

    private String readFile(String fileName) {
        StringBuffer htmlBuffer = new StringBuffer();

        try {
            InputStream inputStream = VelocityHtmlPanel.class.getResourceAsStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            while (true){
                String line = reader.readLine();
                if (line != null){
                    htmlBuffer.append(line);
                } else {
                    break;
                }
            }
        } catch (IOException iox){
            iox.printStackTrace();
        }
        return htmlBuffer.toString();
    }

    private String processString(VelocityContext context, String htmlText) throws Exception {
        StringWriter writer = new StringWriter();
        Properties properties = new Properties();
        properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
        Velocity.init(properties);
        Velocity.evaluate(context,
                writer,
                "LOG", // used for logging
                htmlText);
        return writer.getBuffer().toString();
    }

    private VelocityContext createContext(Weather weather) {
        VelocityContext context = new VelocityContext();
        context.put("TEMP", weather.getTemperature() + " F");
        context.put("HUMIDITY", weather.getHumidity() + " %");
        context.put("PRESSURE", "10" + " bars");
        return context;
    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */



Code:

<html>
    <head>
        <title>Flying Saucer</title>
    </head>
    <body>
        <div class="section">
           
  <div class="top">
    <div>
      <h2> Big Jon's Weather Fiasco</h2>
      <table width="50%" border="1">
        <tr>
          <td width="33%">Temperature</td>
          <td width="33%">Humidity </td>
          <td width="33%">Pressure</td>
        </tr>
        <tr>
          <td rowspan="2"><p>${TEMP}</p>
            </td>
          <td>${HUMIDITY}</td>
          <td><p> </p>
            <p>${PRESSURE}</p>
            <p> </p></td>
        </tr>
      </table>
      <p> </p>
    </div>
  </div>
  <div class="content">
<p></p> </div>
        </div>
    </body>
</html>



Code:



import com.jonathansimon.swing.hacks.velocityhtml.VelocityHtmlPanel;

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jonathan Simon
 * Date: Mar 3, 2005
 * Time: 3:41:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class VelocityHtmlPanelSimulator {

    public VelocityHtmlPanelSimulator() {
        JFrame frame = new JFrame("Velocity HTML Panel Simulator");
        frame.setBounds(200,200, 500, 350);

        VelocityHtmlPanel velocityHtmlPanel = new VelocityHtmlPanel();

        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(velocityHtmlPanel.getComponent(), BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.show();

    }


    public static void main(String[] args) {
        new VelocityHtmlPanelSimulator();
    }

}

/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy