Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Import data form text into Jtable

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Import data form text into Jtable

    Hello i made a GUI. In that GUI i make a Jtable here the code that i make the Jtable with name table 1
     centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                        inCenterPanel = new JPanel(new GridLayout(1, 2));
                        label4 = new JLabel("A= ");
                        inCenterPanel.add(label4);
                        table1 = new JTable(x, x);
                        table1.setBackground(Color.red);
                        inCenterPanel.add(table1);
                        centerPanel.setBackground(Color.green);
                        inCenterPanel.setBackground(Color.green);
                        centerPanel.add(inCenterPanel);
                        contentPane.add(centerPanel, BorderLayout.NORTH);

    I make o method that i want the GUI read from text file and put the int elements into the jtable with name table 1! here is the code which i try to make that and i dont now how
    public void data2() {
     
     
            try {
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream("data.txt");
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null) {
                    for (int i = 0; i < x; i++) {
                        for (int j = 0; j < x; j++) {
     
     
                        }
                    }
     
     
                }
                //Close the input stream
                in.close();
            } catch (Exception e) {//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
     
        }
    i make this thread also here http://forums.netbeans.org/viewtopic.php?t=44908
    Can you help me ?
    Last edited by redpower1989; November 24th, 2011 at 05:53 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Import data form text into Jtable

    Are there any exceptions showing when you run it? If yes, paste here the full exceptions messages.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Import data form text into Jtable

    Isn't that exception?
    catch (Exception e) {//Catch exception if any
    System.err.println("Error: " + e.getMessage());

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Import data form text into Jtable

    Quote Originally Posted by redpower1989 View Post
    Isn't that exception?
    Well, it's the coding part where you actually catching the exception. What i asked was, when you run the program, is there any exception on the console?

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Import data form text into Jtable

    here is the error
    Error: For input string: ""

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Import data form text into Jtable

    Doesn't this error clearly mean that you are passing an empty string?

  7. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Import data form text into Jtable

    if you see my method data2() i haven't the line to read the integer numbers from text file and put them into my Jtable! I don't how to write that.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Import data form text into Jtable

    So, now you came to the exact point. That was actually your problem. Now if you asked exactly this in your very first post, you will get the very specific answer.

    Well, start reading the file and get the data in the String variable. And then parse them to Integer using,
    Integer.parseInt(String obj);

  9. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Import data form text into Jtable

    Well i think that is
    Integer.parseInt((String) strLine);
    am i right?

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Import data form text into Jtable

    Did you try?
    And yes you are right.
    Simply write
    Integer.parseInt(strLine);

  11. #11
    Junior Member
    Join Date
    Jul 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Import data form text into Jtable

    i write this to strLine= br.readLine();. But i don't know the command to put the numbers into my Jtable with name table 1

Similar Threads

  1. Getting data back from a child form
    By ishtar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 5th, 2011, 08:49 AM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. J2ME-Show RMS data in tabular form
    By Sunil Raghuvanshi in forum Java ME (Mobile Edition)
    Replies: 1
    Last Post: January 24th, 2011, 08:06 AM
  4. Any way to write html form data to file?
    By nathan.fortier@gmail.com in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 14th, 2011, 03:03 PM
  5. storing data using form
    By anupam.j2ee in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2010, 10:43 AM