Forum Stats

  • 3,815,606 Users
  • 2,259,059 Discussions
  • 7,893,185 Comments

Discussions

import text file to JTable by using JFilechooser

AmrutaSriram
AmrutaSriram Member Posts: 1
edited Dec 31, 2018 4:54AM in Swing

If Anyone Knows , can you please help me how to import 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

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_CLOSE);

       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.PNG1b.PNG1c.PNG

But i want sampleinput notepad values in jtable by using jfilechooser.

Message was edited by: AmrutaSriram