1

I have 2 class one for io/reading file and other one for the GUI, in the past few days I was working on the class of io/reading and I was using simple technique for getting the file from hard disk by writing the file name only as string note: look at the code, but now after I finished the io/reading code I need to use more professional way by adding JFileChooser in the Gui class and couple them and here is the difficulty can some one tell me how?

The Jfilechooser code inside the constrictor in the GUI class this only going to open the JFileChooser and select the file and save the file to the String srtPath.

 Action openAction = new AbstractAction("Open Subtitle", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                ourFileSelector.showSaveDialog(null);
                ourSrtFile = ourFileSelector.getSelectedFile();
                srtPath = ourSrtFile.getAbsolutePath();

            }
        };

all the class of reading file

       package AnimeAid;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReadFile {

public static ArrayList<String> getFileStartingTime(String file){   
   ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader(file));

            String line;

            while((line = br.readLine()) != null) {
        if (line.indexOf(':') != -1 && line.indexOf(',') != -1 && line.indexOf('0') != -1) { 
        Lines.add(line.substring(0, 12));
        }
        }
        }catch(IOException ex){
        System.err.println(ex);
        }

         return Lines;
}

public static ArrayList<String> getFileEndingTime(String file){   
     ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            while((line = br.readLine()) != null) {
        if (line.indexOf(':') != -1 && line.indexOf(',') != -1 && line.indexOf('0') != -1) { 
            Lines.add(line.substring(18, 29));
        }
        }
        }catch(IOException ex){
        System.err.println(ex);
        }

         return Lines;

}




public static ArrayList<String> readSubtitles(String file)
{
     ArrayList<String> Lines = new ArrayList<String>();
    try{

        //String file = "tra.srt";
        Charset charset = Charset.defaultCharset();
        Path path = Paths.get(file);

        byte[] encoded = Files.readAllBytes(path);
        String data = charset.decode(ByteBuffer.wrap(encoded)).toString();

        Pattern p =  Pattern.compile("(\\d+:\\d+:\\d+,\\d+) --> (\\d+:\\d+:\\d+,\\d+)\\s*(.*?)\\s*(^$|\\Z)", Pattern.DOTALL | Pattern.MULTILINE);
        Matcher m = p.matcher(data);

        while (m.find()){
            //String startTime = m.group(1);
            //String endTime = m.group(2);
            //String subtitle = m.group(3);
            Lines.add(m.group(3));
            //System.out.println(startTime);
            //System.out.println(endTime);
        }
    }catch(IOException ex){
    System.err.println(ex);
    }
    return Lines;     
}

 public static ArrayList<String> ArraylineLengths(String file) {
        ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));

            String line;

            while((line = br.readLine()) != null) {
                    line = line.replace("\uFEFF", "");
                if(isInteger(line)) {
                    int i = Integer.parseInt(line);
                    if(i > 0) {
                        Lines.add(line);

                    }
                }
            }

        } catch(IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if(br != null) {
                try {
                    br.close();
                } catch(IOException e) {
                    // do nothing
                }
            }
        }
        return (Lines);

    }

 public static boolean isInteger(String s) {
    try { 
        Integer.parseInt(s); 
    } catch(NumberFormatException e) { 
        return false; 
    }
    // only got here if we didn't return false
    return true;
}

  public static int maxLine(String file){
    try
        {

            //String file = "tra.srt";
            int max = 0;
            BufferedReader br = null;
            try  { br = new BufferedReader(new FileReader(file)); }
            catch (FileNotFoundException e) { System.out.println(e); }

            String line;
            while((line = br.readLine()) != null)
            {
                if (isInteger(line))
                {

                    max++;
                }
            }
              return max+1;
        }
        catch(  NumberFormatException | IOException ex) {ex.printStackTrace();}
        return 0;


 }


}

The answer should be something like this

String file = GuiInterface.srtPath;

and i tried this

    public class ReadFile {
GuiInterface tt = new GuiInterface(null);
public static String file = GuiInterface.srtPath;
public static ArrayList<String> getFileStartingTime(){   
   ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader(file));

            String line;

            while((line = br.readLine()) != null) {
        if (line.indexOf(':') != -1 && line.indexOf(',') != -1 && line.indexOf('0') != -1) { 
        Lines.add(line.substring(0, 12));
        }
        }
        }catch(IOException ex){
        System.err.println(ex);
        }

         return Lines;
}

