Results 1 to 2 of 2

Thread: JTable Help

  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default JTable Help

    Hello,

    I am having trouble getting the JTable to show. When I run the code, i don't get a table at all. I am not sure why this is.

    Any help is much appreciated.

    data.txt contains:
    Last Name = Doe
    First Name = John
    Age = 21
    ID Number = 011234
    Serial = 123456

    The code is meant to check a directory containing a series of files each named "data.txt". Each file is checked for "123456" and once it is found, the last name, first name, age and Id number should be displayed in a Jtable.

    I have no idea why this is not working.
    Please help.

    thanks in advance for your help.


    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    import javax.swing.*;
    import javax.swing.table.*;
     
    public class TableProcessing extends JFrame
    {
        JTable table;
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<Item> data = new ArrayList<Item>();
        public TableProcessing()
        {
            ArrayList<String> columnNames = new ArrayList<String>(4);
            columnNames.add("Last Name");
            columnNames.add("First Name");
            columnNames.add("Age");
            columnNames.add("ID Number");
            
            traverse(new File("//Root/dir"));
            TableModel model = new ItemTableModel(stringsToItems(list), columnNames);
            table = new JTable( model );
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
    
            JScrollPane scrollPane = new JScrollPane( table );
            getContentPane().add( scrollPane );
        }
    
      public void traverse(File dir) {
                if (!dir.exists()) {
                    System.out.println("Does not exist " + dir);
                }
    
                if (dir.isDirectory()) {
                    File[] children = dir.listFiles();
    
                    for (int i = 0; i < children.length; i++) {
                        traverse(children[i]);
                    }
                } 
                else if (dir.getName().matches("data.txt")) {
        	      if (find(dir, "123456")) {
        		System.out.println(dir.getParentFile());
        		if (dir.getParentFile().exists()){
        		
        			ScannerReadFile(dir.getName());
        		}
        	    }
           }
        }    
        
        
        public boolean find(File f, String searchString) {
            boolean result = false;
            Scanner in = null;
            try {
                in = new Scanner(new FileReader(f));
                while(in.hasNextLine() && !result) {
                    result = in.nextLine().indexOf(searchString) >= 0;
                    
                }
            }
            catch(IOException e) {
                e.printStackTrace();      
            }
            finally {
                try { in.close() ; } catch(Exception e) { /* ignore */ }  
            }
            return result;
        }
       
    
     
    
    public void ScannerReadFile(File f) {
    	Scanner scanner = null;
            try {
    
                //
                // Create a new Scanner object which will read the data from the 
                // file passed in. To check if there are more line to read from it
                // we check by calling the scanner.hasNextLine() method. We then
                // read line one by one till all line is read.
                //
                scanner = new Scanner(f);
    
                while (scanner.hasNextLine()) {
                    String nl = scanner.nextLine();
    
                    if (parseString(nl) != null) {
                        // output += parseString(nl);
    		    list.add(parseString(nl));
    		  
    		}
                  
    	    }
               
    
    	 //   System.out.println(list);
    	    System.out.println();
    
    	    for (int i = 0; i < list.size(); i++) {
    		//System.out.println(list.get(i));
    	    }
    
    	   // System.out.println(list.size());
    
    
    	    //System.out.println(list);
    	} catch (FileNotFoundException e) {
    	    e.printStackTrace();
    	}
    	finally {
    	    scanner.close();
    	}
        }
        
        private String parseString(String line) {
            
            if (line.contains("Last Name") || line.contains("First Name") || line.contains("Age")|| line.contains("ID Number")) {
            
                line = line.substring(line.indexOf("=") + 1, line.length());
                //return line + "\n";
                return line;
               
            	
            } else {
                return null;
            }
        } 
        
        public static void main(String[] args)
        {
        	
            TableProcessing frame = new TableProcessing();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        }
    
    
    }
    
    
    
     	 private List<Item> stringsToItems(List<String> strings) {
        	List<Item> result = new ArrayList<Item>(strings.size() / 4);
        	Iterator<String> i = strings.iterator();
        	while(i.hasNext()) {
        	    result.add(new Item(i.next(),i.next(), i.next(), i.next()));
        	   
        	    
        	}
        	// System.out.println(result);
        	return result;
        	
     	 }
        
     
        class Item
        {
            String lastName;
            String firstName;
            String Age;
            String idNum;
     
            public Item(String lastName, String firstName, String Age, String idNum)
            {
                this.lastName = lastName;
                this.firstName = firstName;
                this.Age = Age;
                this.idNum = idNum;
            }
        }
     
        class ItemTableModel extends AbstractTableModel
        {
            List data;
            List columnNames;
     
            public ItemTableModel(List data, List columnNames)
            {
                this.data = data;
                this.columnNames = columnNames;
            }
     
            public String getColumnName(int col) { return columnNames.get(col).toString(); }
     
            public int getRowCount() { return data.size(); }
     
            public int getColumnCount() { return columnNames.size(); }
     
            public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); }
     
            public boolean isCellEditable(int row, int column)
            {
                //if (column == 3)
                    //return false;
               // else
                    return false;
            }
     
            public Object getValueAt(int row, int column)
            {
                Item item = (Item)data.get( row );
     
                switch ( column )
                {
                    case 0: return item.lastName;
                    case 1: return item.firstName;
                    case 2: return item.Age;
                    case 3: return item.idNum;
                }
     
                return null;
            }
     
            public void setValueAt(Object value, int row, int column)
            {
                Item item = (Item)data.get( row );
     
                switch ( column )
                {
                    case 0:
                        item.lastName = (String)value;
                        break;
                    case 1:
                        item.firstName = (String)value;
                        break;
                    case 2:
                        item.Age = (String)value;
                        break;
                    case 3:
                        item.idNum = (String)value;
                        break;
                }
     
                fireTableCellUpdated(row, column);
     
                        }
        }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,984
    Rep Power
    9

    Default

    Break your problem up into smaller pieces. You have at least two separate issues here: reading data from a file (and storing it into dummy variables), and creating a JTable (with dummy data to display).

    Create an individual SSCCE for each, and it will be easier to see where your actual problem is.

Similar Threads

  1. jtable
    By bonjovi4u in forum AWT / Swing
    Replies: 3
    Last Post: 08-31-2010, 03:20 PM
  2. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  3. JTable help
    By FFEJ in forum New To Java
    Replies: 5
    Last Post: 10-05-2009, 11:30 PM
  4. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  5. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 12:26 PM

Tags for this Thread

Posting Permissions

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