File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JAVA Swing JTree and ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JAVA Swing JTree and ArrayList" Watch "JAVA Swing JTree and ArrayList" New topic
Author

JAVA Swing JTree and ArrayList

Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Can we create a tree using 2d ArrayList...
Let us assume that there is a 2-D ArrayList<String> type created we have to show the 1st element of each row as a node and next elements as its child.....
can we do it...???Please help me out.....
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Rachit Vaish wrote:Let us assume that there is a 2-D ArrayList<String>

What's a '2-D ArrayList<String>'? Did you mean List<List<String>>?


luck, db
There are no new questions, but there may be new answers.
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Darryl Burke wrote:
Rachit Vaish wrote:Let us assume that there is a 2-D ArrayList<String>

What's a '2-D ArrayList<String>'? Did you mean List<List<String>>?


its not a List it is ArrayList like...............



import java.util.ArrayList;

class Main{
public static void main(String []a){

ArrayList<String> trfRgnsList=new ArrayList<String>();
trfRgnsList.add("A");
trfRgnsList.add("B");

ArrayList<ArrayList<String>> intrf=new ArrayList<ArrayList<String>>();
int count=trfRgnsList.size();
for(int i=0;i<count;i++)
{
intrf.add(new ArrayList<String>());
((ArrayList)intrf.get(i)).add(trfRgnsList.get(i));
((ArrayList)intrf.get(i)).add("1");
((ArrayList)intrf.get(i)).add("2");
}
System.out.println("Values----<>>>>>"+intrf);
}
}
now i want to represent A and B as a node and 1 and 2 as their child. Can we do it.....?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> now i want to represent A and B as a node and 1 and 2 as their child. Can we do it.....?

yes.

create a DefaultMutableTreeNode root node (name it whatever you want)

in your for loop:
create a parent node from 'trfRgnsList.get(i)' (same as you're adding to intrf.get(i))
create 2 child nodes:
new DefaultMutableTreeNode(((ArrayList)((ArrayList)intrf.get(i))).get(1));// and get(2);
add both child nodes to the parent
add the parent to the root

now it's just
frame.add(new JScrollPane(new JTree(root)));
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Rachit Vaish wrote:
Darryl Burke wrote:
Rachit Vaish wrote:Let us assume that there is a 2-D ArrayList<String>

What's a '2-D ArrayList<String>'? Did you mean List<List<String>>?


its not a List it is ArrayList

ArrayList is-a List.
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Darryl Burke wrote:
Rachit Vaish wrote:
Darryl Burke wrote:
Rachit Vaish wrote:Let us assume that there is a 2-D ArrayList<String>

What's a '2-D ArrayList<String>'? Did you mean List<List<String>>?


its not a List it is ArrayList

ArrayList is-a List.
Thanks for this info.....
Rachit Vaish
Greenhorn

Joined: Feb 10, 2012
Posts: 9
Michael Dunn wrote:> now i want to represent A and B as a node and 1 and 2 as their child. Can we do it.....?

yes.

create a DefaultMutableTreeNode root node (name it whatever you want)

in your for loop:
create a parent node from 'trfRgnsList.get(i)' (same as you're adding to intrf.get(i))
create 2 child nodes:
new DefaultMutableTreeNode(((ArrayList)((ArrayList)intrf.get(i))).get(1));// and get(2);
add both child nodes to the parent
add the parent to the root

now it's just
frame.add(new JScrollPane(new JTree(root)));


Thank you very much.........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAVA Swing JTree and ArrayList