PDA

View Full Version : Resolved Global Object for a class


mayankgera
03-31-2009, 01:07 PM
Hi

I m creating an application using swing.Now i have a menu in which i select new project and take path from user(this happens in menu.java).Then i want to create a Jtree for that path i have with me.

Now i create JTree with that path.JTree does gets created but when i try to add it to the frame that i had created in frame.java(having JFrame creation) it is not visble.I am making an object for that class there(in menu.java) and calling the function in frame.java to add it to JFrame

The tree is not added to frame (having menu created earlier).
I think it has to do with object of the frame.java, as i m creating a new instance in menu.java

Please help how do i do it any other way.

bdl
03-31-2009, 01:49 PM
Can you show us the relevant section of code?

mayankgera
03-31-2009, 02:09 PM
The code is attached

From main i call frame -> which creates frame calling menubar
menubar-> on actionListener calls tree and and adds to frame

frame.java

/*
* Frame.java
*
* Created on 31 March, 2009, 4:45 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
*
* @author Mayank
*/
public class Frame {
JFrame frame = new JFrame();
/** Creates a new instance of Frame */
public void CreateFrame() {
//Create and set up the window.
File temp = new File("C:\\Tool\\temp.txt");
temp.delete();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


contents file_content = new contents();
file_content.showcontents(frame);
try {
file_content.addfile("../Tool/src/tool/PropertyWindow.java");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
//Create and set up the content pane.
Menubar menubar = new Menubar();
frame.getContentPane().add(menubar.CreateMenubar(),BorderLayout.NORTH);
//frame.getContentPane().add(menubar.createContentPane(),BorderLayout.CENTER);

//Display the window.
frame.setSize(800, 600);
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );


// Tree tree = new Tree();
// addTree(tree.CreateTree("."));



}
public void addTree(JScrollPane treePane)
{
frame.getContentPane().add(treePane,BorderLayout.EAST);
//frame.repaint();

}

}



main.java



package tool;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Main {
static Frame frame;
/** Create final JFrame frame = new JFrame("Advanced WebPage Development Tool");s a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void createAndShowGUI() {

frame=new Frame();
frame.CreateFrame();


}



public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();


}

});


}

}



menubar.java

/*
* Menubar.java
*
* Created on 19 March, 2009, 4:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
*
* @author Mayank
*/
public class Menubar extends Main implements ActionListener {
JTextArea output;
JScrollPane scrollPane;
ButtonHandler handler=new ButtonHandler();
JButton BrowseButton = new JButton();
JButton OkButton = new JButton();
JFileChooser BrowseFile= new JFileChooser();
JTextField PathBar = new JTextField();
JTextField ProjectName = new JTextField();
JLabel ProjLab= new JLabel();
JLabel FolderLab= new JLabel();
JDialog newFolderDiag;

/** Creates a new instance of Menubar */
public JScrollPane CreateMenubar() {

JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;

//Create the menu bar.
menuBar = new JMenuBar();
JScrollPane scrollpane = new JScrollPane();
scrollpane.setViewportView(menuBar);
menuBar.setPreferredSize(new Dimension(50,10));
//Build the first menu.
menu = new JMenu("FILE");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);

//a group of JMenuItems
menuItem = new JMenuItem("New",KeyEvent.VK_T);

//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem.addActionListener(this);


menuItem = new JMenuItem("Open");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);


//a group of radio button menu items
menu.addSeparator();


menuItem = new JMenuItem("Save");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);
menu.addSeparator();

//a group of check box menu items
menuItem = new JMenuItem("Exit");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);


//Build second menu in the menu bar.
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);

return scrollpane;
}
/*Not using right now */
public JScrollPane createContentPane() {

//Create a scrolled text area.
output = new JTextArea();

output.setEditable(false);
scrollPane = new JScrollPane();
scrollPane.setViewportView(output);

//Add the text area to the content pane.

return scrollPane;
}

