CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    1

    Loading my text file into my jtable

    Ok i was able to figure out how to save the data in my jtable as text file but im not sure how i suppose to load it.
    I've been trying to figure this out for few day now and i still lost on how to go about it.

    this is the code I have for saving my jtable
    when i save my code the output in the text file look like this:



    First Name: dan
    Last Name: ram
    Phone Number: (342) 423-4324
    Email: email@live.com


    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.close();
    				            System.out.println("Done!");
    				        } catch (IOException e1) {
    				            // TODO Auto-generated catch block
    				            e1.printStackTrace();
    				        
    				    }
    				}
    			}
    		});

    this is my table
    Code:
    table.setModel(new DefaultTableModel(
    			
    				new Object[][] {
    			},
    			new String[] {
    				"First Name", "Last Name", "Phone Number", "Email"
    			}
    		));

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Loading my text file into my jtable

    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured