Reading txt files to JTable


I'm having troubles reading data from a txt file and implementing it into a JTable. I created a demo system as a test (using text field instead of JTable to show information by clicking a button) and it was a success, I managed to create a txt file and the text field would read it and display the information. But doing the same task with a JTable doesn't seem as simple.

Below is the code I used for creating the txt file.

private void EnterNewCarButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        DefaultTableModel model = (DefaultTableModel) CarDetailsTable.getModel();
        String carID = CarIDTextField.getText().toString();
        String carMandM = CarMandMTextField.getText().toString();
        String carColor = CarColorTextField.getText().toString();
        String carLicensePlate = CarLicensePlateTextField.getText().toString();
        String carEngineModel = CarEngineModelTextField.getText().toString();
        if((!CarIDTextField.getText().trim().equals("") || 
                && !(CarColorTextField.getText().trim().equals("") ||
                && !(CarEngineModelTextField.getText().trim().equals("")))
            model.addRow(new Object[]{
                CarIDTextField.getText() ,CarMandMTextField.getText(),
        else if (carLicensePlate.length()<=6)
            ErrorMessage.setText("LicnsePlate Character too short.");
              ErrorMessage.setText("Text Fields Should not be left Blank");  
        try (FileWriter cars = new FileWriter ("Cars.txt", true)) {            
            cars.write("CarID: " + carID);
            cars.write("Car Make and Model :" + carMandM);
            cars.write("car Color: " + carColor );
            cars.write("Car License Plate: "+ carLicensePlate);
            cars.write("Car Engine Model: "+ carEngineModel);
            JOptionPane.showMessageDialog(rootPane,"Car has been added!");
        } catch (Exception e) {
           JOptionPane.showMessageDialog(null, "Fill in all Text Field", "Login Error", JOptionPane.ERROR_MESSAGE);


Making the TextField read from a txt file was quite simple, making a JTable isn't as straight forward. I went to table events and clicked on propertychange since that makes the most since and inserted the same code I used for the TextField but there is an error on line 6. When I siwtched it out for the textfield variable name it worked fine. Did I choose the wrong events or do I have to do something else entirely.

  private void CarDetailsTablePropertyChange(java.beans.PropertyChangeEvent evt) {                                               
         try {
            FileReader read = new FileReader("Cars.txt");
            BufferedReader br = new BufferedReader(read);
            //, read);
        catch(Exception e){
            JOptionPane.showMessageDialog(rootPane,"No feedback has been received");


I'm pretty sure its something wrong with this line of code. But I don't know what to do about it. When I remove the comments a red error line show up under "read", read);


A method that you aren't showing is giving off an error message that you're not saying what it is

You're missing some essential information here

It usually helps a lot if the problem is described to the point that it can be easily reproduced (which would therefore include code that can be executed) and it also helps to only include the part that is giving you trouble - to exclude everything that's not directly related

Anyway, it sounds like the signature of your method simply doesn't match the way that you're trying to invoke it. Your error message would tell you more about that so the way to continue is to read it!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.