public void actionPerformed(ActionEvent e) {


if(e.getActionCommand().equals("New"))
{

newFolderDiag =new JDialog();


newFolderDiag.setTitle("Select Project Location");
Container dialogContentPane = newFolderDiag.getContentPane();
dialogContentPane.setLayout(null);
newFolderDiag.setVisible(true);
newFolderDiag.setBounds(300,200,450,150);

ProjLab.setText("Enter Project Name");
ProjLab.setBounds(10,20,130,25);

FolderLab.setText("Select Path");
FolderLab.setBounds(10,50,130,25);

PathBar.setBounds(150,50,155,25);
PathBar.setEditable(true);

ProjectName.setBounds(150,20,100,25);
ProjectName.setEditable(true);

BrowseButton.setText("Browse");
BrowseButton.setBounds(310, 50, 85, 23);

dialogContentPane.add(PathBar);
dialogContentPane.add(ProjLab);
dialogContentPane.add(FolderLab);
dialogContentPane.add(ProjectName);
dialogContentPane.add(BrowseButton);

BrowseButton.setVisible(true);
BrowseButton.addActionListener(handler);

OkButton.setText("OK");
OkButton.setBounds(150, 85, 55, 23);
dialogContentPane.add(OkButton);
OkButton.setVisible(true);
OkButton.addActionListener(handler);



PathBar.setText("C:");
ProjectName.setText("webapp");


}


if(e.getActionCommand().equals("Open"))
{

JFileChooser dialog = new JFileChooser();
dialog.showOpenDialog(null);


}
if(e.getActionCommand().equals("Save"))
{

JFileChooser dialog3 = new JFileChooser();
dialog3.showSaveDialog(null);


}
if(e.getActionCommand().equals("Exit"))
{
String path = null ;
BufferedReader input;
try {
input = new BufferedReader(new FileReader("C:\\Tool\\temp.txt"));
try {
path = input.readLine();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}


} catch (FileNotFoundException ex) {
ex.printStackTrace();
}

PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(path+"\\web\\jsp\\LeftCol.jsp", true));
out.println("</div>");
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}

System.exit(0);



}

}


public static void copyFolder(File srcFolder, File destFolder) throws IOException{
File oSourcecDir = null;
if (srcFolder.isDirectory())
{
if (! destFolder.exists())
{
destFolder.mkdir();
}

String[] oChildren = srcFolder.list();
for (int i=0; i < oChildren.length; i++)
{
copyFolder(new File(srcFolder, oChildren[i]), new File(destFolder, oChildren[i]));
}
}
else
{
if(destFolder.isDirectory())
{
copyFile(oSourcecDir, new File(destFolder, srcFolder.getName()));
}
else
{
copyFile(srcFolder, destFolder);
}
}
}

public static void copyFile(File srcFile, File destFile) throws IOException{
InputStream oInStream = new FileInputStream(srcFile);
OutputStream oOutStream = new FileOutputStream(destFile);

// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
private class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==BrowseButton){
JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(null);
String temp= chooser.getSelectedFile().toString();
PathBar.setText(temp);
}

if(e.getSource()==OkButton){
//template select_template=new template();
// select_template.createtemplate(PathBar.getText()+ File.separator + ProjectName.getText());
newFolderDiag.setVisible(false);

PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("C:\\Tool\\temp.txt", true));
out.println(PathBar.getText()+ File.separator + ProjectName.getText());
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Tree tree = new Tree();

Frame frame = new Frame();

frame.addTree(tree.CreateTree(PathBar.getText()+ File.separator + ProjectName.getText()));
// main.createTree(PathBar.getText()+ File.separator + ProjectName.getText());

////////////

File dest = new File (PathBar.getText()+ File.separator + ProjectName.getText());

File src = new File ("C:\\Tool\\src\\webapp");

try {
copyFolder(src,dest);
File dest1=new File(PathBar.getText()+ File.separator + ProjectName.getText() + "\\src\\java\\com\\ui_framework\\tags\\PropertyFileReader.java");
File src1=new File("C:\\Tool\\src\\javafiles\\PropertyFileReader.txt");

copyFile(src1,dest1);

File dest2=new File(PathBar.getText()+ File.separator + ProjectName.getText() + "\\src\\java\\com\\ui_framework\\struts\\LogOnAction.java");
File src2=new File("C:\\Tool\\src\\javafiles\\LogOnAction.txt");

copyFile(src2,dest2);
} catch (IOException ex) {
ex.printStackTrace();
}

