问题
I am very new to Java and brand new to stack overflow.
I am attempting to create a simple media player coded in Java utilizing the JMF API. Thus far I have been able to set up a simple queue/playlist to hold song files using a JComboBox
called playListHolder
. When the user selects open from the menu bar, they select a song they want to add from a JFileChooser
. The song file is then added to playListHolder
using the addItem()
method. When an item is selected in playListHolder
and the user clicks the play
button, a File object file is assigned the item to be played using playListHolder.getSelectedItem()
. Section of code and relevant variables below:
File file;
Player p;
Component cont;
Container c;
Component visual;
JButton play = new JButton("Play");
play.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
file = (File) playListHolder.getSelectedItem();
startplay();
}
});
public void openFile() {
JFileChooser filech = new JFileChooser();
int result = filech.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) {
file = null;
} else {
file = filech.getSelectedFile();
playListHolder.addItem(file);
;
}
}
public void startplay() {
if (file == null)
return;
removepreviousplayer();
try {
p = Manager.createPlayer(file.toURI().toURL());
p.addControllerListener(new ControllerListener() {
public void controllerUpdate(ControllerEvent ce) {
if (ce instanceof RealizeCompleteEvent) {
c = getContentPane();
cont = p.getControlPanelComponent();
visual = p.getVisualComponent();
if (visual != null)
c.add(visual, BorderLayout.CENTER);
if (cont != null)
c.add(cont, BorderLayout.SOUTH);
c.doLayout();
}
}
});
p.start();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid file or location",
"Error loading file", JOptionPane.ERROR_MESSAGE);
}
}
What I would like to do, is have the song files appear in the JComboBox
with just the filename, not the entire path that the file.toString()
sets up in the JComboBox
.
Thus far, I tried just adding the file.getName()
to the box, but quickly realized my noobishness. Doing this only adds a String
of the file name to the box, such that when you use the play
button in the media player to actually playback the file, it fails to find the file and throws an exception.
I also tried creating a FileWrapper
class that had a toString()
method which utilized the file.getName()
method to return just the file name, and then added that FileWrapper
to the box instead of the file object directly. I got the same result.
I am certain that it is just my amateur level of knowledge that is creating this stumbling block, there has to be a simple way to do this, but believe it or not I could not seem to find one, at lest not one written in a way I easily understood. Any help is much appreciated.
回答1:
I think this is what are you looking for, make a custom renderer for your comboBox
myComboBox.setRenderer( new DefaultListCellRenderer(){
@Override
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
if(value == null){
return this;
}
if(value instanceof File){
File song = (File)value;
setText(song.getName());
}else{
setText(value.toString());
}
return this;
}
});
Read more How to use ComboBox
回答2:
Formatting content on the screen often falls to a renderer. In this case a ListCellRenderer
.
Take a look at How to use combo boxes and in particualr Providing a Custom Renderer
来源:https://stackoverflow.com/questions/19919635/editing-how-file-name-is-displayed-in-jcombobox-while-maintaining-access-to-file