编辑文件名在JComboBox中的显示方式

编辑文件名在JComboBox中的显示方式

本文介绍了编辑文件名在JComboBox中的显示方式,同时保持对文件的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,也是堆栈溢出的新手.

I am very new to Java and brand new to stack overflow.

我正在尝试使用JMF API创建一个用Java编码的简单媒体播放器.到目前为止,我已经能够使用称为playListHolderJComboBox设置一个简单的队列/播放列表来保存歌曲文件.当用户从菜单栏中选择打开时,他们会从JFileChooser中选择要添加的歌曲.然后使用addItem()方法将歌曲文件添加到playListHolder.当在playListHolder中选择一个项目并且用户单击play按钮时,将使用playListHolder.getSelectedItem()为文件对象文件分配要播放的项目.下面的代码部分和相关变量:

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

        }
    }

我想做的是将歌曲文件显示在JComboBox中,并且只包含文件名,而不是file.toString()JComboBox中设置的整个路径.

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.

因此,到目前为止,我尝试将file.getName()添加到包装盒中,但很快意识到我的笨拙.这样做只会在框中添加文件名的String,这样,当您使用媒体播放器中的play按钮实际播放文件时,它将无法找到文件并引发异常.

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.

我还尝试创建一个具有toString()方法的FileWrapper类,该方法利用file.getName()方法仅返回文件名,然后将该FileWrapper而不是直接添加到文件对象中.我得到了相同的结果.

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.

推荐答案

我认为这就是您想要的,为您的comboBox创建自定义渲染器

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;
        }
});

了解更多信息如何使用ComboBox

这篇关于编辑文件名在JComboBox中的显示方式,同时保持对文件的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:27