受http://www.oreillynet.com/onjava/blog/2007/05/javafx_first_steps_hello_onjav_1.html这篇文章的启发,加上想学习一下JavaFX,就在上面那篇文章的基础上,做了一个简易的RSS阅读器,其界面类似于Adobe Flex 2的一个demo:进入http://try.flex.org/index.cfm,找到Blog Reader的demo。
读取RSS feed并没有什么问题,使用rome:
java 代码
- package rssreader;
- import com.sun.syndication.feed.synd.SyndEntry;
- import com.sun.syndication.feed.synd.SyndFeed;
- import com.sun.syndication.io.FeedException;
- import com.sun.syndication.io.SyndFeedInput;
- import com.sun.syndication.io.XmlReader;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class RssReader {
- private URL url;
- private Integer titleLength = 50;
- public RssReader() {
- }
- public SyndEntry[] load(String urlString) throws IllegalArgumentException, FeedException, IOException {
- if (urlString != null && urlString != "") {
- try {
- url = new URL(urlString);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- return null;
- }
- }
- if (url == null) {
- return null;
- }
- SyndFeedInput input = new SyndFeedInput();
- SyndFeed feed = input.build(new XmlReader(url));
- SyndEntry[] entries = (SyndEntry[]) feed.getEntries().toArray(new SyndEntry[0]);
- for( SyndEntry entry : entries ) {
- entry.setTitle( entry.getTitle().length() > titleLength ? entry.getTitle().substring(0, titleLength - 3 ) + "..." : entry.getTitle() );
- }
- return entries;
- }
- }
使用JavaFX构建UI:
JavaFX代码
- package rssreader;
- import javafx.ui.*;
- import javafx.ui.canvas.*;
- import javax.swing.JComponent;
- import com.sun.syndication.feed.synd.SyndEntry;
- var reader:RssReader = READER;
- class RssReaderModel {
- attribute rssUrl: String;
- attribute rssEntries: SyndEntry*;
- attribute rssContent: String?;
- attribute rssSelectedIndex: Integer;
- }
- var model = RssReaderModel {
- rssUrl: "http://woodstudio.javaeye.com/blog/rss_blog/alexcheng",
- };
- var panel = GroupPanel {
- var row1 = Row {}
- var row2 = Row {}
- var col = Column {}
- rows: [row1, row2]
- columns: col
- content:
- [
- GroupPanel {
- row: row1
- column: col
- var row = Row {}
- var labelCol = new Column
- var urlCol = new Column
- var butCol = new Column
- rows: row
- columns: [labelCol, urlCol, butCol]
- content:
- [
- SimpleLabel {
- row: row
- column: labelCol
- text: "RSS URL:"
- },
- TextField {
- row: row
- column: urlCol
- columns: 60
- value: bind model.rssUrl
- },
- Button {
- row: row
- column: butCol
- text: "Get Entries"
- action: operation() {
- model.rssEntries = reader.load(model.rssUrl);
- }
- }
- ]
- },
- SplitPane {
- row: row2
- column: col
- orientation: HORIZONTAL
- content:
- [
- SplitView {
- weight: 0.3
- content:
- Table {
- columns:
- [
- TableColumn {
- text: "Title"
- },
- TableColumn {
- text: "Date"
- },
- ]
- cells : bind foreach (entry in model.rssEntries)
- [
- TableCell {
- text: bind entry.title
- },
- TableCell {
- text: bind entry.publishedDate.toLocaleString()
- }
- ]
- selection: bind model.rssSelectedIndex
- }
- },
- SplitView {
- weight: 0.7
- content:
- EditorPane {
- opaque: true
- contentType: HTML
- editable: false
- text: bind model.rssContent
- }
- }
- ]
- }
- ]
- };
- trigger on (RssReaderModel.rssSelectedIndex = value) {
- var desc = rssEntries[value].description.value;
- rssContent = " {desc} ";
- }
- MY_CONTAINER:JComponent.add(panel.getComponent());
接着就是使用JSR223的scripting framework来执行:
java 代码
- package rssreader;
- import javax.script.Bindings;
- import javax.script.ScriptContext;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import javax.script.SimpleScriptContext;
- import javax.swing.JFrame;
- public class Main {
- /**
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- RssReader reader = new RssReader();
- JFrame frame = new JFrame(
- "RSS Reader implemented using JavaFX by alexcheng");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(800, 600);
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- ScriptEngineManager manager = new ScriptEngineManager(loader);
- ScriptEngine engine = manager.getEngineByExtension("fx");
- Bindings bindings = engine.createBindings();
- bindings.put("READER:rssreader.RssReader", reader);
- bindings.put("MY_CONTAINER:javax.swing.JComponent", frame
- .getContentPane());
- ScriptContext context = new SimpleScriptContext();
- context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
- context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
- engine.setContext(context);
- String script = "import rssreader.FxMain;";
- engine.eval(script);
- frame.setVisible(true);
- }
- }
最后的界面如下:
javafxrssreader.JPG
描述:
JavaFX RSS Reader
文件大小:
74 KB
看过的:
文件被下载或查看 28 次
文章来源: http://x-spirit.spaces.live.com/Blog/cns!CC0B04AE126337C0!274.entry