Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,152 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,691 people online right now. Registration is fast and FREE... Join Now!




Swing Tutorial V

 
Reply to this topicStart new topic

> Swing Tutorial V, Learning Swing

jinnyishere
Group Icon



post 27 Sep, 2009 - 05:43 PM
Post #1


Remember click here for a list of the Methods and Constructor explanation

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.

CODE

/*
    The example demonstrate a simple spinner
    It allows the user to choose the time
    In addition, user can choose the am/pm

*
*/


import javax.swing.*;
import java.awt.event.*;

public class Spinner extends JFrame
{
    private JPanel panel;
    private JButton ok;
    private JSpinner spinner;

    public Spinner()
    {
        // Sets the title of the window

        setTitle("JSpinner Window");

        // Sets the size of the window

        setSize(300, 200);

        // Sets the start location of the window

        setLocationRelativeTo(null);

        // Sets the action when a window is close

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Creates a spinner with initial, min, and max value

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


        // Creates a OK button

        ok = new JButton("OK");

        // Creates an action listener for the button

        ok.addActionListener(new okButtonListener());

        // Creates a panel

        panel = new JPanel();

        // Adds the components to the panel

        panel.add(spinner);
        panel.add(ok);

        // Adds the panel to the content pane

        add(panel);

        // Sets the visibility of the window

        setVisible(true);

    }

    private class okButtonListener implements ActionListener
    {    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==ok)
            {
                Object value = spinner.getValue();

                JOptionPane.showMessageDialog(null, "You have selected " + value);

            }
        }
    }

    public static void main(String[] args)
    {
        new Spinner();

    }
}


IPB Image

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.

CODE

/*
    This is a JTree program

*
*/

import javax.swing.*;
import java.awt.event.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class JTreeWindow extends JFrame
{
    private JTree tree;
    private DefaultTreeModel model;
    private JLabel name;
    private JPanel panel;

    public JTreeWindow()
    {
        // Sets the title of the window

        setTitle("JTree Window");

        // Sets the size of the window

        setSize(300, 400);

        // Sets the start location of the window

        setLocationRelativeTo(null);

        // Sets the action when the window close

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Creates a panel

        panel = new JPanel();

        // Create a empty tree node

        DefaultMutableTreeNode root, Program_Files, Windows, System_32;

        // Creates the root directory

        root = new DefaultMutableTreeNode("C");

        // Adds program files, windows, and system 32 to the root directory

        Program_Files = create("Program Files", root);
        Windows = create("Program Files", root);
        System_32 = create("System 32", root);

        // Add files to the program file folder

        create("Winamp", Program_Files);
        create("JGrasp", Program_Files);
        create("Dev++", Program_Files);

        // Add files to the windows folder

        create("Window Media", Windows);
        create(".dll", Windows);
        create(".temp", Windows);

        // Add files to System 32

        create(".source", System_32);
        create("cookies", System_32);
        create("bugs", System_32);

        // Creates a tree that displays the tree that starts at the root node

        tree = new JTree(root);

        // Gets the model of the tree

        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

        // Sets the number of rows visible in the display

        tree.setVisibleRowCount(11);

        // Create an even listener for the tree

        tree.addTreeSelectionListener(new TreeListener());

        // Add a scroll pane to the tree

        JScrollPane sp = new JScrollPane(tree);

        // Create a label

        name = new JLabel();

        // Add the scroll pane and label into the panel

        panel.add(name);
        panel.add(sp);

        // Add the panel to the content pane

        add(panel);

        // Sets the visibility of the window

        setVisible(true);

    }

    private DefaultMutableTreeNode create(String title, DefaultMutableTreeNode parent)
    {
        // Creates a tree node with a specified user object

        DefaultMutableTreeNode creating = new DefaultMutableTreeNode(title);

        // Adds it to the tree

        parent.add(creating);

        return creating;

    }

    private class TreeListener implements TreeSelectionListener
    {
        public void valueChanged(TreeSelectionEvent e)
        {
            // Gets the currently selected node

            Object getSelect = tree.getLastSelectedPathComponent();

            DefaultMutableTreeNode creating = (DefaultMutableTreeNode) getSelect;

            String title = (String)creating.getUserObject();

            name.setText(title);

        }
    }

    public static void main(String[] args)
    {
        new JTreeWindow();

    }
}



IPB Image

Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

jinnyishere
Group Icon



post 4 Oct, 2009 - 12:03 PM
Post #2
Please!! Do leave some feedbacks so i know what to improve...

is it confusing, not clear enough, needs more examples and so on..

open for any suggestions...
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:39PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month