but when I use this vlcj library only show the video.

this is my way of adding data to the Jtable

     /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package AnimeAid;



import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.BorderFactory.*;
import javax.swing.border.*;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.player.MediaPlayerFactory;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;




/**
 *
 * @author isslam
 */
public class GuiInterface extends JFrame {

    JComboBox laList;  
    ReadFile reader;
    private final int numberOfButton = 6;
    private final JTable table;
    JToolBar toolBar;
    private final JTextField enterText,startTime,endTime;
    private final JMenu jMenu1,jMenu2,jMenu3;
    private final JMenuBar jMenuBar1;
    private final JMenuItem itemNewSrt,itemOpenVideo,itemSavefile;
    private static JFileChooser ourFileSelector,ourVideoSelector;
    File ourVideoFile,ourSrtFile;
    Border Campound,empty,Boveld,etch;
    private final JLabel startTimeingLable,endTimeingLabel;
    public static String  mediaPath="",srtPath="";
    Canvas c;
    ImageIcon[] icon;
    JButton[] Obutton;
   static final int COLUMN = 4 ; 



         public static void main(String[] args) throws IOException 
       {
           //ReadFile.readSubtitles();
        NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "C:\\Program Files\\VideoLAN\\VLC");
        Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
          SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GuiInterface("");

            }

        });

}



    public GuiInterface(String title){

    //reader = new ReadFile();

    setSize(1024, 720);
    setVisible(true);
    setTitle("AnimeFactor");
    setDefaultCloseOperation(GuiInterface.EXIT_ON_CLOSE);
    //video setting 
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    c = new Canvas();
    String[] petStrings = { "Translation Line", "Both Line" };
    laList = new JComboBox(petStrings);
    c.setBackground(Color.black);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(c, BorderLayout.CENTER);
    add(p, BorderLayout.CENTER);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
    mediaPlayer.playMedia("C:\\Users\\isslam\\Downloads\\gg.mp4");

   /* String[] columnNames = {"#","Start","End","Translation column"};

    Object[][] data = new Object [ReadFile.maxLine(srtPath)][COLUMN];
    ArrayList <String > Dumy = new ArrayList<String>();

   //String [] countries = list.toArray(new String[list.size()]);

   Dumy = ReadFile.ArraylineLengths(srtPath);


    for(int i = 0; i < Dumy.size(); i++) 
    {
    data[i][0]  = Dumy.get(i);
    }

    ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
    ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
    ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
    for(int i=0;i < ReadFile.maxLine(srtPath);i++){ 
         // data[i][0] = ReadFile.lineLengths();
            data[i][1] = starts.get(i) ;
            data[i][2] = ends.get(i);
            data[i][3] = subs.get(i);

    } 
  DefaultTableModel model = new DefaultTableModel(data, columnNames);
 */

    ImageIcon openIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/folder-icon.png"));
        ImageIcon saveIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/red-disk-icon.png"));
        ImageIcon newIcon = new ImageIcon(
                GuiInterface.class.getResource("/resources/image/Actionsnew-icon.png"));






        Action saveAction = new AbstractAction("Save", saveIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        };
        Action newAction = new AbstractAction("New", newIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("New File");
            }
        };
    jMenu1 = new JMenu("File");
    jMenu2 = new JMenu("Video");
    jMenu3 = new JMenu("Subtitle");
    itemNewSrt = new JMenuItem(newAction);
    jMenu1.add(itemNewSrt);


    itemSavefile = new JMenuItem(saveAction);
    jMenu1.add(itemSavefile);
    jMenuBar1 = new JMenuBar();
    jMenuBar1.setBorder(etch);
    setJMenuBar(jMenuBar1);
    jMenuBar1.add(jMenu1);
    jMenuBar1.add(jMenu2);
    jMenuBar1.add(jMenu3);
     table = new JTable();
     table.setFillsViewportHeight(true);
     table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
     TableColumn columnA = table.getColumn("#");
      columnA.setMinWidth(10);
      columnA.setMaxWidth(40);
      TableColumn columnB= table.getColumn("Start");
      columnB.setMinWidth(80);
      columnB.setMaxWidth(90);
      TableColumn columnC= table.getColumn("End");
      columnC.setMinWidth(80);
      columnC.setMaxWidth(90);
    Obutton = new JButton[numberOfButton];
    etch = BorderFactory.createEtchedBorder();
    enterText = new JTextField();
    enterText.setPreferredSize(new Dimension(0,100));
    ourFileSelector = new JFileChooser();
    startTime = new JTextField();
    startTime.setPreferredSize(new Dimension(120, 20));
    startTimeingLable = new JLabel("Starting Time");
    endTimeingLabel = new JLabel("Ending Time");
    endTime = new JTextField();
    endTime.setPreferredSize(new Dimension(120, 20));
    toolBar = new JToolBar();
        //toolBar.add(Box.createHorizontalGlue());
        toolBar.setBorder(new LineBorder(Color.LIGHT_GRAY, 1));
        toolBar.add(newAction);
        toolBar.add(saveAction);


        JPanel toolBarPane = new JPanel(new BorderLayout());
        toolBarPane.add(toolBar,BorderLayout.NORTH);

        JPanel timing = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        timing.add(startTimeingLable);
        timing.add(startTime);
        timing.add(endTimeingLabel);
        timing.add(endTime);
        timing.add(laList);

        empty = BorderFactory.createEmptyBorder(30, 5, 5, 5);
        Boveld = BorderFactory.createBevelBorder(BevelBorder.RAISED);
        Campound = BorderFactory.createCompoundBorder(empty,Boveld);


        JPanel textFiled = new JPanel(new BorderLayout());
        textFiled.add(timing);
        textFiled.add(enterText,BorderLayout.SOUTH);
        textFiled.setBorder(Campound);

        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.SOUTH);
        add(textFiled, BorderLayout.EAST);
        add(toolBarPane,BorderLayout.PAGE_START);

         Action openAction = new AbstractAction("Open Subtitle", openIcon) {
            @Override
            public void actionPerformed(ActionEvent e) {
                ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                ourFileSelector.showSaveDialog(null);
                ourSrtFile =  ourFileSelector.getSelectedFile();
                srtPath = ourSrtFile.getAbsolutePath();
                DefaultTableModel model = createModel(srtPath);
                table.setModel(model);


            }
        };

        //Container cp = getContentPane();
       toolBar.add(openAction);
       itemOpenVideo = new JMenuItem(openAction);
       jMenu1.add(itemOpenVideo);
       itemOpenVideo.addActionListener(new MenuBarMethod());

    }

    private DefaultTableModel createModel(String srtPath) {
        String[] columnNames = {"#", "Start", "End", "Translation column"};

        int maxLine = ReadFile.maxLine(srtPath);  // debug
        //Object[][] data = new Object[maxLine][];
        System.out.println(maxLine);  // debug

        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
        ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
        ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
        for (int i = 0; i < ReadFile.maxLine(srtPath) - 1; i++) {
            model.addRow(new Object[] {starts.get(i), ends.get(i), subs.get(i)});
        }


        return model;
    }

