[英]how to close visibility of “create new folder” in jfilechooser
I have aimed to close "create new folder" button on the file chooser . 我的目的是关闭文件选择器上的“创建新文件夹”按钮。 Is it possible to set visibility of "create new folder" button on the file chooser ?
是否可以设置文件选择器上“创建新文件夹”按钮的可见性? I can set visibility of first component which row start with "look in" words to off but I want set visibility of only "create new folder" not all of them.
我可以设置以“查看”字开始哪一行的第一个组件的可见性关闭但是我想要设置只有“创建新文件夹”的可见性并不是全部。 How can I do that ?
我怎样才能做到这一点 ?
Two suggestions: 两个建议:
You can make the button disabled by accessing the default Action and disabling the Action: 您可以通过访问默认操作并禁用操作来禁用该按钮:
Action folder = fileChooser.getActionMap().get("New Folder");
folder.setEnabled( false );
Or you can use reflection to access the button and make the button invisible: 或者您可以使用反射来访问按钮并使按钮不可见:
//JButton button = SwingUtils.getDescendantOfType(
// JButton.class, fileChooser, "ToolTipText", "Create New Folder");
//button.setVisible(false);
For this approach you will need to use the Swing Utils class. 对于这种方法,您将需要使用Swing Utils类。
Here is a quick demo showing both approaches: 这是一个快速演示,显示了两种方法:
import java.awt.*;
import javax.swing.*;
public class Main
{
public static void main(String[] args)
{
JFileChooser fileChooser = new JFileChooser();
Action folder = fileChooser.getActionMap().get("New Folder");
folder.setEnabled( false );
// Make the button invisible
//JButton button = SwingUtils.getDescendantOfType(
// JButton.class, fileChooser, "ToolTipText", "Create New Folder");
//button.setVisible(false);
fileChooser.showOpenDialog(null);
}
}
I have used icon name to close buttons. 我用图标名来关闭按钮。 My implementation is ;
我的实施是;
JFileChooser fileChooser = new JFileChooser();
// operations related with adjusting JFileChooser user interface
closeButton(fileChooser, "FileChooser.newFolderIcon");
closeButton(fileChooser, "FileChooser.upFolderIcon");
Main function which is going to be called to dis-appear buttons on user interface 将要调用的主要功能是在用户界面上显示按钮
void closeButton(JFileChooser fileChooser, String label){
Icon icon = UIManager.getIcon(label);
closeButtonHelper(fileChooser.getComponents(), icon);
}
void closeButtonHelper(Component[] containers, Icon icon) {
for(Object iterator:containers){
if(iterator instanceof JButton){
Icon temp = icon.getIcon();
if(temp != null && temp == icon){
(JButton.class.cast(iterator)).setVisible(false);
}
} else
if(iterator instanceof Container){
doVisibleHelper(Container.class.cast(iterator).getComponents(), icon);
}
}
}
For toggle button, just add new if-then-else statement, like; 对于切换按钮,只需添加新的if-then-else语句,如;
if(iterator instanceof JToggleButton){
Icon temp = icon.getIcon();
if(temp != null && temp == icon){
(JToggleButton.class.cast(iterator)).setVisible(false);
}
} else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.