JTree Simple Practice Application in Java
Rohan Sakhale 6/16/2012 code-examplejavajava-swing
# Summary
JTree is a control that displays a set of hierarchical data as an outline.
We will use it as a menu to navigate through various small applications we made previously. The tree will basically contain each program as a node when selected using mouse, that particular program is displayed onto the JFrame.
Programs used in this code are already being posted before for practice, I will name the Class & the link to find the class
# Reference Code
- Simple Calculator in Java
- Factorial Program in Java
- Calculate Celsius to Fahrenheit Temperature in Java
- Calculate Fahrenheit to Celsius Temperature in Java
# Screenshot
# Code
package com.rohansakhale.treedemo;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
/**
*
* @author RohanSakhale
*/
public class TreeDemo {
static String lastComponent = "";
public static void main(String[] args) {
final JFrame jf = new JFrame("Practice Applications");
DefaultMutableTreeNode pracApps = new DefaultMutableTreeNode("Practice Apps", true);
DefaultMutableTreeNode calc = new DefaultMutableTreeNode("Calculator");
DefaultMutableTreeNode fact = new DefaultMutableTreeNode("Factorial");
DefaultMutableTreeNode temperature = new DefaultMutableTreeNode("Temperature", true);
DefaultMutableTreeNode c2f = new DefaultMutableTreeNode("C to F");
DefaultMutableTreeNode f2c = new DefaultMutableTreeNode("F to C");
temperature.add(c2f);
temperature.add(f2c);
pracApps.add(calc);
pracApps.add(fact);
pracApps.add(temperature);
final JTree tree = new JTree(pracApps);
// JTree here can act as a menu for Practice Applications
// We add it on the west side of the JFrame
jf.add(BorderLayout.WEST, tree);
/*
* Classes created here within the same package can also be found online
* Calc = http://rohansakhale.com/codes/39/simple-calculator-in-java
* Factorial = http://rohansakhale.com/codes/40/factorial-program-in-java
* C2F = http://rohansakhale.com/codes/41/calculate-celsius-to-fahrenheit-temperature-in-java
* F2C = http://rohansakhale.com/codes/42/calculate-fahrenheit-to-celsius-temperature-in-java
*
*/
final Calc calculatorPanel = new Calc();
final Factorial factorialPanel = new Factorial();
final C2F c2fObj = new C2F();
final F2C f2cObj = new F2C();
// Lets change the panel on center of JFrame
// by checking the mouse click
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
TreePath path = tree.getPathForLocation(me.getX(), me.getY());
if (path != null) {
// Check the last added component and remove it
if (!lastComponent.equals("")) {
if (lastComponent.equals("Factorial")) {
jf.remove(factorialPanel);
} else if (lastComponent.equals("C to F")) {
jf.remove(c2fObj);
} else if (lastComponent.equals("Calculator")) {
jf.remove(calculatorPanel);
} else if (lastComponent.equals("F to C")) {
jf.remove(f2cObj);
}
}
// Add the new component on the frame
// Depending on the Selected Tree Node
if (path.toString().contains("Calculator")) {
jf.add(calculatorPanel);
lastComponent = "Calculator";
} else if (path.toString().contains("Factorial")) {
jf.add(factorialPanel);
lastComponent = "Factorial";
} else if (path.toString().contains("C to F")) {
jf.add(c2fObj);
lastComponent = "C to F";
} else if (path.toString().contains("F to C")) {
jf.add(f2cObj);
lastComponent = "F to C";
}
jf.repaint();
}
}
});
jf.setSize(600, 400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97