当前位置: 技术问答>java相关
怎样实现java.awt.FileDialog.setFilenameFilter 方法
来源: 互联网 发布时间:2015-03-03
本文导语: 有代码最好。 | Bug Id 4031440 Votes 488 Synopsis FileDialog doesn't call FilenameFilter.accept() Category java:classes_awt Reported Against 1.1beta3, atlantis_dev2, 1.1.4, 1.0beta2, 3.1, 1.0.2, 1.1beta1, 1.1...
有代码最好。
|
Bug Id 4031440
Votes 488
Synopsis FileDialog doesn't call FilenameFilter.accept()
Category java:classes_awt
Reported Against 1.1beta3, atlantis_dev2, 1.1.4, 1.0beta2, 3.1, 1.0.2, 1.1beta1, 1.1.2, 1.2beta3, 1.1.6, 1.2beta4, 1.1.7a, 1.2.2, kestrel, 1.1.3, 1.0.1, 1.1.1
Release Fixed
State Closed, will not be fixed
Related Bugs 1243038, 4024056, 4044943, 4062249, 4067485, 4265223
Submit Date Feb 10, 1997
Description FileDialog doesn't call FilenameFilter.accept().
--------------------------------------------------------------------------------
Windows NT 4.0, Service Pack 2
Pentium 120
Add a System.out.println to the accept method of Main.java
Compile the example (from Java Class examples (from 1.0.2, in which the bug
also manifests itself)).
Run using java Main
Put any filter in you wish, the accept method is never called, and the list is
not filtered.
Code for Main.java follows:
import java.awt.*;
import java.io.*;
class Main extends Frame implements FilenameFilter {
FileDialog fd;
TextField tfDirectory = new TextField();
TextField tfFile = new TextField();
TextField tfFilter = new TextField();
Main() {
super("FileDialog Example");
add("West", new Button("Load"));
add("East", new Button("Save"));
Panel p = new Panel();
p.setLayout(new GridBagLayout());
addRow(p, new Label("directory:", Label.RIGHT), tfDirectory);
addRow(p, new Label("file:", Label.RIGHT), tfFile);
addRow(p, new Label("filter:", Label.RIGHT), tfFilter);
add("South", p);
pack();
show();
}
// Adds a row in a gridbag layout where the c2 is stretchy
// and c1 is not.
void addRow(Container cont, Component c1, Component c2) {
GridBagLayout gbl = (GridBagLayout)cont.getLayout();
GridBagConstraints c = new GridBagConstraints();
Component comp;
c.fill = GridBagConstraints.BOTH;
cont.add(c1);
gbl.setConstraints(c1, c);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
cont.add(c2);
gbl.setConstraints(c2, c);
}
public boolean accept(File dir, String name) {
System.out.println("File "+dir+" String "+name);
if (fd.getMode() == FileDialog.LOAD) {
return name.lastIndexOf(tfFilter.getText()) > 0;
}
return true;
}
public boolean action(Event evt, Object what) {
boolean load = "Load".equals(what);
if (load || "Save".equals(what)) {
fd = new FileDialog(this, null,
load ? FileDialog.LOAD : FileDialog.SAVE);
fd.setDirectory(tfDirectory.getText());
fd.setFile(tfFile.getText());
fd.setFilenameFilter(this);
fd.show();
tfDirectory.setText(fd.getDirectory());
tfFile.setText(fd.getFile());
// Filter must be the same
if (fd.getFilenameFilter() != this) {
throw new RuntimeException("Internal error");
}
return true;
}
return false;
}
static public void main(String[] args) {
new Main();
}
}
Votes 488
Synopsis FileDialog doesn't call FilenameFilter.accept()
Category java:classes_awt
Reported Against 1.1beta3, atlantis_dev2, 1.1.4, 1.0beta2, 3.1, 1.0.2, 1.1beta1, 1.1.2, 1.2beta3, 1.1.6, 1.2beta4, 1.1.7a, 1.2.2, kestrel, 1.1.3, 1.0.1, 1.1.1
Release Fixed
State Closed, will not be fixed
Related Bugs 1243038, 4024056, 4044943, 4062249, 4067485, 4265223
Submit Date Feb 10, 1997
Description FileDialog doesn't call FilenameFilter.accept().
--------------------------------------------------------------------------------
Windows NT 4.0, Service Pack 2
Pentium 120
Add a System.out.println to the accept method of Main.java
Compile the example (from Java Class examples (from 1.0.2, in which the bug
also manifests itself)).
Run using java Main
Put any filter in you wish, the accept method is never called, and the list is
not filtered.
Code for Main.java follows:
import java.awt.*;
import java.io.*;
class Main extends Frame implements FilenameFilter {
FileDialog fd;
TextField tfDirectory = new TextField();
TextField tfFile = new TextField();
TextField tfFilter = new TextField();
Main() {
super("FileDialog Example");
add("West", new Button("Load"));
add("East", new Button("Save"));
Panel p = new Panel();
p.setLayout(new GridBagLayout());
addRow(p, new Label("directory:", Label.RIGHT), tfDirectory);
addRow(p, new Label("file:", Label.RIGHT), tfFile);
addRow(p, new Label("filter:", Label.RIGHT), tfFilter);
add("South", p);
pack();
show();
}
// Adds a row in a gridbag layout where the c2 is stretchy
// and c1 is not.
void addRow(Container cont, Component c1, Component c2) {
GridBagLayout gbl = (GridBagLayout)cont.getLayout();
GridBagConstraints c = new GridBagConstraints();
Component comp;
c.fill = GridBagConstraints.BOTH;
cont.add(c1);
gbl.setConstraints(c1, c);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
cont.add(c2);
gbl.setConstraints(c2, c);
}
public boolean accept(File dir, String name) {
System.out.println("File "+dir+" String "+name);
if (fd.getMode() == FileDialog.LOAD) {
return name.lastIndexOf(tfFilter.getText()) > 0;
}
return true;
}
public boolean action(Event evt, Object what) {
boolean load = "Load".equals(what);
if (load || "Save".equals(what)) {
fd = new FileDialog(this, null,
load ? FileDialog.LOAD : FileDialog.SAVE);
fd.setDirectory(tfDirectory.getText());
fd.setFile(tfFile.getText());
fd.setFilenameFilter(this);
fd.show();
tfDirectory.setText(fd.getDirectory());
tfFile.setText(fd.getFile());
// Filter must be the same
if (fd.getFilenameFilter() != this) {
throw new RuntimeException("Internal error");
}
return true;
}
return false;
}
static public void main(String[] args) {
new Main();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。