JTrees Vectors Help

Can anyone give some advice on how to implement a JTree using a vector and adding tree listner's or a good site to reference. I have scourge many sites trying to find what I am looking for but I can never find an example containing both actions. What I am looking for is a way to nodes such as Titles, Genre,Rating,Cast, Directors, Year..etc. To note this is being used to house movies. However, I want to set it up so a movie may be in multiple nodes. For example, Star Wars II would be in the node Titles, Genre node containing an Action node, Rating node containing a PG-13 node, ...etc. I want to press the leafs (being movies of a particular node) to display in another panel with the Movies details. My biggest hang up seems to stem from the .getUserObject(). How does the Jtree and the information I need in the vector establish the desired connection. Any help would be appreciated.

did you read
http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html?
hth

Similar Messages

  • Vector help

    hi
    im making a level editor for my breakout game now
    and I use a vector to store the game objects..
    However when painting I want to go through the vector and call
    each objects draw method..but that doesnt work. Im just used to use arrays but they cant hold objects of different types so thats a problem.
    What should I do? I just want to reference an object in the vector and call the method to draw it.

    If you look at the JavaDocs, you'll note that everything extends java.lang.Object. So what does this mean? It means, we can mix types in an array. :-)
    Object[] stuff = new Object[10];
    populateArray(stuff);
    for(int i=0; i<stuff.length; i++)
        if(stuff[i] instanceof String) System.out.println("First three characters of "+ stuff[i] +" are "+((String)stuff).subString(0, 3));
    if(stuff[i] instanceof Integer) System.out.println(stuff[i]+" + 1 = "+(((Integer)stuff[i]).intValue()+1));
    Of course, that's rather inefficient. That's why we have interfaces. Let's say you had the following interface:
    public interface Drawable
        public void draw(int x, int y, Graphics g);
    }We'll assume that you have two classes, Block1 and Block2. That means you can do this:
    public void paint(Graphics g)
        Drawable[] items = new Drawable[2];
        items[0] = new Block1();
        items[1] = new Block2();
        for(int i=0; i<items.length; i++) items.draw(i*10, 0, g);
    Does that help?
    P.S. The forums converts brackets like stuff[ i ] into stuff<i>

  • Hi..............I Stuk up with a problem in JTree Please, help me any one..

    Hi,
    I created a JTree where the DefaultMutableTreeNode is created at run time and for this node, the leaf items are added dynamically.....Now my problem is, I should get a popup menu when i click on the treenode particularly-----
    * Node1
    -------Node11
    -------Child1
    In this I want a popup only for the Node11 & its child elements if there is any Node2 also i don't want a popup and also for Node1 i don't want, when I am trying this I am getting the Popup to all nodes and child elements, please, help me any one who cross over this problem.............
    Thanks In Advance,
    Regards,
    Sarikakiran

    Firstly, do you know how to detect the right click? (Use a MouseListener.)
    When you receive the MouseEvent for the right-click, you can get the coordinates of the click (i.e. where on the JTree the mouse was clicked).
    You can then use JTree.getPathForLocation to get the path for the clicked node, which is a TreePath object. Use TreePath.getLastPathComponent to find which node was actually clicked on.
    Having determined which node was clicked, you can then make a decision about whether to show a popup menu.

  • How To Work With Jtree Pls Help

    I heared about JTree but dont have idea to use it in form6i where i can get
    help or sample form can i use jtree in forms 6i

    JTree is a Java component - do you really mean JTree or do you just want to use the Forms Tree control.
    You'd need to check out the documentation on Java Beans in Forms which is on otn.oracle.com/products/forms
    For JTree documentation try googling...we don't have a JTree example on OTN that I am aware of.
    Regrads
    Grant

  • From JTree to Help panel events JTree Single leaf

    i'm a begginer and i really don't know how to give events for the JTree i watch an example of JTree where them create a method that create de the DefaultMutableTreeNode and givs a bookInfo or something like that the thing is that the event is node.isLeaf() that compromise all the leafs of the JTree and i want that i give a event for a single leaf to provide the help information in another panel
    JTree
    |
    |_____option
    |
    |_____option ========> to Jpanel
    it has to show the information like in an image or something like that but i cant do it i really don't know how to do it please help me how can i do this events for the leafs, not for all the leaf, i want an event for a single leaf.

    Try to express the problem again but use some new lines, full stops, etc. Using a), b), c), etc to separate out the points you are trying to make would help.
    So that when we try to read your problem it makes sense.

  • "digesting" protein strings - need vector help!

    Alright, here's my problem. I've got a GUI up and running to accept a long protein string, which will look like this (no breaks):
    G D V E K G K K I F V Q K C A Q C H T V E K G G K H K T G P N L H G L F G R K T G Q A P G F S Y T D A N K N K G I T W G E E T L M E Y L E N P K K Y I P G T K M I F A G I K K K G E R E D L I A Y L K K A T N E
    The gui offers a number of different proteases which are just things that cut the protein in certain places.
    For example, the protease Trypsin will cut the string after every K.
    What I need to do is write a method to cut the string after every K, save that sring to a vector, and continue with the remainder of the original protein string until there's no letters left.
    My problem is that I really don't know how to use vectors, and I don't know what this method should look like! Can anyone help? Thanks!

    if you want to keep the 'K' you will want to do this
    import java.util.*;
    public class Prot {
      public static void main(String[] args) {
        String str = "G D V E K G K K I F V Q K C A Q C H T V E K G G K H K T G P N L H G L F G R K T G Q A P G F S Y T D A N K N K G I T W G E E T L M E Y L E N P K K Y I P G T K M I F A G I K K K G E R E D L I A Y L K K A T N E";
        StringTokenizer stoken = new StringTokenizer (str, "K");
        List prots = new ArrayList();
        while (stoken.hasMoreTokens()) {
          String thisProt = stoken.nextToken();
          if (stoken.hasMoreTokens()) thisProt += "K";
          prots.add (thisProt);
          System.out.println("str = " + thisProt);
    // iterate the list
        Iterator it = prots.iterator();
        while (it.hasNext()){
          String dd = (String) it.next();
          System.out.println(dd);

  • Throws infinite-loop error after cycline through Vector - HELP!!!

    public void paint(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    xpos = (getSize().width - fm.stringWidth(((String[])parseVector.elementAt(kount))[1])) / 2;
    if (ypos >= getSize().height || ypos <= 0) {
    g.drawString("", 0, 0); // CLEAR THE APPLET
    ypos = getSize().height;
    if (kount <= parseVector.size()) {
    kount++;
    } else {
    kount = 0;
    g.drawString(((String[])parseVector.elementAt(kount))[1], xpos, ypos);
    class ScrollNews implements Runnable {
    public void run() {
    while (true) {
    ypos = ypos - 1;
    ParseNews.this.repaint();
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {}
    I don't know why this breaks after working one time through the Vector parseVector, what am I missing????
    Phil

    public void paint(Graphics g) {
    FontMetrics fm = g.getFontMetrics();
    xpos = (getSize().width -
    fm.stringWidth(((String[])parseVector.elementAt(kount))
    1])) / 2;
    if (ypos >= getSize().height || ypos <= 0) {
    g.drawString("", 0, 0); // CLEAR THE APPLET
    ypos = getSize().height;
    if (kount <= parseVector.size()) {
    kount++;
    } else {
    kount = 0;
    g.drawString(((String[])parseVector.elementAt(kount))[
    ], xpos, ypos);
    class ScrollNews implements Runnable {
    public void run() {
    while (true) {
    ypos = ypos - 1;
    ParseNews.this.repaint();
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {}
    I don't know why this breaks after working one time
    through the Vector parseVector, what am I missing????
    PhilI think your problem is here:
    while (true) {You have an infinite loop - how will the while loop ever be false?

  • Vector in Vector help

    Hi, I'm trying to make a 2 dimensional vector, and I'm having problems accessing the inside vector. Here's an example of what I'm trying to do:
    slist.firstElement().addElement(new Long(current));
    err, slist is a vector that holds another vector as its first element. I'm still a little iffy on the reference deals in java, if I set a vector equal to firstElement, change things with that first one, will it affect the one inside slist as well? Also, if I then reuse that name (the one outside the array), will that affect the list inside slist? The latter I don't think will, but better to be safe than sorry.
    Thanks!

    Hi, I'm trying to make a 2 dimensional vector, and I'm
    having problems accessing the inside vector. Here's an
    example of what I'm trying to do:
    slist.firstElement().addElement(new Long(current));that will work fine.
    err, slist is a vector that holds another vector as
    its first element. I'm still a little iffy on the
    reference deals in java, if I set a vector equal to
    firstElement, change things with that first one, will
    it affect the one inside slist as well? Also, if I
    then reuse that name (the one outside the array), will
    that affect the list inside slist? The latter I don't
    think will, but better to be safe than sorry.whenever you are dealing with references, and you have two references that are talking about the exact same object the changes you make via one reference will be visible by the other - it is very similar to the following:
    Vector v1 = new Vector();
    v1.addElement( "Foo" );
    Vector v2 = v1;
    v2.addElement( "Bar" );
    System.out.println( v1 ); // will print [Foo, Bar]v1 and v2 are references to the same object here, so a change via one results in a change to the other.

  • Raster to Vector help with Live Trace

    Hello All,
    When I convert a raster to vector with Live Trace the result is great but I want to reduce the number of points so I get less layers when I import it into Painter. Can anyone advise on how to do this?
    Rayne

    Jacob Bugge wrote:
    I know nothing about Painter. Is it from the same company as is Worst?
    Jacob...
    Shame! I would have thought that, at the very least,  you knew that Painter existed! Oh! Woe is the world, woe is the world!!

  • Plz Help...It is URGENT.JTREE

    Hi All:
    I am developing a Application where I am populating my Jtree from the database.Now the Problem is when I click on one of the Nodes of the Jtree,I am unable to populate the Jtree-window(along with the Jtree) with my JInternalFrame in one the Split Windows(SPlitPane is used).[i.e the left pane consists of Jtree & the right pane consists of JInternalFrame].
    How to activate and properly set the window.
    Help is urgent.Provide me some pointers plz/Source COde.
    Thanking you.
    Sincere
    Regards
    Ritesh

    Dear DrCLap:
    Thanks for the reply.
    Let me explain my problem.Deadline is there.
    Well I am having a Jtree(dynamically updated from Database) added to 1 Split Pane.
    I am having a Popup-Menu with items namely Add,View and close with the ActionListener Interface.
    When I click the LastNode of the JTree,I am firing a Popup Menu and the Add Event when I click Add.
    But after clicking the "Add",the Layout is completely occupying the Window and destroying the Jtree.,i.e.I am unable to see the JTree.And when I finally add a new node ,I am unable to refresh the JTree.
    Help me.
    Regards
    Ritesh

  • Create multiple root in a JTree??????very urgent..pl

    hello
    i have a jtree which takes a defaultMutableTreenode as its parameter.
    My problem is that i have to create multiple root.
    and what i get with this is a single root at the top.
    what is the solution for creating multiple roots in a jtree.
    pl. help its very urgent
    thanx
    hussain

    Hello Hussain,
    I found in the sourcecode of JTree the following constructor which might is be helpful for you:
    * Returns a <code>JTree</code> with each element of the specified
    * <code>Vector</code> as the
    * child of a new root node which is not displayed. By default, the
    * tree defines a leaf node as any node without children.
    * @param value a <code>Vector</code>
    * @return a <code>JTree</code> with the contents of the
    *          <code>Vector</code> as children of the root node
    * @see DefaultTreeModel#asksAllowsChildren
    public JTree(Vector value) {
    this(createTreeModel(value));
    this.setRootVisible(false);
    this.setShowsRootHandles(true);
    Greetings
    Helmut

  • JTree from Hashtable: weird ordering

    Need some help on this:
    I am making a JTree from a Hashtable:
    myJTree = new JTree(myHashtable);The elements are put in the hashtable in the right order, but they come out in the GUI all wrong, can't seem to see any logic behind the order.
    The children are not supposed to be sorted alphabetically, but in the order I get from an XML file. They are put in the Hashtable one by one.
    Any ideas?

    They are not sorted by key or value. But they are put in the hashtable in a specific order, the same order as they are in a XML file. And I need it to be in a data structure I can use to create a JTree.
    I tried to put the items in a vector. The order was then correct. The problem is that I then loose info (<key, value>) from the hashtable. And I cant get more than one level in the tree from the JTree(vector) constructor.
    So what do I do?
    To make it more clear how my tree looks like:
    ___Root object _nr1 (object in xml)
    |
    |________Child (first subcomponent in xmll)
    |
    |________Child (second subcomponent in xmll)
    |
    |________Child(third subcomponent in xmll)
    |
    etc.
    Its as simple as that

  • Read data from Vector object serialized in a file issue!!

    Hi all,
    I have two classes :CBase and CHelper class in a package and serializing some information in a file and then reading it. I am facing some problem while reading the data
    which was stored using vector of objects. Below is the detailed problem:::::
    ////////////CHELPER
    public class CHelper implements Externalizable, Cloneable {
    public string szname;
    //..other functions writeexternal read external
    ////////////CBASE
    public class CBase
    implements Externalizable, Cloneable {
    protected boolean bRead;
    protected boolean bWrite;
    protected Vector vData;
    public void addInstance(CHelper obj) {
    vData.addElement(obj);
    //..other functions writeexternal read external
    I am Serializing all the CBase information in a file and trying to read the contents from this serialized file::::
    CBase base = new CBase();
    CHelper obj = new CHelper();
    obj.setName("HELPER");
    base.addInstance(obj); // ADDING THE OBJECT DATA INTO THE VECTOR
    base.bRead = true;
    base.bWrite = true;
    // Writing to the file
    FileOutputStream fs = new FileOutputStream("d:\\temp\\Base.txt");
    ObjectOutputStream os = new ObjectOutputStream(fs);
    os.writeObject(base);
    os.close();
    //reading from the file
    FileInputStream fi = new FileInputStream("d:\\temp\\Base.txt");
    ObjectInputStream oi = new ObjectInputStream(fi);
    CBase read = (CBase) oi.readObject();
    PRINTING THE OUTPUT
    System.out.println("Vector = " + read.vData.firstElement().toString());
    System.out.println("Write = " + read.bRead);
    System.out.println("Read = " + read.bWrite);
    The output is something like:
    Vector = com.sp.CHelper@14b7453 // WHAT IS THIS ?? GARBAGE ?? NOT ABLE TO ANALYSE :(
    Write = true;
    Read = true;************
    MY PROBLEM:
    i.e How to get the information stored in the Vector ? I am getting something "com.sp.CHelper@14b7453 " whereas the expected output should be Vector = HELPER. I am
    writing and reading the vector as follows:
    // CHELPER
    public void writeExternal(ObjectOutput out) throws IOException {
    out.writeUTF(szName.toString());
    public void readExternal(ObjectInput in) throws IOException,
    ClassNotFoundException {
    setName(in.readUTF());
    Externalize in CBase Classs
    out.writeInt(getInstanceCount()); // getting number of objects in the vector
    for (int index = 0; index < getInstanceCount(); index++) {
    out.writeObject((CHelper)vData.get(index));
    out.writeBoolean(bWrite);
    out.writeBoolean(bRead);
    And reading like this::
    int iNumberofInstances; // number of instances
    iNumberofInstances = in.readInt();
    vData.clear();
    // Get the CHelper object and add it into the CFeatureBase vector
    for (int i = 0; i < iNumberofInstances; i++) {
    CHelper objbase = (CHelper)in.readObject();
    vData.add(objbase);
    this.bWrite = in.readBoolean();
    this.bRead = in.readBoolean();
    I suppose that i am correctly WRITING AND READING the data. B/w what can be the problem. All suggestions are welcome/
    Thanks,
    Rohit

    Vector = com.sp.CHelper@14b7453 // WHAT IS THIS ??
    GARBAGE ?? NOT ABLE TO ANALYSE :(
    Write = true;
    Read = true;************
    MY PROBLEM:
    i.e How to get the information stored in the Vector
    ? I am getting something "com.sp.CHelper@14b7453 "
    whereas the expected output should be Vector =
    HELPER.This is perfectly normal behaviour if your CHelper class doesn't override the toString() method. The default toString() method in Object just prints the name of the class and a hex representation of the object's hash code.
    Add a toString() method to CHelper that returns szname and you should see what you are expecting.

  • Change font size of JTree nodes dynamically

    hi,
    I have a requirement to change the font size of nodes in a JTree as and when the user wants.
    I have implemented a custom renderer for the JTree, but right now i have a fixed font size for all the nodes.
    How can I change the font size of JTree at runtime??
    thanks.

    I haven't done this, but it seems like
    public Component getTreeCellRendererComponent(...Object value,...) {
    Component c = super.getTreeCellRendererComponent(...);
    if (value!=null &&
    value instanceof(String) &&
    ((String)value).startsWith("A")) {
    c.setFont(bigFont);
    else c.setFont(littleFont);
    }It seems that this code is just making the font size of nodes begining with "A" bigger and the rest smaller.
    It doesn't solve my problem.
    I want to have something like a font chooser by which user can choose the font size they want for the JTree.
    any help ??

  • JTree adding nodes

    Hello all,
    I have two questions concerning Trees,
    1. I have created the Tree by the JTree(Vector) method, initially the vector does have some elements but i have to add some more later, so when my vector gets updated how can I add the same element to the tree in the form of a node?
    2. The Tree being created like this, has automatically generated names for the nodes (i guess it shows the memory address) but i want to change the name of all of my nodes, what is the method for renaming nodes?

    There are two pieces of Trees that you have not yet found. The first is the TreeModel, which is accessible through myTree.getModel(). You can use this model to create the data structure; the JTree object is simply a view of that model.
    The second piece is the TreeCellRenderer, accessible through myTree.getCellRenderer(). This renderer is used to draw the nodes of the tree and, by default, one is used that renders the Strings returned by the toString() method of the objects in the model. To display different strings, you can either (1) replace the renderer or (2) override the toString() method on the objects in your model. The first is recommended if the strings vary radically from the desired result of toString() or if toString() is being used for something else; the second is recommended for simplistic models or for situations in which the display properly reflects the nature of the object.
    Replacing the renderer also allows you to use any JComponent for rendering you wish. This can give you the ability to present gradient backgrouns, dynamically loaded icons, etc.
    Many objects in Swing use this format in which there are three portions of an object: the view (which is the standard object, like an instance of JTree, JList, or JTable), the model (which contains the actual data structure), and the renderer (which acts like a rubber stamp to draw the objects without causing more than one object to be instantiated to render thousand-element lists).

Maybe you are looking for

  • Disk Utility Error-Please Help

    I tried to repair my hard disk. I couldn't repair straight away as the repair button was greyed out so I varified the HD. The message came up: *Disk Utility* *First Aid* *Disk Utility stopped verifying "Macintosh HD" because the following error was e

  • Finder dies when opening Applications

    Whenever I open Applications, regardless of which user account I'm logged in as, the Finder dies. Starting apps from the doc does this too. Some of them will actually start up, some won't, but in any event the finder dies and restarts. Help! Thanks,

  • My Iphone keeps coming out of recovery mode before Im done

    anyone know how to make your iphone stay in recovery mode till its done restoring

  • BC4J, uiXML and SSO

    Can someone please point me in the direction of some documentation on how to integrate SSO with a uiXML/BC4J project ? Can you achieve the same logon page as portal where users can also change their password ? Also, if using Jakarta Struts, does the

  • Cant get open nat type for PS3, it use to be but wont go back

    im on Fios with the verizon router. I had the DMZ set to an ip with the PS3 also on it. the nat type was open and all was ok. I dont know how long its been this way (guessing not long because I have sucked at COD this week!) but I noticed im not at m