7 Replies - 465 Views - Last Post: 11 January 2013 - 10:36 AM Rate Topic: -----

#1 brendar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-January 13

Jtable - reading from file and splitting data

Posted 10 January 2013 - 12:22 PM

I know how to read text from a file line by line. But I would like to get data from a file to the Jtable using a better method such as a for loop or a while loop. Basically, the jtable has 4 columns. Name, Hotel, Cost, Country. I want the jtable to read every four lines into each row.

filename.txt
----------
Ramal
Hotel1
100
UK
Bren
Hotel2
200
Spain
Dave
Hotel3
300
France


For example, first row should look like this:

Ramal Hotel1 100 UK



This is what i have done so far.




button3.addActionListener(new ActionListener(){
public  void actionPerformed(ActionEvent e){

		try{
		
		FileInputStream fstream = new FileInputStream("filename.txt");
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));	
        
        pane.setVisible(true);
        
        String line1 = br.readLine();
		String line2 = br.readLine();
		String line3 = br.readLine();
		String line4 = br.readLine();
		String line5 = br.readLine();
		String line6 = br.readLine();
		String line7 = br.readLine();
		String line8 = br.readLine();
		
			
		String a = (line1.toString());
		String b = (line2.toString());
		String c = (line3.toString());
		String d = (line4.toString()); 
		combobox1.getSelectedItem();
		String e = (line5.toString());
		String f = (line6.toString());
		String g = (line7.toString());
		String h = (line8.toString()); 
		combobox1.getSelectedItem();

      	        String[] data1 = {(a), (B)/>/>/>/>/>/>, (c), (d)};
      	        String[] data2 = {(e), (f), (g), (h)};

      	
      	        model.addRow(data1);
      	        model.addRow(data2);
      	
		in.close();
		
		}catch (Exception e){
			
		System.err.println("Error: " + e.getMessage());
		
		}

   	}
       });




Is This A Good Question/Topic? 0
  • +

Replies To: Jtable - reading from file and splitting data

#2 tango185  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-January 13

Re: Jtable - reading from file and splitting data

Posted 10 January 2013 - 12:33 PM

What's the purpose of the combobox1 in your code?
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Jtable - reading from file and splitting data

Posted 10 January 2013 - 12:38 PM

I do not understand why you are not using a loop

Your variable names dont make much sense

Again, your code would be so much simpler with a loop
DefaultTableModel model = (DefaultTableModel) table.getModel();

try {
	FileInputStream fstream = new FileInputStream("filename.txt");
	BufferedReader reader = new BufferedReader(fstream); 
	
	while (reader.ready()) { //While we have content to read
		String[] data = { reader.readLine(), reader.readLine(), reader.readLine(), reader.readLine() };
		model.addRow(data);
	}
	
	reader.close();
}
catch (IOException e) {
	System.err.println("Error: " + e.getMessage());
}


Was This Post Helpful? 3
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: Jtable - reading from file and splitting data

Posted 10 January 2013 - 12:59 PM

Use a Scanner and fill a DefaultTableModel
Was This Post Helpful? 0
  • +
  • -

#5 brendar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 10-January 13

Re: Jtable - reading from file and splitting data

Posted 10 January 2013 - 02:01 PM

CasiOo Thank you so so much. It worked perfectly. :)

Can i ask another question regarding another piece of code?

Basically, the code below allows the user to click on any row on the table to edit it. But the code below edits the item and saves it in the file as a new item. I would like to remove the old item and place the updated item on the same lines.



button9.addActionListener(new ActionListener(){
public  void actionPerformed(ActionEvent e){

		try{
		  BufferedWriter fileOut = new BufferedWriter(new FileWriter("filename.txt", true)); 
		  
		  fileOut.write(textfield1.getText());
		  fileOut.newLine(); 
		  fileOut.write(textfield2.getText());
		  fileOut.newLine(); 
		  fileOut.write(textfield3.getText());
		  fileOut.newLine();  
		  fileOut.write(combobox1.getSelectedItem().toString());
		  
		  fileOut.close(); 
		  
		  JOptionPane.showMessageDialog(null, "Details Updated!");
		}
		catch (IOException ioe)
		{
		  
		}
	
	}
	});



Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1414
  • View blog
  • Posts: 3,136
  • Joined: 05-April 11

Re: Jtable - reading from file and splitting data

Posted 10 January 2013 - 02:25 PM

You are changing the data in the tablemodel, arent you? At least that is what you should be doing in my opinion

The easiest solution is to just grab the whole tablemodel, and overwrite all of the data in the file with the new data :) this way you would not have to search for the place where you need to replace the text
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Jtable - reading from file and splitting data

Posted 11 January 2013 - 10:27 AM

You missed the concept here. JTable do not store data.
JTable are just a fancy GUI component that display the data contained in its model.
So when you change the model, the displayed data is changed. You responsability to change/update/modify the model the way you want storing the information the way you want (array. file, database, ...)

http://www.dreaminco...h-a-tablemodel/
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2816
  • View blog
  • Posts: 11,924
  • Joined: 20-September 08

Re: Jtable - reading from file and splitting data

Posted 11 January 2013 - 10:36 AM

Quote

The easiest solution is to just grab the whole tablemodel, and overwrite all of the data in the file with the new data
That's true. An alternative could be to use TableModelListener events to write to fixed length records in a random access file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1