日期:2014-05-20 浏览次数:20750 次
import java.awt.*; import java.awt.event.*; import java.io.*; import java.lang.reflect.*; import javax.swing.*; import javax.swing.plaf.basic.*; public class TestJFileChooser extends JFileChooser { private boolean isSelected = false; public static void main(String[] args) { new TestJFileChooser().showOpenDialog(null); } @Override public int showDialog(Component parent, String approveButtonText) throws HeadlessException { if (this.ui instanceof BasicFileChooserUI) { try { Method method = BasicFileChooserUI.class.getDeclaredMethod("getApproveButton", JFileChooser.class); method.setAccessible(true); JButton btnApprove = (JButton) method.invoke(this.ui, this); System.out.println(btnApprove); btnApprove.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (TestJFileChooser.this.isSelected) { TestJFileChooser.this.isSelected = false; } else { JOptionPane.showMessageDialog(null, "Please select a file."); } } }); } catch (Exception e) { e.printStackTrace(); } } return super.showDialog(parent, approveButtonText); } @Override public void setSelectedFile(File file) { this.isSelected = true; super.setSelectedFile(file); } }