//////////////
}
}
}
}



tree.java


/*
* Tree.java
*
* Created on 20 March, 2009, 12:57 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

/**
*
* @author Mayank
*/
public class Tree {

JTree tree;
JScrollPane scrollpane = new JScrollPane();
/** Creates a new instance of Tree */
public JScrollPane CreateTree(String path) {

tree=FileTree(new File(path));
tree.setPreferredSize(new Dimension(300,120));
tree.setScrollsOnExpand(true);
scrollpane.setViewportView(tree);

return scrollpane;
}

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) { // should only be null at root
curTop.add(curDir);
}
Vector ol = new Vector();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector files = new Vector();
// Make two passes, one for Dirs and one for Files. This is #1.
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
// Pass two: for files.
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}

public JTree FileTree(File dir) {
tree = new JTree(addNodes(null, dir));

// Add a listener
/*tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
.getPath().getLastPathComponent();
if(node.isLeaf())
{ System.out.println(node);

}
}
});*/
return tree;
}
}

mayankgera
03-31-2009, 02:12 PM
The code is attached

From main i call frame -> which creates frame calling menubar
menubar-> on actionListener calls tree and and adds to frame

frame.java

/*
* Frame.java
*
* Created on 31 March, 2009, 4:45 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
*
* @author Mayank
*/
public class Frame {
JFrame frame = new JFrame();
/** Creates a new instance of Frame */
public void CreateFrame() {
//Create and set up the window.
File temp = new File("C:\\Tool\\temp.txt");
temp.delete();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


contents file_content = new contents();
file_content.showcontents(frame);
try {
file_content.addfile("../Tool/src/tool/PropertyWindow.java");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
//Create and set up the content pane.
Menubar menubar = new Menubar();
frame.getContentPane().add(menubar.CreateMenubar(),BorderLayout.NORTH);
//frame.getContentPane().add(menubar.createContentPane(),BorderLayout.CENTER);

//Display the window.
frame.setSize(800, 600);
frame.setVisible(true);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH );


// Tree tree = new Tree();
// addTree(tree.CreateTree("."));



}
public void addTree(JScrollPane treePane)
{
frame.getContentPane().add(treePane,BorderLayout.EAST);
//frame.repaint();

}

}



main.java



package tool;

import java.awt.BorderLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class Main {
static Frame frame;
/** Create final JFrame frame = new JFrame("Advanced WebPage Development Tool");s a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void createAndShowGUI() {

frame=new Frame();
frame.CreateFrame();


}



public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();


}

});


}

}



menubar.java

/*
* Menubar.java
*
* Created on 19 March, 2009, 4:06 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;

/**
*
* @author Mayank
*/
public class Menubar extends Main implements ActionListener {
JTextArea output;
JScrollPane scrollPane;
ButtonHandler handler=new ButtonHandler();
JButton BrowseButton = new JButton();
JButton OkButton = new JButton();
JFileChooser BrowseFile= new JFileChooser();
JTextField PathBar = new JTextField();
JTextField ProjectName = new JTextField();
JLabel ProjLab= new JLabel();
JLabel FolderLab= new JLabel();
JDialog newFolderDiag;

/** Creates a new instance of Menubar */
public JScrollPane CreateMenubar() {

JMenuBar menuBar;
JMenu menu, submenu;
JMenuItem menuItem;
JRadioButtonMenuItem rbMenuItem;
JCheckBoxMenuItem cbMenuItem;

//Create the menu bar.
menuBar = new JMenuBar();
JScrollPane scrollpane = new JScrollPane();
scrollpane.setViewportView(menuBar);
menuBar.setPreferredSize(new Dimension(50,10));
//Build the first menu.
menu = new JMenu("FILE");
menu.setMnemonic(KeyEvent.VK_A);
menu.getAccessibleContext().setAccessibleDescription(
"The only menu in this program that has menu items");
menuBar.add(menu);

//a group of JMenuItems
menuItem = new JMenuItem("New",KeyEvent.VK_T);

//menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
menuItem.getAccessibleContext().setAccessibleDescription(
"This doesn't really do anything");
menu.add(menuItem);
menuItem.addActionListener(this);


menuItem = new JMenuItem("Open");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);


//a group of radio button menu items
menu.addSeparator();


menuItem = new JMenuItem("Save");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);
menu.addSeparator();

//a group of check box menu items
menuItem = new JMenuItem("Exit");
menuItem.setMnemonic(KeyEvent.VK_B);
menu.add(menuItem);
menuItem.addActionListener(this);


//Build second menu in the menu bar.
menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_N);
menu.getAccessibleContext().setAccessibleDescription(
"This menu does nothing");
menuBar.add(menu);

