简体   繁体   English

为swing类重写toString()

[英]Override toString() for swing classes

I want to override toString() method for a FileFilter object. 我想覆盖FileFilter对象的toString()方法。 I have this piece of code 我有这段代码

JFileChooser saveFile = new JFileChooser();
saveFile.setAcceptAllFileFilterUsed(false);
saveFile.setMultiSelectionEnabled(false);
saveFile.setFileFilter(new FileNameExtensionFilter("PNG", ".png"));
String type = saveFile.getFileFilter().toString();
System.out.println(type);

Which prints out something along the lines of javax.swing.filechooser.FileNameExtensionFilter@39cc65b1[description=PNG extensions=[.png]] . 其中打印出javax.swing.filechooser.FileNameExtensionFilter@39cc65b1[description=PNG extensions=[.png]] My goal is to create an overridden toString method, so I would get only the .png part. 我的目标是创建一个重写的toString方法,所以我只得到.png部分。
I KNOW that there are more efficient ways to do this exact task, and I know the basic string functions to get the part I need, but my goal is to do it with an overridden method. 我知道有更有效的方法来完成这个确切的任务,我知道基本的字符串函数来获取我需要的部分,但我的目标是使用重写的方法来完成它。

my goal is to do it with an overridden method. 我的目标是使用重写方法。

This is not possible, for a number of reasons described below. 由于下面描述的多种原因,这是不可能的。 The only way to do it is through composition, not through inheritance. 唯一的方法是通过组合,而不是通过继承。

You can override a method only in classes that you own. 您只能在您拥有的类中覆盖方法。 Since you do not own the FileNameExtensionFilter , an implementation of FileFilter returned by JFileChooser , there is no way for you to override its toString method. 由于您不拥有FileNameExtensionFilterJFileChooser返回的FileFilter实现),因此您无法覆盖其toString方法。

Moreover, FileNameExtensionFilter is a final class , so you cannot override any of its methods. 而且, FileNameExtensionFilter是一个final ,因此您不能覆盖它的任何方法。

The only approach available in this situation is to create your own FileFilter wrapping FileNameExtensionFilter , and pass it the filter that you get from JFileChooser . 在这种情况下唯一可用的方法是创建自己的FileFilter包装FileNameExtensionFilter ,并将它从JFileChooser传递给它。 You would own this class, so you could override its toString as needed: 您将拥有此类,因此您可以根据需要覆盖其toString

class FileFilterWrapper {

    private final FileFilter innerFilter;

    public FileFilterWrapper(FileFilter innerFilter) {
        this.innerFilter = innerFilter;
    }

    @Override
    public String toString(){
        // Use innerFilter properties to produce the desired String
        return ...
    }
}

If you literally just want to do it right here you could do an anonymous inner class for it: 如果你真的想在这里做,你可以为它做一个匿名的内部类:

JFileChooser saveFile = new JFileChooser() {
   @Override
   public String toString() {
      return ".png";
   }
}

I can't recommend that approach though, it would be better to implement your own subclass of JFileChooser and define a new String getFileExtension() method inside that. 我不能推荐这种方法,最好是实现自己的JFileChooser子类并在其中定义一个新的String getFileExtension()方法。

To do that, you need to use a subclass of JFileChooser. 为此,您需要使用JFileChooser的子类。 If that's the only method you want to override, its quite simple. 如果这是您想要覆盖的唯一方法,那么它非常简单。

public class MyFileChooser extends JFileChooser{
    public MyFileChooser(){
        super();
    }

    @Override
    public String toString(){
        //your string conversion code here
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM