1.JFileChooser chooser = new JFileChooser("E:\\file\");
怎样使"E:\\file\"的路径不可改?保存文件时只能改文件名.2.JSpinner中如何限制只可以输入数字(0-9),而不能输入其他任何字符?我使用了事件监听
键盘的输入,但是好像没有效果,在网上找了很久也没有答案.高手快来帮忙解答呀!!!
怎样使"E:\\file\"的路径不可改?保存文件时只能改文件名.2.JSpinner中如何限制只可以输入数字(0-9),而不能输入其他任何字符?我使用了事件监听
键盘的输入,但是好像没有效果,在网上找了很久也没有答案.高手快来帮忙解答呀!!!
解决方案 »
- myeclipse中无法使用jdom问题
- 原来JAVA...
- 求助:如何比较两个文件夹是否一致?
- 关于 JTextPane 的问题?
- 如何设置log4j的相对路径问题和日志日期生成问题
- 请问如何创建目录?
- snmp中的snmpViewMask与snmpSubtree 如何工作啊?谢谢!!!!
- 简单的数组问题
- 关于下载以色列的Magic的工具及一些东东,大家可以到下面的站点去。我刚刚传上去!!!
- Forte(tm) for Java(tm),release 2.0,Community Edition (Build 1160)是什么
- 如何用socket来获取网页的内容(包括网页上的图片)?
- 字符串对换的问题?谢谢!在线等
import java.io.IOException;import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JSpinner;
import javax.swing.UIManager;
import javax.swing.JSpinner.DefaultEditor;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;public class T
{
public static void main(String[] args) throws IOException
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
} JSpinner spinner = new JSpinner();
JSpinner.DefaultEditor editor = (DefaultEditor) spinner.getEditor();
JFormattedTextField tf = editor.getTextField();
tf.setDocument(new PlainDocument() {
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
if (check(str) == false) {
return;
}
super.insertString(offs, str, a);
}
public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException
{
if (check(text) == false) {
return;
}
super.replace(offset, length, text, attrs);
} private boolean check(String text)
{
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < '0' || c > '9') {
return false;
}
}
return true;
}
});
JFrame f = new JFrame();
f.getContentPane().add(spinner, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
大哥,你也特强了,太崇拜你了!第一个问题可以解决吗?
第一个就是不可以改该文件夹当前路径,程序是在Solaris下运行的.
{
public static void main(String[] args)
{
final File destDir = new File("E:/temp"); JFileChooser dirChooser = new JFileChooser() {
public void setCurrentDirectory(File dir)
{
if (destDir.equals(dir)) {
super.setCurrentDirectory(destDir);
}
}
};
dirChooser.setCurrentDirectory(destDir);
dirChooser.showSaveDialog(null);
}
}
第二个问题中的JSpinner为什么>999就不能按键累加了?
上面给的方法报错了:
java.lang.NullPointerException
at LockedDirChooser$1.setCurrentDirectory(LockedDirChooser.java:14)
at javax.swing.JFileChooser.<init>(Unknown Source)
at javax.swing.JFileChooser.<init>(Unknown Source)
at LockedDirChooser$1.<init>(LockedDirChooser.java:11)
at LockedDirChooser.main(LockedDirChooser.java:11)
Exception in thread "main"
JSpinner spinner = new JSpinner();
JSpinner.DefaultEditor editor = (DefaultEditor) spinner.getEditor();改成JSpinner spinner = new JSpinner();
JSpinner.DefaultEditor editor = new JSpinner.NumberEditor(spinner, "#");
spinner.setEditor(editor);因为缺省的Spinner编辑器里面会用逗号作为分节符号,比如 10000 会被表示成 10,000
原来的程序中没有考虑到这种情况。那个NullPointerException的问题有点儿奇怪,我试了如果用1.5的编译器编译就不会有问题。改成这样吧if (destDir != null && destDir.equals(dir)) {
首先谢谢之前的解答,还想问一下,可以使JFileChooser路径显示栏disable掉吗?
JFileChooser用来文件保存时输入'1.txt',如果文件不存在则不会创建'1.txt'?是不是
要用代码实现创建文件并且保存文件呢?
import java.awt.Container;
import java.awt.Dimension;
import java.io.File;
import java.util.ArrayList;
import java.util.List;import javax.swing.JComboBox;
import javax.swing.JFileChooser;public class LockedDirChooser
{
public static void main(String[] args)
{
final File destDir = new File("E:/temp"); JFileChooser dirChooser = new JFileChooser()
{
public void setCurrentDirectory(File dir)
{
if (destDir != null && destDir.equals(dir)) {
super.setCurrentDirectory(destDir);
}
}
};
dirChooser.setCurrentDirectory(destDir);
ArrayList comboBoxList = new ArrayList();
findComboBox(dirChooser, comboBoxList);
for (int i = 0; i < comboBoxList.size(); i++) {
JComboBox comboBox = (JComboBox) comboBoxList.get(i);
if (comboBox.getItemAt(0) instanceof File) {
comboBox.setEnabled(false);
}
}
dirChooser.showSaveDialog(null);
} private static void findComboBox(Container c, List comboBoxList)
{
int n = c.getComponentCount();
for (int i = 0; i < n; i++) {
Component comp = c.getComponent(i);
if (comp instanceof JComboBox) {
comboBoxList.add(comp);
}
else if (comp instanceof Container) {
findComboBox((Container) comp, comboBoxList);
}
}
}
}