Page 1 of 1

Swing Tutorial V Learning Swing Rate Topic: ***** 1 Votes

#1 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Post icon  Posted 27 September 2009 - 06:43 PM

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.

/*
	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();

	}
}



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

/*
	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();

	}
}




Posted Image

Is This A Good Question/Topic? 4
  • +

Replies To: Swing Tutorial V

#2 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 October 2009 - 01:03 PM

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...
Was This Post Helpful? 0
  • +
  • -

#3 blixna_waka  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 60
  • Joined: 14-July 10

Posted 15 July 2010 - 07:29 PM

View Postjinnyishere, on 04 October 2009 - 07:03 PM, said:

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


I'm terribly sorry to inconvenience you...but it works flawlessly. ;)
Keep up the good work.
Was This Post Helpful? 0
  • +
  • -

#4 jinnyishere  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 46
  • View blog
  • Posts: 127
  • Joined: 06-July 09

Posted 04 May 2012 - 09:59 AM

:P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1