return scrollpane;
}
/*Not using right now */
public JScrollPane createContentPane() {

//Create a scrolled text area.
output = new JTextArea();

output.setEditable(false);
scrollPane = new JScrollPane();
scrollPane.setViewportView(output);

//Add the text area to the content pane.

return scrollPane;
}

public void actionPerformed(ActionEvent e) {


if(e.getActionCommand().equals("New"))
{

newFolderDiag =new JDialog();


newFolderDiag.setTitle("Select Project Location");
Container dialogContentPane = newFolderDiag.getContentPane();
dialogContentPane.setLayout(null);
newFolderDiag.setVisible(true);
newFolderDiag.setBounds(300,200,450,150);

ProjLab.setText("Enter Project Name");
ProjLab.setBounds(10,20,130,25);

FolderLab.setText("Select Path");
FolderLab.setBounds(10,50,130,25);

PathBar.setBounds(150,50,155,25);
PathBar.setEditable(true);

ProjectName.setBounds(150,20,100,25);
ProjectName.setEditable(true);

BrowseButton.setText("Browse");
BrowseButton.setBounds(310, 50, 85, 23);

dialogContentPane.add(PathBar);
dialogContentPane.add(ProjLab);
dialogContentPane.add(FolderLab);
dialogContentPane.add(ProjectName);
dialogContentPane.add(BrowseButton);

BrowseButton.setVisible(true);
BrowseButton.addActionListener(handler);

OkButton.setText("OK");
OkButton.setBounds(150, 85, 55, 23);
dialogContentPane.add(OkButton);
OkButton.setVisible(true);
OkButton.addActionListener(handler);



PathBar.setText("C:");
ProjectName.setText("webapp");


}


if(e.getActionCommand().equals("Open"))
{

JFileChooser dialog = new JFileChooser();
dialog.showOpenDialog(null);


}
if(e.getActionCommand().equals("Save"))
{

JFileChooser dialog3 = new JFileChooser();
dialog3.showSaveDialog(null);


}
if(e.getActionCommand().equals("Exit"))
{
String path = null ;
BufferedReader input;
try {
input = new BufferedReader(new FileReader("C:\\Tool\\temp.txt"));
try {
path = input.readLine();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}


} catch (FileNotFoundException ex) {
ex.printStackTrace();
}

PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter(path+"\\web\\jsp\\LeftCol.jsp", true));
out.println("</div>");
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}

System.exit(0);



}

}


public static void copyFolder(File srcFolder, File destFolder) throws IOException{
File oSourcecDir = null;
if (srcFolder.isDirectory())
{
if (! destFolder.exists())
{
destFolder.mkdir();
}

String[] oChildren = srcFolder.list();
for (int i=0; i < oChildren.length; i++)
{
copyFolder(new File(srcFolder, oChildren[i]), new File(destFolder, oChildren[i]));
}
}
else
{
if(destFolder.isDirectory())
{
copyFile(oSourcecDir, new File(destFolder, srcFolder.getName()));
}
else
{
copyFile(srcFolder, destFolder);
}
}
}

