Skip to content
Advertisement

How to use JFileChooser in a JTable to select files and display data in the JTable

I am trying to be able to implement JFileChooser to be able to select from two .txt files made from another program. I have a fairly simple JTable created that will load in the data from one of the .txt files depending on which path I give it. But I am lost on how to make a JTable implement JFileChooser to be able to select either file and have the data shown in the correct cells. Below is the code for the JTable and a small sample of data from one of the files

JavaScript

Data from .txt

JavaScript

Advertisement

Answer

DICLAIMER: This answer was originally posted to a duplicate question by the same user, so I’m reposting it here to keep everything in the same place for the sake of future readers.

You have multiple issues that need to be fixed before you can even begin to address the main problem — the code you posted doesn’t even compile.

This is your code, below. Can you spot the problem?

JavaScript

It should read as follows (your compiler would have alerted you to the issue):

JavaScript

Your table model is missing a mandatory method. Any time you create an implementation which extends an Interface, you are required to implement all method signatures from that Interface. In your case, you’re missing the following method, and again, your compiler would have warned you about the issue:

JavaScript

Your data file is organized in a less than desirable manner. You seem to have written it as if its primary function was to supply information to a person, rather than a program, so you have these nice labels oriented vertically instead of horizontally, even though you want your JTable to display horizontally.

Instead of

JavaScript

you should have maybe a comma separated value (CSV) file, like

JavaScript

You were getting ArrayIndexOutOfBoundsException because your code was reading the line

JavaScript

as line1, and when you call line1[1], there’s nothing there. You split on colon, but there’s nothing to the right of the colon, so the split method doesn’t bother to make a new token.

Get rid of all the nice headers and such because you’ll just have to read those lines and throw them away later — they’re worse than useless because they actually introduce problems when your code doesn’t anticipate their presence.

Now, you seem to have based your file reading code on a misunderstanding of how files are read. Whatever your current understanding of file reading is, forget it and pretend you’ve never heard of it before. The goal is to read lines one by one, break the line up into chunks of data (we’ll call them tokens) and package them into a Line object before moving to the next line — we’re not reading five lines at a time here.

This kind of stuff is no longer necessary once you organize your input data sensibly:

JavaScript

Now, all you have to do is something like this:

JavaScript

Fix all this stuff and your table will display your data.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement