Posts

TreeEditor: Customizing Drag and Drop

JTree is a Java GUI component for tree structure.  The tree structure is represented by nodes ( DefaultMutableTreeNode ).  The path or the placement of the node in the tree is represented by TreePath . DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); Use TransferHandler for drag and drop.  Supposing a class derived from TransferHandler called MenuTransferHandler handles drag and drop, set the TransferHandler as follows: MenuTransferHandler handler = new MenuTransferHandler(); treeEditor . tree .setTransferHandler(handler); The copied new node should be generated at TransferHandler#createTransferable() .  Here shows the function copy() that may come in handy in writing createTransferable() .   copy() should be called recursively. private DefaultMutableTreeNode copy (TreeNode node) { DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(node); if (!node.isLea...

JPopupMenu

Setting up a right click menu, or popup menu in Java is a tricky but simple procedure.  You would instantiate JPopupMenu, set JMenuItem, attach an ActionListener, set MouseListener.  The beauty of Java GUI structure is in that the listener can be set anywhere.  Any arbitrary component can implement the listener.  A JFrame can have a JPopupMenu and any component can set the MouseListener that set the popup.  And then any component can process the selected JMenuItem. Supposing say a JFrame has a JTable and a JTextArea.  You would want different JPopupMenu.  Each component can possess the JMenuPopup of its own, and the menu can invoke the function in JFrame and not only of JTable nor JTextArea. Which component should have the JPopupMenu (usually the sub-component but can be anywhere) determines what should implements the MouseListener.  The JMenuItem in the JPopupMenu should be 'listened' by an ActionListener that should process the demand. Her...

Quasi Abstraction of the JMenu system

Image
Setting up the menu for JFrame is a matter of creating JMenuBar, JMenu, JMenuItem, add()-ing the components and setting ActionListener for JMenuItem.  The each menu item must be associated with the menu, and the menu must be associated with the menubar.  The ActionListener must be set for each menu item, and the ActionEvent must be processed appropriately. This is a process that can be automated.  It would be far more easier if the menu item can be processed in some managed way.  There are ways of thinking on this: for smaller system, it would be more intuitive if the action is defined in the proximity of the menu item being declared.  In a larger system, it gets unmanageable unless all the accesses are not processed in a centralized manner. →  JavaMenuMenu.java Here I show a way that this procedure can be if not abstracted then greatly simplified by creating a menu class.  Here each menu items is associated with a tag.  A hashtable as the ...

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 {...