public class MenuBarMethod implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent a){
        Object buttonPressed=a.getSource();
       if(buttonPressed.equals(itemOpenVideo)){
        ourVideoSelector.setFileSelectionMode(JFileChooser.FILES_ONLY);
        ourVideoSelector.showSaveDialog(null);
        ourVideoFile = ourVideoSelector.getSelectedFile();
        mediaPath = ourVideoFile.getAbsolutePath();
       }
    }

}

}

the issue with the code is this part of code are not working

 table = new JTable();
 table.setFillsViewportHeight(true);
 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
 TableColumn columnA = table.getColumn("#");
  columnA.setMinWidth(10);
  columnA.setMaxWidth(40);
  TableColumn columnB= table.getColumn("Start");
  columnB.setMinWidth(80);
  columnB.setMaxWidth(90);
  TableColumn columnC= table.getColumn("End");
  columnC.setMinWidth(80);
  columnC.setMaxWidth(90);

part of the file

    1
00:00:01,600 --> 00:00:04,080
<b>Mr Magnussen, please state your
full name for the record.</b>

2
00:00:04,080 --> 00:00:07,040
Charles Augustus Magnussen.

now this is a picture of the application application

as you see the icon of opening the file to read are not there for some reason the white background under the JTextFiled tells that jtable are created but no information yet

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is there any problem with your code? It works as you expect? Keeping the strPath as static depends on your application design!! – AJJ Mar 28 '14 at 14:59
  • Hi Jack, while English is not my first language, as most people here in stackoverflow, I do my best to express myself in English as well as making an effort to understand what other non-native english speakers are trying to say. Anyway,it is really hard to understand what you are asking here. Could you please review your question and try to explain what is the problem? – Anthony Accioly Mar 28 '14 at 15:06
  • take look at the code –  Mar 28 '14 at 16:08
  • 2
    This question appears to be off-topic because the OP is treating this post like a help desk and introduces a new problem after the previous is solved. – Paul Samsotha Mar 29 '14 at 18:18
  • ok i added new question –  Mar 29 '14 at 18:59

