JTree Example
Would you like to make this site your homepage? It's fast and easy...
Yes, Please make this my home page!
/*
Author:ZT
Date : Aug 30,2017
Purpose: Using JTtree & JMenu & Menu Bar in java
to compile it use javac -d "." {Appl08302017o.java}
to run it use java Appl08302017o
to make jar file jar -cfe myjar.jar Appl08302017o Appl08302017o.class
to run it from command line java -jar myjar.jar
to run it from desktop use link it to myjar.jar
*/
package a.b.c;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
class Appl08302017o extends JFrame implements ActionListener{
Appl08302017o(){
//form1();
form2();
} // constrcutor
public static void main(String str1[]){
new Appl08302017o();
// _welmsg();
} // end of main() method
static void _welmsg(){
for(int i=0;i<5;i++)
System.out.println("Welcome to DM java Team");
} // end of _welmsg() method
//
void form1(){
setSize(600,600);setLocation(60,60);
setTitle("Dubai Municipality");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(true);
} // end of form1() method
void form2(){
JMenu mn1,mn2,mn3,mn4,mn5;
JFrame f2=new JFrame();
f2.setLayout(new BorderLayout(2,2));
JMenuBar br1=new JMenuBar();
JSeparator sep1 = new JSeparator(JSeparator.VERTICAL);
JMenu m1=new JMenu("File");
br1.add(m1," ");
//mn1=new JMenu("File");
//mb1.add(mn1);
//
mn2=new JMenu("Edit");
br1.add(mn2);
//
mn3=new JMenu("Tools");
br1.add(mn3);
//
mn4=new JMenu("Help");
JMenuItem mn4_i1=new JMenuItem("About");
mn4.add(mn4_i1);
mn4_i1.addActionListener(this);
br1.add(mn4);
JMenuItem i1=new JMenuItem("New");
i1.addActionListener(this);
JMenuItem i2=new JMenuItem("Open");
i2.addActionListener(this);
JMenuItem i3=new JMenuItem("Save");
JMenuItem i4=new JMenuItem("Save As");
JMenuItem i5=new JMenuItem("Exit");
i5.addActionListener(this);
m1.add(i1);
m1.addSeparator();
m1.add(i2);
m1.addSeparator();
m1.add(i3);
m1.addSeparator();
m1.add(i4);
m1.addSeparator();
m1.add(i5);
//m1.addSeparator();
// ContextMenu.MenuItems.Add(new MenuItem("Cancel"));
JTextArea a1=new JTextArea(2,2);
a1.setText("Write SomeThing.....");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("QA System");
DefaultMutableTreeNode auto = new DefaultMutableTreeNode("Automation");
DefaultMutableTreeNode BlackBox = new DefaultMutableTreeNode("BlackBox");
DefaultMutableTreeNode White = new DefaultMutableTreeNode("WhiteBox");
DefaultMutableTreeNode Performance = new DefaultMutableTreeNode("Performance");
DefaultMutableTreeNode aut1 = new DefaultMutableTreeNode("Aut1");
DefaultMutableTreeNode aut2 = new DefaultMutableTreeNode("Aut2");
//add the child nodes to the root node
root.add(auto);
auto.add(aut1);
aut1.add(aut2);
root.add(BlackBox);
root.add(White);
root.add(Performance);
JTree tree1=new JTree(root);
f2.add(tree1,BorderLayout.CENTER);
//
JLabel footer1=new JLabel("Copy Right reserved for DM");
f2.add(footer1,BorderLayout.SOUTH);
f2.add(br1,BorderLayout.NORTH);
f2.setSize(400,500);
f2.setTitle("Dubai Municipality /ITD/QA ");
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setVisible(true);
} // end of form2() method
public void actionPerformed(ActionEvent e){
String act1=e.getActionCommand();
if(act1.equals("Open")){
_OpenFiles();
}
else if (act1.equals("Exit")){
//JOptionPane.showMessageDialog(null,act1);
System.exit(0);
}
else if(act1.equals("About")){
JOptionPane.showMessageDialog(null,"Version 1.1 \n Copy Right Reserved for DM \n Powered by ZT ");
}
else if(act1.equals("New")){
form1();
}
} // end of actionPeformed() method
void _OpenFiles(){
//JOptionPane.showMessageDialog(null,act1);
Frame f1=new Frame(BorderLayout.CENTER);
FileDialog fd = new FileDialog(f1, "Load Points", FileDialog.LOAD);
fd.setVisible(true);
} // end of _OpenFiles()
}