Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to read in a txt file to a JTable?

IggyIggy Member Posts: 6
I was wondering if anyone could advise how I would read in a txt file to a JTable object?

The books I have only inform me on how to set up the table, but not to open and read a file into one.

Any help would be great.


Comments

  • bashirobashiro Member Posts: 244
    : I was wondering if anyone could advise how I would read in a txt file to a JTable object?
    :
    : The books I have only inform me on how to set up the table, but not to open and read a file into one.
    :
    : Any help would be great.
    :

    Do you mean how to import data stored in text file into JTable ?
    Which enviroment are you working in ? For example jbuilder, fort for java etc ..

    bashiro
  • IggyIggy Member Posts: 6
    : : I was wondering if anyone could advise how I would read in a txt file to a JTable object?
    : :
    : : The books I have only inform me on how to set up the table, but not to open and read a file into one.
    : :
    : : Any help would be great.
    : :
    :
    : Do you mean how to import data stored in text file into JTable ?
    : Which enviroment are you working in ? For example jbuilder, fort for java etc ..
    :
    : bashiro
    :

    Yes, I wish to import the data stored in a text file into a JTable in an application I'm trying to build.

    I'm using Textpad when coding.

    I've found some stuff about JFileChooser, but I'm not confident about how to get it to work with the JTable, or even if that's the best method?






  • chamsterchamster Member Posts: 662
    : I've found some stuff about JFileChooser, but I'm not confident about how to get it to work with the JTable, or even if that's the best method?

    JFileChooser is a class for GUI-oriented selection of a file (or directory). It has little to do with the JTable. I don't have the API at hand right now so i'm not sure about the exact name but there's something called [blue]TableModel[/blue] which will help you load the values into the table.

    THe most important issue is, of course, what format you have on the saved data. Is it a binary file or a normal text file and if so - how is the data "written" in there?

    For instance:
    1)
    a, b, c, d, e,
    f, g, h, i, j,
    2)
    a b c d e | f g h i j
    ...and so on.


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • IggyIggy Member Posts: 6
    : : I've found some stuff about JFileChooser, but I'm not confident about how to get it to work with the JTable, or even if that's the best method?
    :
    : JFileChooser is a class for GUI-oriented selection of a file (or directory). It has little to do with the JTable. I don't have the API at hand right now so i'm not sure about the exact name but there's something called [blue]TableModel[/blue] which will help you load the values into the table.
    :
    : THe most important issue is, of course, what format you have on the saved data. Is it a binary file or a normal text file and if so - how is the data "written" in there?
    :
    : For instance:
    : 1)
    : a, b, c, d, e,
    : f, g, h, i, j,
    : 2)
    : a b c d e | f g h i j
    : ...and so on.
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : chamster@home.se
    : http://konrads.webbsida.com
    :
    :
    The data is stored in a text file (.txt).

    The layout is:
    data(tab space)data(tab space)data(tab space)data
    data(tab space)data(tab space)data(tab space)data
    etc.





  • bashirobashiro Member Posts: 244

    : The data is stored in a text file (.txt).
    :
    : The layout is:
    : data(tab space)data(tab space)data(tab space)data
    : data(tab space)data(tab space)data(tab space)data
    : etc.
    :

    I normally use Java Develop Program .I use Jbuilder and it's
    simple using Jbuilder since it has visual tools. And I am sure
    itwill be easier with Visaul Cafe, Forte for java etc.

    My suggestion are listed below .

    Alternative 1;

    what u can do is like this.
    a) read a line from the file using BufferedReader

    b) Tokenize this line using StringTokenizer by default StringTokenizer will separate tokens on the basis of tab and space character.

    c)Now you consider each token one by one and check if it contains
    a , ( comma ) . in case it contains comma then you further tokenize this token using , as delimiter and so on. untill you reach the actual field.

    d) keep on adding the final token to a Vector

    e) and pass this Vector to JTable Constructor.


    Alternative 2:
    This is the easiest way for you.In this case you don't have to
    use the text file. You simply write your datas as multiple arrays.
    And then use them in Jtable.
    An example can be found here
    http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/SimpleTableDemo.java

    A good tutorial here:
    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html


    Hope this helps.
    Mean while I have some notes only I am a little busy.
    I willtry to such them in the week-end and if I found something
    good I will let you know.


    Regards
    Bashiro

  • chamsterchamster Member Posts: 662
    : The data is stored in a text file (.txt).
    : The layout is:
    : data(tab space)data(tab space)data(tab space)data
    : data(tab space)data(tab space)data(tab space)data

    You can use StringTokenizer to get the String-objects from the file. After that, use the table model (or whatever that's called) and it will place the data for you. If it's not so many posts, you can actually do a double for-loop and add the data yourself.

    The model-way gives you more flexibility later on but it's fully possible that you won't be needing it, in which case a for-loop will be enough to make yo happy :-).


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    chamster@home.se
    http://konrads.webbsida.com

  • Shawn CarterShawn Carter Member Posts: 0

    _____ || http://forcoder.org || free ebooks and video tutorials about || Perl, Assembly, Ruby, Swift, Go, Python, Delphi, PL/SQL, JavaScript, Objective-C, Visual Basic, C++, R, Java, PHP, C#, C, Scratch, MATLAB, Visual Basic .NET Bash, Erlang, Kotlin, D, FoxPro, Lua, LabVIEW, Scala, Awk, SAS, ABAP, Transact-SQL, Ada, Crystal, Apex, Hack, Fortran, Rust, Scheme, F#, Prolog, ML, Clojure, COBOL, Logo, Alice, VBScript, Dart, Lisp, Julia || _____

Sign In or Register to comment.