Jtree, Jtable Java Programming
Jtree, Jtable Java Programming
Root
Leaf
TREE MODELS
Saat JTree menampilkan pohon, representasi data dari pohon ditangani
oleh TreeModel, TreeNode, dan TreePath. TreeModel mewakili seluruh
pohon, TreeNode merepresentasikan sebuah node, dan TreePath
merepresentasikan path ke node. Berbeda dengan ListModel atau
TableModel, model pohon tidak secara langsung menyimpan atau
mengelola data pohon. Data pohon disimpan dan dikelola di TreeNode dan
TreePath.
TreeNode
javax.swing.JTree
CLASS
#model: TreeModel
#rootVisible: boolean Specifies whether the root is displayed (depending on the constructor).
#rowHeight: int Specifies the height of the row for the node displayed in the tree
(default: 16 pixels).
#scrollsOnExpand: boolean If true, when a node is expanded, as many of the descendants are
scrolled to be visible (default: 16 pixels).
#selectionModel: TreeSelectionModel Models the set of selected nodes in this tree.
#showsRootHandles: boolean Specifies whether the root handles are displayed (default: true).
#toggleClickCount: int Number of mouse clicks before a node is expanded (default: 2).
-anchorSelectionPath: TreePath The path identified as the anchor.
-expandsSelectedPaths: boolean True if paths in the selection should be expanded (default: true).
-leadSelectionPaths: TreePath The path identified as the lead.
+JTree() Creates a JTree with a sample tree model, as shown in Figure 24.35.
+JTree(value: java.util.Hashtable) Creates a JTree with an invisible root and the keys in the Hashtable
key/value pairs as its children.
+JTree(value: Object[]) Creates a JTree with an invisible root and the elements in the array as
its children.
+JTree(newModel: TreeModel) Creates a JTree with the specified tree model.
+JTree(root: TreeNode) Creates a JTree with the specified tree node as its root.
+JTree(root: TreeNode, asksAllowsChildren: Creates a JTree with the specified tree node as its root and decides
boolean) whether a node is a leaf node in the specified manner.
+JTree(value: Vector) Creates a JTree with an invisible root and the elements in the vector as
its children.
+addSelectionPath(path: TreePath): void Adds the specified TreePath to the current selection.
+addSelectionPaths(paths: TreePath[]): void Adds the specified TreePaths to the current selection.
+addSelectionRow(row: int): void Adds the path at the specified row to the current selection.
+addSelectionRows(rows: int[]): void Adds the path at the specified rows to the current selection.
+clearSelection() : void Clears the selection.
+collapsePath(path: TreePath): void Ensures that the node identified by the specified path is collapsed and
viewable.
+getSelectionPath(): TreePath Returns the path from the root to the first selected node.
+getSelectionPaths(): TreePath[] Returns the paths from the root to all the selected nodes.
+getLastSelectedPathComponent() Returns the last node in the first selected TreePath.
+getRowCount():int Returns the number of rows currently being displayed.
+removeSelectionPath(path: TreePath): void Removes the node in the specified path.
+removeSelectionPaths(paths: TreePath[]):void Removes the node in the specified paths.
CONTOH : SIMPLE TREE
Permasalahan : Tulis program untuk membuat empat pohon:
pohon default menggunakan konstruktor no-arg, pohon yang
dibuat dari larik objek, pohon yang dibuat dari vektor, dan pohon
yang dibuat dari tabel hash. Memungkinkan pengguna untuk
secara dinamis mengatur properti untuk rootVisible, rowHeight,
dan showsRootHandles.
TREEMODEL DAN DEFAULTTREEMODEL
TreeModel berisi
informasi struktural
tentang tree, dan javax.swing.tree.TreeModel
+getChild(parent: Object, index: int): Object Returns the child of parent at the index in the parent's child array.
data tree disimpan +getChildCount(parent: Object): int Returns the number of children of the specified parent in the tree model.
TreeModel yang #asksAllowsChildren: Boolean Tells how leaf nodes are determined. True if only nodes that do not allow
children are leaf nodes, false if nodes that have no children are leaf nodes.
javax.swing.tree.MutableTreeNode
MutableTreeNode
+getDepth(): int Returns the depth of the tree rooted at this node.
+getLevel(): int Returns the distance from the root to this node.
+getNextNode(): DefaultMutableTreeNode Returns the node that follows (precedes) this node in a preorder traversal of
+getPreviousNode(): DefaultMutableTreeNode this node.
+getSiblingCount(): int Returns the number of siblings of this node.
+getNextSibling(): DefaultMutableTreeNode Returns the next sibling of this node in the parent's child vector.
+getPath(): TreeNode[] Returns the path from the root to this node.
+getRoot(): TreeNode Returns the root of the tree that contains this node.
+isRoot(): boolean Returns true if this node is the root of the tree.
+breadthFirstEnumeration(): Enumeration Creates and returns an enumeration that traverses the subtree rooted at this
+depthFirstEnumeration(): Enumeration node in breadth-first order (depth-first order, postorder, preorder). These
+postorderEnumeration(): Enumeration traversals were discussed in Chapter 17, “Data Structure
+preorderEnumeration(): Enumeration Implementations.”
CONTOH : TREE MODEL DEMO
Masalah: Menulis program untuk membuat dua tree yang
menampilkan “World”, benua, negara dan negara bagian.
Kedua tree menampilkan konten yang identik. Program ini
juga menampilkan properti dari pohon di area teks.
TREESELECTIONMODEL DAN
DEFAULTTREESELECTIONMODEL
Pemilihan node Tree
didefinisikan dalam
antarmuka javax.swing.tree.TreeSelectionModel
TreeSelectionModel. +addSelectionPath(path: TreePath): void Adds the specified TreePath to the current selection.
+addSelectionPaths(paths: TreePath[]): void Adds the specified TreePaths to the current selection.
+clearSelection() : void Clears the selection.
+getLeadSelectionPath(): TreePath Returns the last path in the selection.
+getSelectionCount(): int Returns the number of paths in the selection.
+getSelectionPath(): TreePath Returns the first path in the selection.
Class +getSelectionPaths(): TreePath[] Returns all the paths in the selection.
DefaultTreeSelection +getSelectionMode(): int Returns the current selection mode,
Model adalah +removeSelectionPath(path: TreePath): void Removes path from the selection.
+removeSelectionPaths(paths: TreePath[]):void Removes paths from the selection.
implementasi nyata +setSelectionMode(mode: int): void Sets the selection mode.
dari +setSelectionPath(path: TreePath): void Sets the selection to path.
TreeSelectionModel, +setSelectionPaths(paths: TreePath[]): void Sets the selection to paths.
+addTreeSelectionListener(x: TreeSelectionListener): void Register a TreeSelectionListener.
yang +removeTreeSelectionListener(x: TreeSelectionListener): void Remove a TreeSelectionListener.
mempertahankan
array objek TreePath javax.swing.tree.DefaultTreeSelectionModel
yang mewakili pilihan
CONTOH : MODIFIYING TREES
Masalah: Menulis program untuk membuat dua pohon yang menampilkan
konten yang sama: dunia, benua, negara dan negara bagian, seperti yang
ditunjukkan pada Gambar dibawah. Untuk tree di sebelah kiri, aktifkan
pengguna untuk memilih mode pilihan, tambahkan anak baru di bawah simpul
yang dipilih pertama, dan hapus semua simpul yang dipilih.
TREE NODE RENDERING
DefaultTreeCellRenderer renderer =
(DefaultTreeCellRenderer)jTree1.getCellRenderer();
renderer.setLeafIcon(yourCustomLeafImageIcon);
renderer.setOpenIcon(yourCustomOpenImageIcon);
renderer.setClosedIcon(yourCustomClosedImageIcon);
renderer.setBackgroundSelectionColor(Color.red);
TREE EDITING
// Customize editor
JComboBox jcboColor = new JComboBox();
jcboColor.addItem("red");
jcboColor.addItem("green");
jcboColor.addItem("blue");
jcboColor.addItem("yellow");
jcboColor.addItem("orange");
jTree1.setCellEditor(new DefaultCellEditor(jcboColor));
jTree1.setEditable(true);
TREE RENDERING AND EDITING
jTree1.setCellEditor
(new DefaultTreeCellEditor(jTree1,
new DefaultTreeCellRenderer(),
new DefaultCellEditor(jcboColor)));
JTree dapat mengaktifkan TreeSelectionEvent dan
TreeExpansionEvent, di antara banyak acara lainnya. Setiap kali
simpul baru dipilih, JTree mengaktifkan TreeSelectionEvent.
Setiap kali node diperluas atau diciutkan, JTree mengaktifkan
TreeExpansionEvent. Untuk menangani peristiwa pemilihan
pohon, pendengar harus mengimplementasikan antarmuka
TreeSelectionListener, yang berisi metode valueChanged handler
tunggal. TreeExpansionListener berisi dua penangan bernama
treeCollapsed dan treeExpanded untuk menangani node
expansion atau node closing.
ARRAY + JTABLE
&
DATABASE
JTABLE
Tabel digunakan untuk menampilkan data dalam mode
spreadsheet
Jadi konsep-konsep kunci dalam model tabel:
cell, baris, kolom
Nilai (s) di masing-masing
Setiap tabel mendapatkan datanya dari sebuah objek
yang mengimplementasikan TableModel Interface
Dapat digunakan class DefaultTableModel, atau
Membuat class baru dimana mengimplementasikan
AbstractTableModel Interface dan mengimplemntasikan
metode berikut
getColumnName
import
getRowCount java.swingx.table.*;
getColumnCount
getValueAt
JAVA
MENGGUNAKAN DEFAULTTABLEMODEL
...
getContentPane().add(table, BorderLayout.CENTER);
LATIHAN