Results 1 to 3 of 3
  1. #1
    legend101z is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default How to read my txt file to my jtable?

    i able to write my jtable to a txt file just fine but i not sure how i suppose to read it back into the jtable.
    This is what the data look like when i write to a text file

    First Name: lee
    Last Name: jim
    Phone Number: (834) 794-7329
    Email: lee_r@wow.com


    this is what i use to save the jtable info as a text file.


    Java Code:
    savecontact.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				 
    				JFileChooser filesave = new JFileChooser();
    				FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT File", ".txt", "text");
    				filesave.setFileFilter(filter);
    				int returnVal = filesave.showSaveDialog(Main.this);
    				
    				    if (returnVal == JFileChooser.APPROVE_OPTION) {
    				        try {
    				        	
    				        
    				        	File file = filesave.getSelectedFile();
    				        	
    				            PrintWriter os = new PrintWriter(file +".txt");
    				            
    
    				            for (int row = 0; row < table.getRowCount(); row++) {
    				                for (int col = 0; col < table.getColumnCount(); col++) {
    				                    os.print(table.getColumnName(col));
    				                    os.print(": ");
    				                    os.println(table.getValueAt(row, col));
    				                
    				                }
    				            
    				                
    
    				                os.println("");
    				            }
    				            os.close();
    				            System.out.println("Done!");
    				        } catch (IOException e1) {
    				            // TODO Auto-generated catch block
    				            e1.printStackTrace();
    				        
    				    }
    				}
    			}
    		});
    this is my table im using.
    Java Code:
    table = new JTable();
    		table.setBorder(UIManager.getBorder("DesktopIcon.border"));
    		scrollPane.setViewportView(table);
    		
    		// the column in the table
    		table.setModel(new DefaultTableModel(
    			
    				new Object[][] {
    			},
    			new String[] {
    				"First Name", "Last Name", "Phone Number", "Email"
    			}
    		));

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: How to read my txt file to my jtable?

    You're using a DefaultTable model; read its API documentation and see how you can add new values to it or modify them or delete them. Your table can give you the DefaultTable method itself ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

Similar Threads

  1. how to embed combobox,checbox in JTable read write to database
    By scorpion.poison in forum New To Java
    Replies: 4
    Last Post: 08-30-2012, 10:52 AM
  2. How to import(read) a data file into jtable
    By leszer in forum NetBeans
    Replies: 1
    Last Post: 06-04-2012, 05:16 AM
  3. Read file in jtable
    By victoryo in forum New To Java
    Replies: 2
    Last Post: 01-11-2012, 12:19 PM
  4. JTable. make it read what's in the box
    By Maraxus in forum AWT / Swing
    Replies: 6
    Last Post: 09-18-2010, 01:41 PM
  5. Replies: 0
    Last Post: 02-11-2009, 09:53 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
  •