2014-09-12 74 views
0

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

这里是我的代码,

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); 

} 

} 

谢谢。

+2

问题是什么? – kiheru 2014-09-12 10:02:43

+0

笏正好你想实现你有错误FileChooser frame = new FileChooser();它应该是JFrame而不是FileChooser() – mussdroid 2014-09-12 10:45:33

+0

恰好我需要的文件应该是以textarea二进制格式打开时从下拉框中选择的二进制文件。 aboue示例工作正常。@ mussdroid – user3531731 2014-09-12 10:55:22

回答

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

+0

谢谢。 @peeskillet – user3531731 2014-09-15 08:00:23

+0

如果您需要进一步帮助,您可能需要更新您的文章(更新后的代码)。不确定你的意思 – 2014-09-15 08:54:26