[英]How to display music information in a jtable and make the jtable clickable to play the song?
FileNameExtensionFilter ff=new FileNameExtensionFilter("mp3 files",
"mp3", "mpeg3");
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(ff);
fileChooser.setMultiSelectionEnabled(true);
int returnVal = fileChooser.showOpenDialog(fileChooser);
if (returnVal==JFileChooser.APPROVE_OPTION) {
File file[] = fileChooser.getSelectedFiles();
DefaultTableModel dtm = (DefaultTableModel)
home_player.jTable1.getModel();
for (int i = 0; i < file.length; i++) {
Vector v = new Vector();
int num = dtm.getRowCount()+1;
String name = file[i].getName();
String album = file[i].getParentFile().getParentFile().getName();
String art= file[i].getParentFile().getName();
v.add(num);
v.add(name);
v.add(art);
v.add(album);
dtm.addRow(v);
playerp p = new playerp();
p.stop();
}
return Arrays.asList(file);
}
return null;
}
You add a MouseListener
to the JTable
. 您将一个
MouseListener
添加到JTable
。 If you don't know how to do that then read the section from the Swing tutorial on How to Write a MouseListener 如果您不知道该怎么做,请阅读Swing教程中有关如何编写MouseListener的部分。
Then the code to handle the mousePressed()
event might be something like: 然后,用于处理
mousePressed()
事件的代码可能类似于:
if (e.getClickCount() == 2)
{
JTable table = (JTable)e.getSource();
int row = table.rowAtPoint(e.getPoint());
int viewColumn = convertColumnIndexToView(1)
String name = table.getValueAt(row, viewColumn);
// invoke your music player using the name?
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.