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 6 of 6

Thread: import my text file to JTable by using JFilechooser

  1. #1
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default import my text file to JTable by using JFilechooser

    can you please help me how to read the text file to JTable by using JFilechooser ?
    Even the text file contains the specific charecters line length
    eg: idUsernamePasswordEmailAgeGender ( ID = 2 Chars, Username = 10 chars ( if username not having 10 chars means, it include space for that, password = 5 chars, email = 5 chars, Age = 2 chars, Gender = 1 chars)


    01Amruta 12345email24F

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: import my text file to JTable by using JFilechooser

    Those are different issues:
    1) find path to file using JFileChooser
    2) read data from file at a given path
    3) parse data into fields
    3) put fieldsinto JTable

    What have you tried for each of the above?
    Please post your code (wrapped in code tags) that shows what you have tried.
    Describe any problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: import my text file to JTable by using JFilechooser

    Here is my code :
    i am not getting error .

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;

    public class TestTable5 {

    public TestTable5() {
    String[] defaultCols = {"Id", "Username", "Password", "email","Name","Age","Gender"};
    DefaultTableModel model = new DefaultTableModel(defaultCols, 0);
    JTable table = new JTable(model);

    JButton button = createButton(table);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add( new JScrollPane(table));
    frame.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    }

    private JButton createButton(final JTable table) {
    JButton button = new JButton("Get File");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(table);
    if (result == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    DefaultTableModel model = createModel(file);
    table.setModel(model);
    }
    }
    });
    return button;
    }

    private DefaultTableModel createModel(File file) {
    DefaultTableModel model = null;

    try {
    BufferedReader txtReader = new BufferedReader(
    new FileReader(file));
    String header = txtReader.readLine();
    model = new DefaultTableModel(header.split("\\s+"), 0);
    String line;
    while ((line = txtReader.readLine()) != null) {
    model.addRow(line.split("\\s+"));
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    return model;
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new TestTable5();
    }
    });
    }
    }

    And my text file values : ( i saved in sampleinput.txt)

    I uploaded here ( filename : 1c.png)

    But my ouput is :
    1a.jpg

  4. #4
    Junior Member
    Join Date
    Dec 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: import my text file to JTable by using JFilechooser

    Here is my code :
    i am not getting error .

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;

    public class TestTable5 {

    public TestTable5() {
    String[] defaultCols = {"Id", "Username", "Password", "email","Name","Age","Gender"};
    DefaultTableModel model = new DefaultTableModel(defaultCols, 0);
    JTable table = new JTable(model);

    JButton button = createButton(table);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add( new JScrollPane(table));
    frame.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    }

    private JButton createButton(final JTable table) {
    JButton button = new JButton("Get File");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(table);
    if (result == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    DefaultTableModel model = createModel(file);
    table.setModel(model);
    }
    }
    });
    return button;
    }

    private DefaultTableModel createModel(File file) {
    DefaultTableModel model = null;

    try {
    BufferedReader txtReader = new BufferedReader(
    new FileReader(file));
    String header = txtReader.readLine();
    model = new DefaultTableModel(header.split("\\s+"), 0);
    String line;
    while ((line = txtReader.readLine()) != null) {
    model.addRow(line.split("\\s+"));
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    return model;
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new TestTable5();
    }
    });
    }
    }

    And my text file values : ( i saved in sampleinput.txt)

    I uploaded here ( filename : 1c.png)

    But my ouput is :
    1a.jpg

    --- Update ---

    Here is my code :
    i am not getting error .

    import java.awt.BorderLayout;
    import java.awt.EventQueue;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;

    public class TestTable5 {

    public TestTable5() {
    String[] defaultCols = {"Id", "Username", "Password", "email","Name","Age","Gender"};
    DefaultTableModel model = new DefaultTableModel(defaultCols, 0);
    JTable table = new JTable(model);

    JButton button = createButton(table);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.add( new JScrollPane(table));
    frame.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
    }

    private JButton createButton(final JTable table) {
    JButton button = new JButton("Get File");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser chooser = new JFileChooser();
    int result = chooser.showOpenDialog(table);
    if (result == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    DefaultTableModel model = createModel(file);
    table.setModel(model);
    }
    }
    });
    return button;
    }

    private DefaultTableModel createModel(File file) {
    DefaultTableModel model = null;

    try {
    BufferedReader txtReader = new BufferedReader(
    new FileReader(file));
    String header = txtReader.readLine();
    model = new DefaultTableModel(header.split("\\s+"), 0);
    String line;
    while ((line = txtReader.readLine()) != null) {
    model.addRow(line.split("\\s+"));
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }

    return model;
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new TestTable5();
    }
    });
    }
    }

    And my text file values : ( i saved in sampleinput.txt)

    I uploaded here ( filename : 1c.png)

    But my ouput is :
    1a.jpg

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: import my text file to JTable by using JFilechooser

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Please copy and paste the output so the contents can be seen and copied. Don't post images.

    i am not getting error .
    Do you have any problems or questions?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,668
    Thanks
    52
    Thanked 2,320 Times in 2,276 Posts

    Default Re: import my text file to JTable by using JFilechooser

    Also posted here: https://www.dreamincode.net/forums/t...-jfilechooser/

    and here: https://coderanch.com/t/704099/java/...hooser#3302554
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: October 4th, 2017, 01:24 PM
  2. Import text file to mysql
    By jayraj in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 6th, 2013, 07:45 PM
  3. Import text file to mysql
    By jayraj in forum JDBC & Databases
    Replies: 6
    Last Post: April 6th, 2013, 07:41 PM
  4. Replies: 0
    Last Post: December 15th, 2011, 01:14 PM
  5. Import data form text into Jtable
    By redpower1989 in forum AWT / Java Swing
    Replies: 10
    Last Post: November 24th, 2011, 08:35 AM