1 Answers1

2

"The answer should be something like this"
String file = GuiInterface.srtPath;

Your GuiInterface is the running class. Since ReadFile is a "helper class" with a static method, it shouldn't have to know anything about the GuiInterface class.

What you should do instead, is have the getFileStartingTime() take a String path argument, and have the FileReader use that path argument.

public static ArrayList<String> getFileStartingTime(String path) {
    ArrayList<String> Lines = new ArrayList<String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(path));

Then when you should call the ReadFile.getFileStartTime(...) from the Action, and do something with the returned ArrayList. Something like

    File ourSrtFile = ourFileSelector.getSelectedFile();
    String srtPath = ourSrtFile.getAbsolutePath();
    ArrayList<String> array = ReadFile.getFileStartingTime(srtPath);
    DefaultListModel model = (DefaultListModel) list.getModel();
    for (String s : array) {
         model.addElement(s);
    }   

Here's a complete example. But note: reading files (and long running tasks) should be done in a background thread, possibly using a SwingWorker, but I'm too lazy to do that right now. You can look at the link

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class GuiInterface {

    private JFileChooser ourFileSelector = new JFileChooser();
    private JList list = new JList(new DefaultListModel());

    public GuiInterface() {
        Action openAction = new AbstractAction("Open Subtitle") {
            @Override
            public void actionPerformed(ActionEvent e) {
                ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                ourFileSelector.showSaveDialog(null);
                File ourSrtFile = ourFileSelector.getSelectedFile();
                String srtPath = ourSrtFile.getAbsolutePath();
                ArrayList<String> array = ReadFile.getFileStartingTime(srtPath);
                DefaultListModel model = (DefaultListModel) list.getModel();
                for (String s : array) {
                    model.addElement(s);
                }
            }
        };
        JButton button = new JButton(openAction);
        JFrame frame = new JFrame();
        JScrollPane scroll = new JScrollPane(list);
        scroll.setPreferredSize(new Dimension(200, 300));
        frame.add(scroll);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

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

        });
    }
}

class ReadFile {

    public static ArrayList<String> getFileStartingTime(String path) {
        ArrayList<String> Lines = new ArrayList<String>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(path));

            String line;

            while ((line = br.readLine()) != null) {
                Lines.add(line);
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }

        return Lines;
    }
}

EDIT

