I have a button which opens up the file chooser. When a txt file is selected a Jframe appears with the data in. How do I change this, so the data displays in a JTable?

Here is my current code

package org.project;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class openResults
{

    public void readMatch() throws Exception
    {
        JFrame frame = new JFrame ("Display File");

        JTextArea ta = new JTextArea (20,30);
        JFileChooser chooser = new JFileChooser();      //Creating the File Chooser

        int status = chooser.showOpenDialog (null);     //setting blank File Chooser

        if(status != JFileChooser.APPROVE_OPTION)
                ta.setText("No File Chosen");
        else
        {
            File file = chooser.getSelectedFile();      //Selecting File
            Scanner scan = new Scanner(file);           //Scanning the file from file chooser

            String info = "";
            while(scan.hasNext())
                info += scan.nextLine() + "\n";
            ta.setText(info);
        }
        frame.getContentPane().add(ta);
        frame.pack();
        frame.setVisible(true);
        }
    }

Hello Ebiz

public static void readMatch() throws FileNotFoundException {
        JFrame frame = new JFrame("Display File");
        JTable table = new JTable();//Creating a new JTable
        List<String> columnData = new ArrayList<String>(); //Creating a List to store String data
        JFileChooser chooser = new JFileChooser(); //Creating the File Chooser
        int status = chooser.showOpenDialog(null); //setting blank File Chooser
        if (status != JFileChooser.APPROVE_OPTION) {
             ((DefaultTableModel) table.getModel()).addColumn("No File Chosen", (Object[])null);
        } else {
            File file = chooser.getSelectedFile(); //Selecting File
            Scanner scan = new Scanner(file); //Scanning the file from file chooser     
            while (scan.hasNext()) {
                columnData.add(scan.nextLine()); // add String data to List
            }
            // one touch !
            ((DefaultTableModel) table.getModel()).addColumn("DATA", columnData.toArray());
        }
        JScrollPane jsp = new JScrollPane(table);
        frame.getContentPane().add(jsp);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//remember close the frame
    }

<String> You can delete
quuba

Thank You.

frame.getContentPane().add(jsp);
this comes up with an error...

and so does:
DefaultTableModel

Thank You.

frame.getContentPane().add(jsp);
this comes up with an error...

and so does:
DefaultTableModel

When you get any errors you have to let us know what they are exactly as we cannot see what you getting on your screen.
For DefaultTableModel I bet you forgot to import required library. With your general imports as I seen import javax.swing.*; this would not cover it. So you need to add import javax.swing.table.*; or even better start using full paths like import javax.swing.table.DefaultTableModel;

Thank You. I have mended these problems, but this creates a default table. I need to get the data from a text file. How would I manage this? I am currently importing a text file through a FileChooser, and displaying this data in a JFrame, so I just need to change this so that there is a table in the frame, and the data is all in the correct cells. Can anyone help? This is the code that I use to select the txt file:

package org.project;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class openResults
{

    public static void readMatch() throws FileNotFoundException
    {
        JFrame frame = new JFrame ("Match Results");

        JTextArea ta = new JTextArea (20,30);
        JFileChooser chooser = new JFileChooser();      //Creating the File Chooser

        int status = chooser.showOpenDialog (null);     //setting blank File Chooser

        if(status != JFileChooser.APPROVE_OPTION)
                ta.setText("No File Chosen");
        else
        {
            File file = chooser.getSelectedFile();      //Selecting File
            Scanner scan = new Scanner(file);           //Scanning the file from file chooser

            String info = "";
            while(scan.hasNext())
                info +=scan.nextLine() + "\n";
            ta.setText(info);
        }
        frame.getContentPane().add(ta);
        frame.pack();
        frame.setVisible(true);
        }
    }

Show Your txt file (a few lines).
quuba

I recived not refreshed subject from www.

@quuba - ebiz absolutely ignored your solution

Just lets wait what he will come back with.

PS: Nice city Krakow, been there in 1988 ;)

I have changed the code. Just put on here the original.

here is a few lines of the file
westham,0,0,Queens Park Rangers
man u,1,0,Preston North End

Please also provide update program or we may just waste time here...

I see, Your data have a form:
4 columns;
Types in order: String,integer,integer,String
Give name for this structure, and names for pariculary columns.
quuba

package org.project;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class openResults
{
	
	public static void readMatch() throws FileNotFoundException
	{
		JFrame frame = new JFrame ("Match Results");
		JTable table = new JTable();
		List<String> columnData = new ArrayList<String>();
		
		JFileChooser chooser = new JFileChooser();		//Creating the File Chooser
		
		int status = chooser.showOpenDialog (null);		//setting blank File Chooser
		
		if(status != JFileChooser.APPROVE_OPTION)
			((DefaultTableModel) table.getModel()).addColumn("No File Chosen", (Object[])null);
	}
	else
		{
			File file = chooser.getSelectedFile();		//Selecting File
			Scanner scan = new Scanner(file);			//Scanning the file from file chooser
			
			while(scan.hasNext(","))
			{
				columnData.add(scan.nextLine());
			}
			((DefaultTableModel) table.getModel()).addColumn("DATA", columnData.toArray());
		}
		JScrollPane jsp = newJScrollPane (table);
		frame.getContentPane().add(jsp);
		frame.pack();
		frame.setVisible(true);
}

Step1.
We focus on one theme. You have

String scanLine="westham,0,0,Queens Park Rangers";
java.util.StringTokenizer st = new java.util.StringTokenizer(scanLine,",");
...

Read all about java.util.StringTokenizer.java class and complete for me the code.
quuba

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.