Swing

Using a Spinner

A spinner is a text field that has two little arrows next to it. A user can click one of these arrows to increase or decrease the value in the text field. Usually, the value in the text field contains a number.

Creating a Spinner

JSpinner jspin = new JSpinner();

Getting value from a spinner we use the getValue method

int value = jspin.getValue();

Creating a spinner with a specify min and max number

JSpinner hours = new JSpinner(new SpinnerNumberModel(1, 1, 12, 1));

Lets see a example on it.
 

 

 

ClickHere to download Spinner.java

 

Using a Tree

A tree is a more fancy swing component that displays hierarchical data in outline form. The type of tree that we most see/familiar with is the directory structure of the disk drive. Before we start to learn how to create a tree, lets learn a few terms that describe the elements in the tree.

 

        *    Node:    Each element in the tree is called a node. The nodes in a tree must be created from a class that implements the TreeNode interface.

        *    Root node:    Root node is the starting node of a tree. Every tree components must only have one root node. When we create a
                                        tree component, we pass the  root node to the JTree Constructor.

        *    Parent node:    The node that is immediately above a given node. Every node except the root node must have only one parent  node.

         *    Child node:    The node that appear immediately below a given node. A node can have more than one child nodes.

         *    Leaf node:    A node that doesn't have any children. It represent the end of a branch.

         *    Sibling nodes:    Nodes that are children of the same parent.

         *    Path:    A node and all of its ancestors such as, its parent, its parent's parent and so on all the way up to the root node.

         *    Collapsed node:    A node whose children are hidden.

         *    Expanded node:    A node whose children are visible.

Building a Tree

The easiest way to build a tree is to use the DefaultMutableTreeNode class, It can be found inside the link below "Swing Method & Constructor Table." Since the DefaultMutableTreeNode class implements the TreeNode interface, we can use the DefaultMutableTreenode objects for any of the methods listed in the table called for TreeNode objects.

 


 

ClickHere to download JTreeWindow.java

 

The above program, a method DefaultMutableTree is created, hence we use the method to create the root folder, files and other folders.

 

 

  <Previous><Java 24><Home><Swing Method & Constructor Table>