0

I'm trying to make a program in java JFrame with a table. I want to save my text from the rows. I put the text in a text file and that is working.

But i want to get my text from my text file into my table. I have tried a lot of things but nothing is working. can somebody help pls?

nachokk
  • 14,363
  • 4
  • 24
  • 53
user3332136
  • 159
  • 1
  • 2
  • 6
  • 5
    `I have tried a lot of things` show us what have you tried so far in a concrete example. Cause if not this question is too broad and is gonna to be closed. – nachokk Feb 24 '14 at 14:58
  • Could you please show that what you tried ? – Suzon Feb 24 '14 at 14:59
  • 1
    Getting text from a text file into anything can be a complex task, information about how the text is stored in the text file will probably be needed before identifying the best way to read it into the JTable – Levenal Feb 24 '14 at 15:00
  • You probable need to take a look in DefaultTableModel – Jose De Gouveia Feb 24 '14 at 15:07
  • _"can somebody help pls?"_ Help us help you! Post some code so we can see where you're going wrong. If you refuse, then take a look at [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – Paul Samsotha Feb 24 '14 at 15:15

1 Answers1

1

'Hypothetically' if you stored the text in your file line by line, for example like this:

tester.txt:

Star Wars
Star Trek
The Lord of The Rings

Then you could read it in line by line and when you have read enough lines add a row to the table. In order to add a row to an existing table I believe you do need to use the Model, or if creating from fresh prepare the data beforehand and then create. Below is a rough example using the above txt file:

public class SO {
public static void main(String[] args) {

    //Desktop
    Path path = Paths.get(System.getProperty("user.home"), "Desktop", "tester.txt");

    //Reader        
    try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))){
        Vector<String> row = new Vector<String>();

        //Add lines of file
        int numOfCellsInRow = 3; //Num of cells we want
        int count = 0;
        while (count < numOfCellsInRow){
                row.addElement(reader.readLine());
                count++;
        }

        //Column names
         Vector<String> columnNames = new Vector<String>();
         columnNames.addElement("Column One");
         columnNames.addElement("Column Two");
         columnNames.addElement("Column Three");  

         Vector<Vector<String>> rowData = new Vector<Vector<String>>();
         rowData.addElement(row);

         //Make table
         JTable table = new JTable(rowData, columnNames);

         //How you could add another row by drawing more text from the file,
         //here I have just used Strings
         //Source: http://stackoverflow.com/questions/3549206/how-to-add-row-in-jtable
         DefaultTableModel model = (DefaultTableModel) table.getModel();
         model.addRow(new Object[]{"Darth Vader", "Khan", "Sauron"});

         //Make JFrame and add table to it, then display JFrame
         JFrame frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         JScrollPane scrollPane = new JScrollPane(table);
         frame.add(scrollPane, BorderLayout.CENTER);
         frame.pack();
         frame.setVisible(true);

    } catch (IOException e) {
        e.printStackTrace();
    }   
}
}

Two things to note, firstly I have used Vectors, but these are discouraged due I believe to speed issues so you may want to look into varying on these. The second and main issue is the text in the file. Only by knowing how you intend to store the text can we know how to read it back successfully into the table. Hopefully though this example can point you in the right direction.

EDIT

Regarding your re-posted code, firstly I made this final:

final Path path = Paths.get(System.getProperty("user.home"), "Desktop", "File.txt");

Then altered your listener method to take the text input based of the file you make by clicking the save button:

  btnGetFile.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        //This prevents exception
        if(!Files.exists(path)){//If no file
            JOptionPane.showMessageDialog(null, "File does not exist!");//MSg
            return;//End method
        }
        /*changed this bit so that it reads the data and 
         * should then add the rows
         */
        try (BufferedReader reader = new BufferedReader(new FileReader(path.toFile()))){
            String line;
            while((line = reader.readLine()) != null){//Chek for data, reafLine gets first line 
                Vector<String> row = new Vector<String>();//New Row
                row.addElement(line);//Add first line
                  int numOfCellsInRow = 3; //Num of cells we want
                  int count = 0;
                  //We only want 2 because we got the first element at loop start
                  while (count < (numOfCellsInRow - 1)){
                      row.addElement(reader.readLine());
                      count++;
                  }
                  model.addRow(row);//Add rows from file
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }  
    }
});

Added comments to try and explain what is going on.

It worked for my by adding the rows from the file to the JTable. Hopefully it works for you now also!

Levenal
  • 3,796
  • 3
  • 24
  • 29