Posts

TreeEditor: editable tree GUI component

Image
TreeEditor is a Java GUI component for editing tree structure.  It is a JScrollPane (as of TreeEditor ver. 0.1.0) derived and you can directly embed the component in your app.  I would suggest using JSplitPane . First and foremost, load the TreeEditor-0.1.5 library (if you are using Maven) as follows. < dependency > < groupId > com.github.easai.utils </ groupId > < artifactId > TreeEditor </ artifactId > < version > 0.1.6 </ version > </ dependency > Instantiate TreeEditor with a file name (or an empty string).    Initialize it by calling TreeEditor#init() .  Here I put it in a JSplitPane  and the TreeEditor will be resizable as the pane resizes. TreeEditor treeEditor = new TreeEditor( "" ); JTextArea menuCode = new JTextArea(); JTextArea listenerCode = new JTextArea(); public void init () { treeEditor. init ();...

Reading RSS in Java

In the previous article, I talked about XML.  It is a hierarchical structure of which each node is represented by <tag> enclosed tags.  The closing tag can either be </tag> or <tag /> format if there is no tags in it. RSS is a type of XML format that consists of pre-defined tags.  Here is how you read RSS feeds.  The structure is simple: the top node is "channel" and the articles are under "item" nodes.  Each "item" node contains nodes such as "title", "description", "link", and so on. The RSS reading code therefore is the same as in the way you read XML files.  I created a library called com.github.easai.text that can simplify the process pretty much.  Using the library, the procedure is abstracted and encapsulated.  So load:   < dependency > < groupId > com.github.easai </ groupId > < artifactId > text </ artifactId > < v...

The use of recursive calls

The term "recursive" means in computer science a function call that calls itself, in a way such that you can traverse a tree.  A tree contains nodes and leaves.  You would process the leaves and call the function again for nodes recursively. Consider traversing a directory structure.   File#list() does not list up all the subdirectories.  It is a matter of writing a function that calls itself, i.e., process the files and call the same function for directories. This procedure can be generalized.  Instead of writing the recursive call, here I wrote a class called RecurseDir .  This is an abstract class that calls on the specified file directory recursively. import java.io.File ; public abstract class RecurseDir { /** * Executed on each file. * * @param fileName */ public abstract void executeOnFile ( String fileName ); /** * Executed on each directory. * * @param fileName */ publi...

Scheduling jobs with Quartz

Quartz is a Java library that can schdule tasks (using Job derived class).  Let me show how you would use the scheduler.  First, import the library. < dependency > < groupId > org.quartz-scheduler </ groupId > < artifactId > quartz </ artifactId > < version > 2.3.0 </ version > </ dependency > < dependency > < groupId > org.quartz-scheduler </ groupId > < artifactId > quartz-jobs </ artifactId > < version > 2.3.0 </ version > </ dependency > The task that the scheduler runs must be a Job class derivative. import org.quartz.Job ; import org.quartz.JobExecutionContext ; import org.quartz.JobExecutionException ; public class NewsJob implements Job { @Override public void execute ( JobExecutionContext context ) throws JobExecutionException {...

Reading JSON with Java

JSON is a "fat-free alternative" of XML format, or the structured dataset format.  Here is a Java code that reads JSON data supplied by a web service.  Replace the URL and perhaps the API key if the site requires it, and the app will parse the JSON response. First of all, edit your POM (pom.xml). < dependency > < groupId > com.github.cliftonlabs </ groupId > < artifactId > json-simple </ artifactId > < version > 2.1.2 </ version > </ dependency > Here is how you can parse the JSON. import java.io.BufferedReader; import java.io.InputStreamReader; import java.net. URL ; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @SuppressWarnings( "deprecation" ) public class NewsApp { public static void main( String args[]) { JSONParser parser = new JSONParser(); Strin...

Parsing XML file with DOM

There are ways in Java how you can read or write  XML files.  Here is a way of reading and writing a XML file using DOM. The function readXML() reads the specified file.  It does all the preliminary works for reading an XML file.  It creates DocumentBuilderFactory , DocumentBuilder , and Document .  The XML file will be read by calling DocumentBuilder#parse(). The parseXMLTree() function recursively parses the DOM tree.  It outputs the text contents of each <value> tag of the node.  A word of caution: it is not Node#getNodeValue() that retrieves the text content of the node.  Call Node#getTextContent() instead. The function writeXML() does all the work for writing out the XML.  It creates TransformerFactor , Transformer , and DOMSource .  The StreamResult object creates the stream for the specified file.  Finally, call Transformer#transform() and it will write out the XML. import javax.xml.parsers.DocumentBuilder;...

Command line options with command-cli

How do you use command line arguments in Java?  The Java SDK does not come with library that can parse the arguments.  Parsing each argument is a tedious work, however. The common-cli does the all the work for you.  The library can parse the input for short and long versions of the command line input.  The "-f" option should be the same with "--trefile" , that is.  The options can be optional or mandatory.  If the required option is not there, it will print out the usage as specified.  Nice, is it?  It only is a matter of editing the pom.xml .  Here you go:   < dependency > < groupId > commons-cli </ groupId > < artifactId > commons-cli </ artifactId > < version > 1.4 </ version > </ dependency > As for parsing the input arguments, set the Option and parse the command line with CommandLineParser with the option settings.  If any wrong option is set, the usa...