Reputation: 69
I have designed a checkbox treeviewer using jface but when i run the program it doesnot display any thing on the UI.I am not able to find the where the issue is.Please help me whrer the problem is as I am new to jface.
private void createTreeMenu(Composite parentComposite){
/*treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));*/
Composite treeMenu = new Composite(parentComposite, SWT.BORDER);
treeMenu.setLayout(new GridLayout(1, false));
treeMenu.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
/*tree = new Tree (treeMenu, SWT.MULTI | SWT.CHECK |SWT.VIRTUAL );
GridData treeGD = new GridData(SWT.FILL, GridData.FILL, true, true);
tree.setLayoutData(treeGD);*/
CheckboxTreeViewer treeViewer=new CheckboxTreeViewer(tree);
treeViewer.setContentProvider(new FileTreeContentProvider());
treeViewer.setLabelProvider(new FileTreeLabelProvider1());
treeViewer.setInput(treeItem);
parameters.add("root");
/*treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));*/
tree.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if(e.item == null)
{
return;
}
treeSelection = tree.getSelection();
}
});
treeMenu.addListener(SWT.MouseDown, new Listener() {
@Override
public void handleEvent(Event event) {
tree.setSelection(new TreeItem[] {});
treeSelection = null;
}
});
final Menu menu = new Menu(tree);
tree.setMenu(menu);
loadMenuDetails(menu);
}
public TreeItem getInput()
{
treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));
return treeItem;
}
/**
* This class provides the content for the tree in FileTree
*/
class FileTreeContentProvider implements ITreeContentProvider {
/**
* Gets the children of the specified object
*
* @param arg0
* the parent object
* @return Object[]
*/
public Object[] getChildren(Object arg0) {
// Return the files and subdirectories in this directory
//return ((File) arg0).listFiles();
return null;
}
/**
* Gets the parent of the specified object
*
* @param arg0
* the object
* @return Object
*/
public Object getParent(Object arg0) {
// Return this file's parent file
//return ((File) arg0).getParentFile();
return null;
}
/**
* Returns whether the passed object has children
*
* @param arg0
* the parent object
* @return boolean
*/
public boolean hasChildren(Object arg0) {
// Get the children
Object[] obj = getChildren(arg0);
// Return whether the parent has children
return obj == null ? false : obj.length > 0;
}
/**
* Gets the root element(s) of the tree
*
* @param arg0
* the input data
* @return Object[]
*/
/*public Object[] getElements(Object arg0) {
// These are the root elements of the tree
// We don't care what arg0 is, because we just want all
// the root nodes in the file system
return File.listRoots();
//return "New ROOT";
}*/
public Object[] getElements(Object inputElement) {
//TreeItem rootItem = new TreeItem((Tree) arg0, SWT.CHECK);
//rootItem.setText("ROOT");
// return new Object[] { "Session Root" }; // your root item you want to display
treeItem = new TreeItem(tree, SWT.MULTI | SWT.CHECK | SWT.VIRTUAL |SWT.BORDER );
treeItem.setText("(1)Test Session");
treeItem.setImage(new Image(null, TreeViewer.class.getClassLoader().getResourceAsStream("icons/Folder-Main.JPG")));
if (inputElement instanceof TreeItem)
{
System.out.println("getelements");
return ((List<String>)inputElement).toArray();
}
return new Object[] {parameters.toArray()};
}
/**
* Disposes any created resources
*/
public void dispose() {
// Nothing to dispose
}
/**
* Called when the input changes
*
* @param arg0
* the viewer
* @param arg1
* the old input
* @param arg2
* the new input
*/
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// Nothing to change
}
}
class FileTreeLabelProvider1 implements ILabelProvider {
// The listeners
private List listeners;
// Images for tree nodes
private Image file;
private Image dir;
// Label provider state: preserve case of file names/directories
/**
* Constructs a FileTreeLabelProvider
*/
public FileTreeLabelProvider1() {
// Create the list to hold the listeners
listeners = new ArrayList();
// Create the images
try {
file = new Image(null, new FileInputStream("images/file.gif"));
dir = new Image(null, new FileInputStream("images/directory.gif"));
} catch (FileNotFoundException e) {
// Swallow it; we'll do without images
}
}
/**
* Gets the image to display for a node in the tree
*
* @param arg0
* the node
* @return Image
*/
public Image getImage(Object arg0) {
// If the node represents a directory, return the directory image.
// Otherwise, return the file image.
return ((File) arg0).isDirectory() ? dir : file;
}
/**
* Gets the text to display for a node in the tree
*
* @param arg0
* the node
* @return String
*/
public String getText(Object arg0) {
// Get the name of the file
String text = ((File) arg0).getName();
// If name is blank, get the path
if (text.length() == 0) {
text = ((File) arg0).getPath();
}
// Check the case settings before returning the text
return text ;
}
/**
* Adds a listener to this label provider
*
* @param arg0
* the listener
*/
public void addListener(ILabelProviderListener arg0) {
listeners.add(arg0);
}
/**
* Called when this LabelProvider is being disposed
*/
public void dispose() {
// Dispose the images
if (dir != null)
dir.dispose();
if (file != null)
file.dispose();
}
/**
* Returns whether changes to the specified property on the specified
* element would affect the label for the element
*
* @param arg0
* the element
* @param arg1
* the property
* @return boolean
*/
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
/**
* Removes the listener
*
* @param arg0
* the listener to remove
*/
public void removeListener(ILabelProviderListener arg0) {
listeners.remove(arg0);
}
}
Upvotes: 1
Views: 1600
Reputation: 386
Try giving the tree object the parent composite and the flags (SWT.BORDER | SWT.CHECK) it worked for me this way
tree = new Tree(treeMenu, SWT.BORDER | SWT.CHECK);
Upvotes: 0