-6

Within a JPanel...

is it possible to display certain lines of text from a specific .txt file?

example...

user chooses from a few dropdowns (I got this part down already) and clicks submit, then in a JPanel on the right, I want it to pull (for example) lines 24 - 43 from xxx.txt

I'm not finding a way to get this done, is it even possible?

Psest328
  • 6,575
  • 11
  • 55
  • 90
  • Yes, it's very possible. Load the file into a newline delimited data structure of some kind (say, a `List`) and then select the desired range from the data structure (e.g. `myList.subList(24,43)` and then (perhaps collapse the text, if you like) add the text to a label or text pane or something and stick that in the panel. – Tim Jan 26 '13 at 06:23
  • 2
    *"is it even possible?"* Which part? Reading lines from a file? Selecting particular lines from those read? Displaying them? It seems a foolish question to ask if any of those lesser tasks is achievable (of course they are), and instead like you just want someone to code it for you. – Andrew Thompson Jan 26 '13 at 06:36
  • At what step are you really having problems? Passing the file name from your JComboBox to a Scanner? Using a Scanner to read lines from a file? Storing the lines from Scanner for easy line by line access? Setting the text in a field using setText? Getting a text pane to update with new text by using an ActionListener paired with your JComboBox? You need to clarify what step is throwing you. – sage88 Jan 26 '13 at 06:46
  • I wasn't looking for the code, just direction. I'm taking on this project as a learning exercise. I'm just getting into swing so I didn't even know where to start. – Psest328 Jan 26 '13 at 06:50

3 Answers3

2

While what you are saying should consider other aspects as well like what if the file is very big and hence simply storing into any collection just to acces it by number wont be a good solution. There are various APIs which can help you to solve this problem like RandomAccessFile. Check these links will be helpful

Reading a specific line from a text file in Java

http://bitsofinfo.wordpress.com/2009/04/15/how-to-read-a-specific-line-from-a-very-large-file-in-java/

How to read a specific line using the specific line number from a file in Java?

Community
  • 1
  • 1
CuriousMind
  • 3,143
  • 3
  • 29
  • 54
  • This is actually exactly what I was looking for. I wasn't looking for the answer, but a guide to help me learn. I guess I should have worded my question better. – Psest328 Jan 26 '13 at 07:05
  • hey good to know that it helps. A word of advice, before you throw the question, hit the good search engines, you will find ansewrs. :) – CuriousMind Jan 26 '13 at 15:51
  • I tried searching but couldn't find what I was looking for. I kept finding how to import images and html tags and whatnot (found the import image thing on accident, which actually helps because I need to put the company logo in the app) – Psest328 Jan 26 '13 at 17:17
2

I'm wondering if you're making this overly complex. All you need to do is use the Scanner class to read the text from your file. And then as Tim mentioned in his comment you could load whatever Scanner takes in into a list structure and then print out the lines from that (though honestly you could even do it without the list storage structure, though it does make manipulating it easier). And then yeah use setText method for a text pane and off you go.

The ListInterface could be replaced with any storage type. Again Tim suggested a list that would work just as well. You could also just use a simple array, though then you wouldn't be able to write your own methods to interface with it as nicely.

ListInterface<String> fileBag = new ListArray<>();
try
{
    /*
     * Open file fileName with Scanner
     * add all entries fron the file somefile.txt into fileBag
     * Close Scanner object
     */
    String fileName = "somefile.txt";
    File file = new File(fileName);
    Scanner scanner = new Scanner(file);
    String line;
    while (scanner.hasNextLine())
    {
        line = scanner.nextLine();
        fileBag.add(line);
    }
    scanner.close();
}
catch(FileNotFoundException e)
{
    System.out.println(e.getMessage());
    System.exit(0);
}

If you don't know how to use interfaces and write your own methods for it in the class ListArray then replace

ListInterface<String> fileBag = new ListArray<>();

with

ArrayList<String> fileArrayList = new ArrayList<>(1000);

where 1000 should be a best guess at how big the array needs to be it doesn't need to be correct, the array will double its capacity if it goes to add and there isn't enough space, but this is an O(n) operation so it's best to avoid it if you have a good idea of how many lines your files will contain at maximum. Also replace:

fileBag.add(line);

with

with fileArrayList.add(line);
sage88
  • 4,104
  • 4
  • 31
  • 41
  • This is awesome. I'm going to try it out tomorrow and let you know how it works out. Thanks so much for the help – Psest328 Jan 26 '13 at 06:50
  • @WizardKnight Keep in mind that the ListInterface and ArrayList classes are ones you'd have to write yourself. If you are uncomfortable doing that (take a data structures course), see my suggestions above. – sage88 Jan 26 '13 at 06:57
  • I have no problem with ArrayLists, will read up on ListInterface now. Thanks so much – Psest328 Jan 26 '13 at 17:33
1

Take a look at

  • Basic IO. I'd recommend you use a FileReader wrapped in BufferedReader
  • How to use Lists which is a way of displaying rows of data ... Or
  • How to Text Areas which is a way of displaying multlines of text. This is probably better as it supports text selection and copying

Small update

A lot is going to depend on the size of the file. It would be more efficient to read the file in once and store it in a java.util.List then reading on each request, but this will depend on how much memory you have available

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah given what he says he's doing with it (just writing whole lines out) BufferedReader would be a slightly better choice, though the efficiency is probably pretty negligible over Scanner. – sage88 Jan 26 '13 at 07:22
  • @Sage88 I just have more experience with Readers then I do with Scanner :P – MadProgrammer Jan 26 '13 at 07:24