I am trying to update the value of the JTextFiwld
to be equal to JTable
cell one of the user told me to use setText()
but this is not the way java going to recognize the cell even if he did how he will get the value from the cell and update it to the JTextField
because every row have 3 cells also I am using SwingWorker
to read files then show the information in the JTable
then the information should be showing in the text field do I have to use ActionListener
or what?
public class GuiInterface extends JFrame {
String[] columnNames = {"#", "Start", "End", "Translation column"};
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();
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");
table = new JTable(new DefaultTableModel(columnNames, 0));
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);
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 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();
Worker p = new Worker(srtPath, table);
p.execute();
}
};
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);
ourFileSelector = new JFileChooser();
Obutton = new JButton[numberOfButton];
etch = BorderFactory.createEtchedBorder();
//starting and ending time
enterText = new JTextField();
enterText.setPreferredSize(new Dimension(0,100));
startTime = new JTextField("0:00:00.00 ");
startTime.setPreferredSize(new Dimension(120, 20));
startTimeingLable = new JLabel("Starting Time");
endTimeingLabel = new JLabel("Ending Time");
endTime = new JTextField("0:00:00.00 ");
endTime.setPreferredSize(new Dimension(120, 20));
//end of start and ending time filed
toolBar = new JToolBar();
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);
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);
toolBar.add(openAction);
itemOpenVideo = new JMenuItem(openAction);
jMenu1.add(itemOpenVideo);
//itemOpenVideo.addActionListener(new MenuBarMethod());
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
}
class Worker extends SwingWorker<DefaultTableModel, Void> {
private final String srtPath;
private final JTable table;
DefaultTableModel model;
public Worker(String srtPath, JTable table) {
this.srtPath = srtPath;
this.table = table;
}
@Override
protected DefaultTableModel doInBackground() {
model = new DefaultTableModel(columnNames, 0);
ArrayList<String> ends = ReadFile.getFileEndingTime(srtPath);
ArrayList<String> starts = ReadFile.getFileStartingTime(srtPath);
ArrayList<String> subs = ReadFile.readSubtitles(srtPath);
ArrayList<String> lins = ReadFile.ArraylineLengths(srtPath);
for (int i = 0; i < ReadFile.maxLine(srtPath); i++) {
model.addRow(new Object[] {lins.get(i), starts.get(i), ends.get(i), subs.get(i)});
}
return model;
}
@Override
protected void done() {
table.setModel(model);
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);
}
}
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();
}
}
}
}
Start by adding a ListSelectionListener
to the table...
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
}
});
This will let you know when the row selection has changed.
In the valueChanged
method, you want to determine which row has been selected...
int row = table.getSelectedRow();
Then you will want to get the value for the specified column...
Object value = table.getValueAt(row, 0);
// 0 represents the first column, change per your requirements...
Now, assuming that the value is simply a String
you could simply use something like...
enterText.setText(value == null ? null : value.toString());
If the value
is an Object
of some other type, you need to cast it first and extract the required information
See How to Use Tables for more details