<< From Intellij to Eclipse (because of Compiz Swing bugs) | Home | Benchmarks Mean Squat >>

JavaFX vs Plain Old Java

This week on JavaRanch we've had the honor of having Jim Weaver promoting his JavaFX Book. Jim's a great guy but I'm not sold on JavaFX just yet. While reading up on it and reading responses to questions I get this horrible feeling in my gut reminisce of another slew of (empty) promises from Sun regarding a web technology who's name I won't mention but its initials are JSF.

So one of Jim's common responses to "why JavaFX" is "The simple, elegant, declarative scripting that (IMO) makes it as easy to create a JavaFX application without a GUI designer as it is to write a Swing/2D app with a GUI designer."

So how simple really? I took the liberty of writing Jim's FreeBase browser in plain old school Java. Note that to get the JSON into something I could deal with I used the Stringtree JSON API.

package com.freebase; import org.stringtree.json.JSONReader; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; public class FreeBaseApp { private JFrame frame; private DefaultTableModel tableModel; private JTextField textField; private JButton button; public FreeBaseApp() { initUI(); } private void initUI() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); tableModel = new DefaultTableModel(); tableModel.addColumn("Albums"); JTable table = new JTable(tableModel); JScrollPane scroll = new JScrollPane(table); frame.add(scroll, BorderLayout.CENTER); final JPanel buttonPanel = new JPanel(new FlowLayout()); textField = new JTextField(20); buttonPanel.add(textField); button = new JButton("Find Albums"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { findAlbums(); } catch (URISyntaxException ex) { JOptionPane.showMessageDialog(frame, "Error Finding Albums", "Error", JOptionPane.ERROR_MESSAGE); } catch (MalformedURLException ex) { JOptionPane.showMessageDialog(frame, "Error Finding Albums", "Error", JOptionPane.ERROR_MESSAGE); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, "Error Finding Albums", "Error", JOptionPane.ERROR_MESSAGE); } } }); buttonPanel.add(button); frame.add(buttonPanel, BorderLayout.SOUTH); frame.setSize(480, 500); frame.setVisible(true); } private void findAlbums() throws URISyntaxException, IOException { String query = "{\"albums\":{\"query\":{\"type\":\"/music/artist\", \"id\":\"/topic/en/" + textField.getText() + "\",\"album\":[]}}}"; URI uri = new URI("http", "www.freebase.com", "/api/service/mqlread", "queries=" + query, null); URL url = uri.toURL(); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); StringBuilder json = new StringBuilder(); String line; while ((line = in.readLine()) != null) { json.append(line); } JSONReader reader = new JSONReader(); Object obj = reader.read(json.toString()); HashMap results = (HashMap) obj; if (results.get("status").toString().equals("200 OK")) { HashMap album = (HashMap) results.get("albums"); HashMap result = (HashMap) album.get("result"); if (album.get("result") == null) { JOptionPane.showMessageDialog(frame, "Unable to find artist", "Error", JOptionPane.ERROR_MESSAGE); } else { java.util.List<String> albums = (ArrayList<String>) result.get("album"); for (String a : albums) { tableModel.addRow(new Object[] { a }); } frame.setTitle("Albums for " + textField.getText()); } } else { JOptionPane.showMessageDialog(frame, "Query Error", "Error", JOptionPane.ERROR_MESSAGE); } } public static void main(String[] args) { new FreeBaseApp(); } }

I tried to duplicate it as exact as possible. I'll have to conceed this one to JavaFX. Its code is much simpler than this. Time and more interesting applications will tell if JavaFX is going to be able to really compete with the big guns that seem to already be well established while JavaFX is still trying to be finalized.


Re: JavaFX vs Plain Old Java

Thanks for the great experience of interacting with the JavaRanch family and community by attempting to answer their JavaFX questions. And thanks for this "shootout" post -- it was fun!

Add a comment Send a TrackBack