public static void copyFile(File srcFile, File destFile) throws IOException{
InputStream oInStream = new FileInputStream(srcFile);
OutputStream oOutStream = new FileOutputStream(destFile);

// Transfer bytes from in to out
byte[] oBytes = new byte[1024];
int nLength;
BufferedInputStream oBuffInputStream = new BufferedInputStream( oInStream );
while ((nLength = oBuffInputStream.read(oBytes)) > 0)
{
oOutStream.write(oBytes, 0, nLength);
}
oInStream.close();
oOutStream.close();
}
private class ButtonHandler implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==BrowseButton){
JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.showOpenDialog(null);
String temp= chooser.getSelectedFile().toString();
PathBar.setText(temp);
}

if(e.getSource()==OkButton){
//template select_template=new template();
// select_template.createtemplate(PathBar.getText()+ File.separator + ProjectName.getText());
newFolderDiag.setVisible(false);

PrintWriter out = null;
try {
out = new PrintWriter(new FileWriter("C:\\Tool\\temp.txt", true));
out.println(PathBar.getText()+ File.separator + ProjectName.getText());
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
Tree tree = new Tree();

Frame frame = new Frame();

frame.addTree(tree.CreateTree(PathBar.getText()+ File.separator + ProjectName.getText()));
// main.createTree(PathBar.getText()+ File.separator + ProjectName.getText());

////////////

File dest = new File (PathBar.getText()+ File.separator + ProjectName.getText());

File src = new File ("C:\\Tool\\src\\webapp");

try {
copyFolder(src,dest);
File dest1=new File(PathBar.getText()+ File.separator + ProjectName.getText() + "\\src\\java\\com\\ui_framework\\tags\\PropertyFileReader.java");
File src1=new File("C:\\Tool\\src\\javafiles\\PropertyFileReader.txt");

copyFile(src1,dest1);

File dest2=new File(PathBar.getText()+ File.separator + ProjectName.getText() + "\\src\\java\\com\\ui_framework\\struts\\LogOnAction.java");
File src2=new File("C:\\Tool\\src\\javafiles\\LogOnAction.txt");

copyFile(src2,dest2);
} catch (IOException ex) {
ex.printStackTrace();
}

//////////////
}
}
}
}



tree.java


/*
* Tree.java
*
* Created on 20 March, 2009, 12:57 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package tool;

import java.awt.Dimension;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collections;
import java.util.Vector;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

/**
*
* @author Mayank
*/
public class Tree {

JTree tree;
JScrollPane scrollpane = new JScrollPane();
/** Creates a new instance of Tree */
public JScrollPane CreateTree(String path) {

tree=FileTree(new File(path));
tree.setPreferredSize(new Dimension(300,120));
tree.setScrollsOnExpand(true);
scrollpane.setViewportView(tree);

return scrollpane;
}

/** Add nodes from under "dir" into curTop. Highly recursive. */
DefaultMutableTreeNode addNodes(DefaultMutableTreeNode curTop, File dir) {
String curPath = dir.getPath();
DefaultMutableTreeNode curDir = new DefaultMutableTreeNode(curPath);
if (curTop != null) { // should only be null at root
curTop.add(curDir);
}
Vector ol = new Vector();
String[] tmp = dir.list();
for (int i = 0; i < tmp.length; i++)
ol.addElement(tmp[i]);
Collections.sort(ol, String.CASE_INSENSITIVE_ORDER);
File f;
Vector files = new Vector();
// Make two passes, one for Dirs and one for Files. This is #1.
for (int i = 0; i < ol.size(); i++) {
String thisObject = (String) ol.elementAt(i);
String newPath;
if (curPath.equals("."))
newPath = thisObject;
else
newPath = curPath + File.separator + thisObject;
if ((f = new File(newPath)).isDirectory())
addNodes(curDir, f);
else
files.addElement(thisObject);
}
// Pass two: for files.
for (int fnum = 0; fnum < files.size(); fnum++)
curDir.add(new DefaultMutableTreeNode(files.elementAt(fnum)));
return curDir;
}

public JTree FileTree(File dir) {
tree = new JTree(addNodes(null, dir));

// Add a listener
/*tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) e
.getPath().getLastPathComponent();
if(node.isLeaf())
{ System.out.println(node);

}
}
});*/
return tree;
}
}

mayankgera
04-01-2009, 07:17 AM
The issuse was resolved by passing the object of frame class.