【问题标题】:In customized JFileChooser select one from the JComboBox perform that particular action [closed]在自定义的 JFileChooser 中,从 JComboBox 中选择一个执行该特定操作 [关闭]
【发布时间】:2014-09-12 10:01:54
【问题描述】:

我通过自定义 JFileChooser 打开文件然后我添加 JComboBox。当我从 JComboBox 中选择一个为特定文件执行的特定操作时。例如,我的文本文件包含一些文本,如“一旦我们完成”。当我选择二进制形式 JComboBox。它显示在文本区域中,如“ 63 6F FD 70 6C 65 74 一旦我们完成”。

这是我的代码,

public class CustomJFileChooser {
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(
                        UIManager.getSystemLookAndFeelClassName());
                FileChooser frame = new FileChooser();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

 final JFileChooser chooser = new JFileChooser();
 JComponent panel = new JPanel((LayoutManager) new FlowLayout( FlowLayout.LEFT));
 JComboBox comboBox = new JComboBox();
 comboBox.setModel(new DefaultComboBoxModel(new String[] { "text", "binary" }));
 panel.add(new JLabel("FileFormat: "));
 panel.add(comboBox);
 chooser.setAccessory(panel);
 JComponent center = null;
 BorderLayout layout = (BorderLayout) chooser.getLayout();
 for (Component child : chooser.getComponents()) {
        if (BorderLayout.CENTER == layout.getConstraints(child)) {
            center = (JComponent) child;
        }
    }
 if (center != null)
        center.add(panel, BorderLayout.SOUTH);
    final JFrame frame = new JFrame();
    JButton button = new JButton("Open File Chooser");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
            chooser.showOpenDialog(frame);
        }
    });
 frame.getContentPane().add(button);
 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 frame.pack();
 frame.setVisible(true);

}

 }

谢谢。

【问题讨论】:

  • 问题是什么?
  • wat 正是你想要实现的,你有一个错误 FileChooser frame = new FileChooser();它应该是 JFrame 而不是 FileChooser()
  • 当从下拉框中选择二进制项目时,我需要以 textarea 二进制格式打开文件。 aboue 示例正常工作。@mussdroid

标签: java swing action customization jfilechooser


【解决方案1】:

不要只调用showOpenDialog而不获取对结果的引用,而是这样做

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        int result = chooser.showOpenDialog(frame);

然后检查result == JFileChooser.APPROVE_OPTION 是否表示打开 按钮被按下。然后就可以拿到选中的文件了

int result = chooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();

然后您要检查组合框中的选定项目。如果它等于“二进制”,则执行二进制操作,否则执行文本操作。类似的东西

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int result = chooser.showOpenDialog(frame);
        if (result == JFileChooser.APPROVE_OPTION) {
            String type = comboBox.getSelectedItem().toString();
            File file = chooser.getSelectedFile();
            if ("binary".equals(type)) {
                // do binary action
            } else {
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    textArea.read(reader, null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
});

How to use File Choosers 上查看更多信息。

【讨论】:

  • 如果您需要进一步的帮助,您可能需要更新您的帖子(使用更新的代码)。不太清楚你的意思
猜你喜欢
  • 2013-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-06
  • 2022-06-26
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
相关资源
最近更新 更多