SWING / CATATAN</>↗Swing30 Nov 2009
The code below demonstrates how to delete a node from a JTree component. package org.kodejava.swing; import javax.swing.*; import javax.swing.tree.*; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class JTreeRemoveNode extends JFrame { public JTreeRemoveNode() throws HeadlessException { initializeUI(); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new JTreeRemoveNode().setVisible(true)); } priva...
SWING / CATATAN</>↗Swing25 Nov 2009
The following example show how to use the JTree.getSelectionPaths() method to get the selected node of a JTree component. This method returns an array of TreePath objects. In the case to get the last node in the path we can call the TreePath.getLastPathComponent(). In the code below when you pressed the button the listener gets […]
SWING / CATATAN</>↗Swing23 Nov 2009
The program below demonstrate how to change the default icons of a JTree component. We can change tree icons of the component which are the closed icon, the open icon and the leaf icon. package org.kodejava.swing; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import java.awt.*; import java.util.Objects; public class JTreeChangeIcon extends JFrame { public JTreeChangeIcon() throws […]
SWING / CATATAN</>↗Swing21 Nov 2009
You can remove JTree default icons by modifying the tree cell renderer object. To get the cell renderer call the JTree.getCellRenderer() which will return a DefaultTreeCellRenderer object. Then you can remove the icon by setting a null value to the setLeafIcon(), setClosedIcon() and setOpenIcon(). package org.kodejava.swing; import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import java.awt.*; public […]
CORE API / CATATAN</>↗Core API17 Nov 2009
To sort the elements of LinkedList we can use the Collections.sort(List<T> list) static methods. The default order of the sorting is a descending order. package org.kodejava.util; import java.util.LinkedList; import java.util.Collections; public class LinkedListSort { public static void main(String[] args) { LinkedList<String> grades = new LinkedList<>(); grades.add("E"); grades.add("C"); grades.add("A"); grades.add("F"); grades.add("B"); grades.add("D"); System.out.println("...
CORE API / CATATAN</>↗Core API16 Nov 2009
The get the first element we can use the LinkedList.getFirst() method and to get the last element we can use the LinkedList.getLast() method. package org.kodejava.util; import java.util.LinkedList; public class LinkedListGetFirstLast { public static void main(String[] args) { LinkedList<String> names = new LinkedList<>(); names.add("Alice"); names.add("Bob"); names.add("Carol"); names.add("Mallory"); // Get the first and the last element of […]