Try this out now

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.AbstractAction;
import javax.swing.Action;
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 GuiInterface {

    private JFileChooser ourFileSelector = new JFileChooser();
    private JTable table = new JTable();

    public GuiInterface() {
        Action openAction = new AbstractAction("Open Subtitle") {
            @Override
            public void actionPerformed(ActionEvent e) {
                ourFileSelector.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                ourFileSelector.showSaveDialog(null);
                File ourSrtFile = ourFileSelector.getSelectedFile();
                String srtPath = ourSrtFile.getAbsolutePath();
                DefaultTableModel model = createModel(srtPath);
                table.setModel(model);
            }
        };
        JButton button = new JButton(openAction);
        JFrame frame = new JFrame();
        JScrollPane scroll = new JScrollPane(table);
        //scroll.setPreferredSize(new Dimension(200, 300));
        frame.add(scroll);
        frame.add(button, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private DefaultTableModel createModel(String srtPath) {
        String[] columnNames = {"#", "Start", "End", "Translation column"};

        int maxLine = ReadFile.maxLine(srtPath);  // debug
        //Object[][] data = new Object[maxLine][];
        System.out.println(maxLine);  // debug

        DefaultTableModel model = new DefaultTableModel(columnNames, 0);

        ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
        ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
        ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
        for (int i = 0; i < ReadFile.maxLine(srtPath) - 1; i++) {
            model.addRow(new Object[] {starts.get(i), ends.get(i), subs.get(i)});
        }


        return model;
    }

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

        });
    }
}

class ReadFile {

    public static ArrayList<String> getFileStartingTime(String file) {
        ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {

            br = new BufferedReader(new FileReader(file));

            String line;

            while ((line = br.readLine()) != null) {
                if (line.indexOf(':') != -1 && line.indexOf(',') != -1 && line.indexOf('0') != -1) {
                    Lines.add(line.substring(0, 12));
                }
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }

        return Lines;
    }

    public static ArrayList<String> getFileEndingTime(String file) {
        ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.indexOf(':') != -1 && line.indexOf(',') != -1 && line.indexOf('0') != -1) {
                    Lines.add(line.substring(18, 29));
                }
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }

        return Lines;

    }

    public static ArrayList<String> readSubtitles(String file) {
        ArrayList<String> Lines = new ArrayList<String>();
        try {

            //String file = "tra.srt";
            Charset charset = Charset.defaultCharset();
            Path path = Paths.get(file);

            byte[] encoded = Files.readAllBytes(path);
            String data = charset.decode(ByteBuffer.wrap(encoded)).toString();

            Pattern p = Pattern.compile("(\\d+:\\d+:\\d+,\\d+) --> (\\d+:\\d+:\\d+,\\d+)\\s*(.*?)\\s*(^$|\\Z)", Pattern.DOTALL | Pattern.MULTILINE);
            Matcher m = p.matcher(data);

            while (m.find()) {
                //String startTime = m.group(1);
                //String endTime = m.group(2);
                //String subtitle = m.group(3);
                Lines.add(m.group(3));
                //System.out.println(startTime);
                //System.out.println(endTime);
            }
        } catch (IOException ex) {
            System.err.println(ex);
        }
        return Lines;
    }

    public static ArrayList<String> ArraylineLengths(String file) {
        ArrayList<String> Lines = new ArrayList<String>();
        //String file = "tra.srt";
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));

            String line;

            while ((line = br.readLine()) != null) {
                line = line.replace("\uFEFF", "");
                if (isInteger(line)) {
                    int i = Integer.parseInt(line);
                    if (i > 0) {
                        Lines.add(line);

                    }
                }
            }

        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    // do nothing
                }
            }
        }
        return (Lines);

    }

    public static boolean isInteger(String s) {
        try {
            Integer.parseInt(s);
        } catch (NumberFormatException e) {
            return false;
        }
        // only got here if we didn't return false
        return true;
    }

    public static int maxLine(String file) {
        try {

            //String file = "tra.srt";
            int max = 0;
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader(file));
            } catch (FileNotFoundException e) {
                System.out.println(e);
            }

            String line;
            while ((line = br.readLine()) != null) {
                if (isInteger(line)) {

                    max++;
                }
            }
            return max + 1;
        } catch (NumberFormatException | IOException ex) {
            ex.printStackTrace();
        }
        return 0;

    }

}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • in my last code i used DefaultTableModel model = new DefaultTableModel(data, columnNames); why did u use DefaultListModel model = (DefaultListModel) list.getModel(); –  Mar 28 '14 at 20:00
  • Because I used a `Jlist` and not a `JTable`. I posted this answer _before_ you edited your original question. If you are still having a problem, post a sample of your text file you are reading from, and all the methods you use to read from that file. Currently, you only have _one_ method that you use posted – Paul Samsotha Mar 28 '14 at 20:01
  • you can take a look now –  Mar 28 '14 at 20:20
  • See my **EDIT** Give it a try. It works for me – Paul Samsotha Mar 28 '14 at 20:54
  • The code above doesn't get the # into the table. You can just change it to this `model.addRow(new Object[] {i + 1, starts.get(i), ends.get(i), subs.get(i)});` and it will have the # – Paul Samsotha Mar 28 '14 at 21:00
  • i posted all the class of the gui your way is right but when ever i run the application the only working is vlcj library –  Mar 29 '14 at 10:45
  • Sorry, I don't have vlcj so I dont know – Paul Samsotha Mar 29 '14 at 10:48
  • no i don't need to understand the vlcj library just look at the code and tell me what is the wrong plz –  Mar 29 '14 at 10:58
  • I'm sorry but you keep throwing more and more code at me. I cannot help you debug code if I don't know what I'm looking for. Also I will not try to debug this long code that won't even compile for me. – Paul Samsotha Mar 29 '14 at 11:02
  • ok that is right 100% i edited the code look at the part "the issue with the code is this part of code are not working" where i have to put this code –  Mar 29 '14 at 11:04
  • when i change the place the application are running good but the jtable are not showing any where –  Mar 29 '14 at 11:08
  • If you make the code compilable for me without the vlcj library. I will take a look at it. I can't see anything wrong right now. But if I can run it, I can play with it. – Paul Samsotha Mar 29 '14 at 11:19
  • Also the table will have nothing in it until you load the file. It won't even have headers. – Paul Samsotha Mar 29 '14 at 11:56
  • can i change that fact –  Mar 29 '14 at 15:28
  • So you want the data to load when the application first opens? Or do you just want the table headers? – Paul Samsotha Mar 29 '14 at 15:30
  • just the table headers –  Mar 29 '14 at 15:48
  • You can just do `String[] cols = { ... }; table = new JTable(new DefaultTableModel(cols, 0));` – Paul Samsotha Mar 29 '14 at 15:52
  • and also the button icon of the jtoolbar are not showing –  Mar 29 '14 at 15:54
  • You can just do String[] cols = { ... }; this in the method or the constrictor –  Mar 29 '14 at 16:09
  • That is the String array of your table column nameds – Paul Samsotha Mar 29 '14 at 16:12
  • should i delete it from the method or something else also the icon of open file are not showing in the jtoolbar to uplode the file i tried the change your code by adding Action openAction = new AbstractAction("Open Subtitle", openIcon); but still –  Mar 29 '14 at 16:39
  • mr.hero where did you go –  Mar 29 '14 at 18:43
  • can you explain this more You can just do String[] cols = { ... }; table = new JTable(new DefaultTableModel(cols, 0)); –  Mar 30 '14 at 10:44
  • 1. You have your column headers. 2. You instantiate the table, passing a DefaultTableModel to it, with your column headers, and 0 rows. See where your instantiate your table, that's where it should go. – Paul Samsotha Mar 30 '14 at 10:53
  • if i set the initialization of table = new JTable(new DefaultTableModel(columnNames, 0)); i will have to remove it from the method private DefaultTableModel createModel(String srtPath) i will get error in the model.addRow –  Mar 30 '14 at 11:05
  • No you don't. In your constructor, that's where you initialize your tabble. That' where you want that – Paul Samsotha Mar 30 '14 at 11:09
  • yes and i moved the String columnNames[]... to the constructor i get error in the new DefaultTableModel(columnNames, 0); –  Mar 30 '14 at 11:12
  • Because you can't see `columnNames` in the method, if you put it in the constructor. I didn't tell you to remove it. What you can do though, is just put it as a global field outside the constructor. So both can use it. – Paul Samsotha Mar 30 '14 at 11:13
  • ok now i fix it both the column name and even th DefaultTableModel as global the icon worked fine i will update the other question with the error of not reading the file –  Mar 30 '14 at 11:20
  • but there is an issue with the method of reading the file i open new question http://stackoverflow.com/questions/22735446/error-when-trying-to-load-file-information-java-lang-nullpointerexception –  Mar 30 '14 at 17:59