Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    15
    Rep Power
    0

    Default Printing Tree Structure using Swings

    hi friends i am pradeep. i want to print a text file as a tree using swings .i wrote a code for that and i am getting only one node after the root node .so i am sending the code here .plz help me anybody .
    here is the code :

    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import javax.swing.JScrollBar;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;
    import javax.swing.JTree;
    import javax.swing.JButton;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.event.TreeSelectionListener;

    class Hierarchy extends JFrame
    {
    public static void main(String args[])
    {
    JFrame frame = new JFrame("DefaultMutableTreeNode Example");
    JButton b=new JButton("search");
    //JButton exit=new JButton("Exit");
    JTextField t1,textField;
    JTree tree=new JTree();
    Container container = frame.getContentPane();
    JTextArea textArea=new JTextArea();
    JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 500);
    JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 400);

    t1=new JTextField(15);
    textField = new JTextField();

    JPanel panel=new JPanel();
    panel.add(t1);
    panel.add(b);
    panel.add(textArea);
    //panel.add(exit);
    frame.add(panel);
    frame.add(hbar, BorderLayout.SOUTH);
    frame.add(vbar, BorderLayout.EAST);
    //frame.add(jsp);
    //frame.add(textArea);
    container.add(panel,BorderLayout.NORTH);


    try {
    String item_no;
    String input = "";
    File fin=new File("C:\\Pradeep\\u.txt");
    FileReader fr=new FileReader(fin);
    BufferedReader br = new BufferedReader(fr);

    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode node1 = null;
    DefaultMutableTreeNode node2 = null;
    DefaultMutableTreeNode node3 = null;
    DefaultMutableTreeNode node4 = null;

    parent = new DefaultMutableTreeNode("UNSPSC");

    while((input = br.readLine()) != null)
    {
    StringTokenizer st = new StringTokenizer(input," ");
    System.out.println(" String "+ input);
    item_no = st.nextToken();
    System.out.println(" item_no -> " + item_no);

    if(item_no.substring(2).equals("000000"))
    {
    node1 = new DefaultMutableTreeNode(input);
    System.out.println(" 1 root node"+ input);
    parent.add(node1);
    tree = new JTree(parent);
    frame.add(tree);

    }
    else if(item_no.substring(4).equals("0000"))
    {
    node2 = new DefaultMutableTreeNode(input);
    System.out.println(" 2 root node" + input);
    node1.add(node2);
    tree = new JTree(node1);
    frame.add(tree);
    }

    else if(item_no.substring(6).equals("00"))
    {
    node3 = new DefaultMutableTreeNode(input);
    System.out.println(" 3 root node"+ input);
    node2.add(node3);
    tree = new JTree(node2);
    frame.add(tree);
    }
    else
    {
    node4 = new DefaultMutableTreeNode(input);
    System.out.println(" 4 root node"+ input);
    node3.add(node4);
    tree = new JTree(node3);
    frame.add(tree);
    }
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(tree);
    splitPane.setRightComponent(textField);
    container.add(splitPane, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
    frame.add(splitPane);
    frame.setSize(700,700);
    frame.setVisible(true);
    }
    }catch(IOException e) { System.out.println(e);}
    }
    }


    so kindly please help me in this problem .i hope i ll get replay .:p:):eek:

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    i am getting only one node after the root node
    Can you post the output from program? And add some coments on what you expected.
    What does the input file look like? How is it to be processed by the program?
    There are NO comments in the program so I can't understand what it is YOU are trying to do.

  3. #3
    Join Date
    Jun 2008
    Posts
    15
    Rep Power
    0

    Default

    Quote Originally Posted by pradeep1_mca@yahoo.com View Post
    hi friends i am pradeep. i want to print a text file as a tree using swings .i wrote a code for that and i am getting only one node after the root node .so i am sending the code here .plz help me anybody .
    here is the code :

    import java.io.*;
    import java.util.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.AdjustmentEvent;
    import java.awt.event.AdjustmentListener;
    import javax.swing.JScrollBar;
    import javax.swing.*;
    import javax.swing.tree.*;
    import javax.swing.event.*;
    import javax.swing.JTree;
    import javax.swing.JButton;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.event.TreeSelectionListener;

    class Hierarchy extends JFrame
    {
    public static void main(String args[])
    {
    JFrame frame = new JFrame("DefaultMutableTreeNode Example");
    JButton b=new JButton("search");
    //JButton exit=new JButton("Exit");
    JTextField t1,textField;
    JTree tree=new JTree();
    Container container = frame.getContentPane();
    JTextArea textArea=new JTextArea();
    JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 500);
    JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 400);

    t1=new JTextField(15);
    textField = new JTextField();

    JPanel panel=new JPanel();
    panel.add(t1);
    panel.add(b);
    panel.add(textArea);
    //panel.add(exit);
    frame.add(panel);
    frame.add(hbar, BorderLayout.SOUTH);
    frame.add(vbar, BorderLayout.EAST);
    //frame.add(jsp);
    //frame.add(textArea);
    container.add(panel,BorderLayout.NORTH);


    try {
    String item_no;
    String input = "";
    File fin=new File("C:\\Pradeep\\u.txt");
    FileReader fr=new FileReader(fin);
    BufferedReader br = new BufferedReader(fr);

    DefaultMutableTreeNode parent;
    DefaultMutableTreeNode node1 = null;
    DefaultMutableTreeNode node2 = null;
    DefaultMutableTreeNode node3 = null;
    DefaultMutableTreeNode node4 = null;

    parent = new DefaultMutableTreeNode("UNSPSC");

    while((input = br.readLine()) != null)
    {
    StringTokenizer st = new StringTokenizer(input," ");
    System.out.println(" String "+ input);
    item_no = st.nextToken();
    System.out.println(" item_no -> " + item_no);

    if(item_no.substring(2).equals("000000"))
    {
    node1 = new DefaultMutableTreeNode(input);
    System.out.println(" 1 root node"+ input);
    parent.add(node1);
    tree = new JTree(parent);
    frame.add(tree);

    }
    else if(item_no.substring(4).equals("0000"))
    {
    node2 = new DefaultMutableTreeNode(input);
    System.out.println(" 2 root node" + input);
    node1.add(node2);
    tree = new JTree(node1);
    frame.add(tree);
    }

    else if(item_no.substring(6).equals("00"))
    {
    node3 = new DefaultMutableTreeNode(input);
    System.out.println(" 3 root node"+ input);
    node2.add(node3);
    tree = new JTree(node2);
    frame.add(tree);
    }
    else
    {
    node4 = new DefaultMutableTreeNode(input);
    System.out.println(" 4 root node"+ input);
    node3.add(node4);
    tree = new JTree(node3);
    frame.add(tree);
    }
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    splitPane.setLeftComponent(tree);
    splitPane.setRightComponent(textField);
    container.add(splitPane, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
    frame.add(splitPane);
    frame.setSize(700,700);
    frame.setVisible(true);
    }
    }catch(IOException e) { System.out.println(e);}
    }
    }


    so kindly please help me in this problem .i hope i ll get replay .:p:):eek:
    OUT PUT OF THE ABOVE PROGRAM WHAT I GETTING IS :
    UNSPSC
    10000000 lIVE PLANT AND ANIMAL MATERIAL .
    10100000 LIVE ANIMALS
    LIKE THIS IT SHOULD DISPALY TOTAL DATA FROM TEXT FILE UPTO
    400000000
    I ADDED CODES LIKE 1000000 TO 40000000

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    What does the input file look like?
    How is it to be processed by the program?
    I don't understand your comments at the end of your post mean.
    Some of it is output from the program and some of it was typed in by you. All in UPPERCASE???

    Please show a simple sample input file and then the results from your program with reading that file and then show what you want the output to look like.

  5. #5
    Join Date
    Jun 2008
    Posts
    15
    Rep Power
    0

    Default Searching a word from a text file .

    hi senior member . requirements for my task as follows :
    1.print the tree structure .i got it .
    2.i have to take TextField and Button .
    3.if i enter any word in TextField and click the Button named as search .then it should search from the text file and print the exact matching from the tree
    in a TextArea .

    Text File ex: u.txt
    so this is my requirement and i am unable to send the output.
    still ur not understand what i posting to u.plz do one thing .

    i want a program for searching using swings .means
    i want to enter one word into tetxtfield and click the search button .
    then it should search and print the values in textarea.so u take anyone text file and save it as .txt .
    then u can write the code to get data from that text file using Button named search .

    i hope u ll understand this .i am waiting for ur replay .

    if u can not understand this ,plz ignore this mail .

    bye

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

Similar Threads

  1. Tree structure using JAVA
    By trill in forum Advanced Java
    Replies: 2
    Last Post: 05-27-2010, 11:02 AM
  2. Drawing charts using swings
    By ravindarjobs in forum New To Java
    Replies: 19
    Last Post: 07-21-2009, 11:03 PM
  3. New to Java, Creating screens with swings on Mac OS X
    By mo_mughrabi in forum New To Java
    Replies: 0
    Last Post: 05-26-2008, 01:17 PM
  4. java swings
    By emperoraj in forum AWT / Swing
    Replies: 0
    Last Post: 03-26-2008, 11:50 AM
  5. Use if then else structure, help
    By paul in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •