Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Writing data to the java DefaultTableModel

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Thanked 0 Times in 0 Posts

    Default Writing data to the java DefaultTableModel

    Hello all,
    I am attempting to write a program that will read the characters of a text file one by one and keep the count of the number of times each character appears. I would like to use the table data structure "DefaultTableModel"
    I would like the table to appear like this

    Character/ Frequency
    a 10
    b 5
    c 7

    However I am not sure how to use the DefaultTableMode.setColumnIdentifiers method since the identifiers can incerease as the file is read. Here is some of my code

    public DefaultTableModel readFile(File filename) throws IOException
        BufferedReader reader = new BufferedReader(new FileReader(filename));
        DefaultTableModel myDefaultTableModel = new DefaultTableModel(1,1);
        String line  = null;
        int i  = 0;
    	    	line = reader.readLine();
    	    	readString(line, myDefaultTableModel, i);
    	    //Close reader object
            return myDefaultTableModel;
    private void readString(String lineString, DefaultTableModel myDefaultTableModel, int i)
    			Object testObject;
    	    	StringTokenizer tokenizer = new StringTokenizer(lineString);
    	        String token = tokenizer.nextToken();
    	        for (int stringCount = 0; stringCount < token.length(); stringCount++)
    	        myDefaultTableModel.setValueAt(token.charAt(stringCount), 0, 0);
    	        testObject = myDefaultTableModel.getValueAt(0, 0);

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 844 Times in 786 Posts
    Blog Entries

    Default Re: Writing data to the java DefaultTableModel

    It might be easier to create a temporary data holder, then create the DefaultTableModel based upon that. One way to do this would be to create a HashMap (keyed with your String and valued with an integer representing the count). Fill up this HashMap as you read the file...then create the TableModel from the map by iterating over its keys and values, filling up the TableModel as you go. Just a suggestion.

Similar Threads

  1. How to start writing Java Apps for Cell Phones?
    By Jchang504 in forum Java ME (Mobile Edition)
    Replies: 7
    Last Post: January 10th, 2011, 04:11 AM
  2. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  3. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  4. Help writing some Scanner code
    By bChEos in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 3rd, 2010, 03:27 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM