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