2015-08-27 42 views
1

我想在用戶沒有選擇兩個PDF時禁用我的mergeButton。 我會如何去做這件事?我試圖做一個while循環來檢查file1file2是否爲空,但循環沒有終止。當用戶沒有選擇文件時,JAVA禁用按鈕

package pdfMerge; 

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionListener; 
import java.io.File; 
import java.io.IOException; 

import javax.swing.JButton; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 

import org.apache.pdfbox.exceptions.COSVisitorException; 
import org.apache.pdfbox.pdmodel.PDDocument; 
import org.apache.pdfbox.util.PDFMergerUtility; 

import java.awt.event.*; 


public class pdfMerger extends JFrame { 
    private static final String ActionEvent = null; 
    private JButton choose1, choose2, mergeButton; 
    private JFileChooser fileChooser1, fileChooser2; 
    private JPanel contentsPane; 
    private int returnValue1, returnValue2; 
    private File file1, file2; 
    private String fileName1, fileName2; 
    private boolean valid; 


    public pdfMerger(){ 
     super("PDF Merger"); 
     setLayout(new BorderLayout()); 
     setSize(500, 500); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     addComponents(); 
     addAction(); 
     setVisible(true); 

    } 

    public void addComponents(){ 

     contentsPane = new JPanel(new GridLayout(0, 3)); 
     add(contentsPane, BorderLayout.SOUTH); 

     choose1 = new JButton("Choose 1st pdf"); 
     choose2 = new JButton("Choose 2nd pdf"); 

     mergeButton = new JButton("Merge"); 



     fileChooser1 = new JFileChooser(); 
     fileChooser2 = new JFileChooser(); 

     contentsPane.add(choose1); 
     contentsPane.add(choose2); 
     contentsPane.add(mergeButton); 

    } 

    public void addAction(){ 
     choose1.addActionListener(
       new ActionListener(){ 
        public void actionPerformed(ActionEvent event){ 

           if (event.getSource() == choose1){ 
            returnValue1 = fileChooser1.showOpenDialog(null); 
            if (returnValue1 == JFileChooser.APPROVE_OPTION){ 
             file1 = fileChooser1.getSelectedFile(); 
             fileName1 = file1.toString(); 
             fileName1 = fileName1.replace("\\", "\\\\"); 
             System.out.println(fileName1); 


           } 
          } 
         } 
        } 
     ); 
     choose2.addActionListener(
       new ActionListener(){ 
        public void actionPerformed(ActionEvent event){ 
           if (event.getSource() == choose2){ 
            returnValue2 = fileChooser2.showOpenDialog(null); 
            if (returnValue2 == JFileChooser.APPROVE_OPTION){ 
             file2 = fileChooser2.getSelectedFile(); 
             fileName2 = file2.toString(); 
             fileName2 = fileName2.replace("\\", "\\\\"); 
             System.out.println(fileName2); 
           } 
          } 
         } 
        } 
     ); 

     mergeButton.addActionListener(
       new ActionListener(){ 


        public void actionPerformed(ActionEvent event){ 


         PDFMergerUtility ut = new PDFMergerUtility(); 
         ut.addSource(fileName1); 
         ut.addSource(fileName2); 
         ut.setDestinationFileName("C:\\Users\\Shaheedul\\Desktop\\MergedPDF.pdf"); 
         try { 
          ut.mergeDocuments(); 
         } catch (Exception error){ 
          System.out.println("Something went wrong!"); 
         } 
        } 
       } 
     ); 


    } 
    } 
+0

有許多方法可以做到這一點,但這裏是我想嘗試:在節目開始禁用mergeButton,只有啓用如果同時設置了file1和file2,則從choose1並選擇2的動作偵聽器。 –

+0

您當前的用戶界面不太友好。點擊每個「選擇」按鈕後,您應該顯示選定的文件,以便用戶可以確認已選擇文件。 – camickr

回答

4

簡單:設置mergeButton或其行動禁用開始:

mergeButton.setEnabled(false); 

然後在文件選擇按鈕的動作偵聽器,設置啓用mergeButton如果2個文件已經被選定。

例如注意到// !!標註主要意見,

// !! class names should begin with upper case letter 
public class PdfMerger extends JFrame { 
    // ... 
    private JButton choose1, choose2, mergeButton; 
    // ... 

    public PdfMerger() { 
     super("PDF Merger"); 
     setLayout(new BorderLayout()); 
     setSize(500, 500); // advise against this 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     addComponents(); 
     addAction(); 
     setVisible(true); 

     // !! 
     mergeButton.setEnabled(false); 
    } 

    // ... 

    public void addAction() { 
     choose1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 

       if (event.getSource() == choose1) { 
        returnValue1 = fileChooser1.showOpenDialog(null); 
        if (returnValue1 == JFileChooser.APPROVE_OPTION) { 
         file1 = fileChooser1.getSelectedFile(); 
         fileName1 = file1.toString(); 
         fileName1 = fileName1.replace("\\", "\\\\"); 
         System.out.println(fileName1); 

         // !! added 
         mergeButton.setEnabled(bothFilesChosen()); 
        } 
       } 
      } 
     }); 
     choose2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent event) { 
       if (event.getSource() == choose2) { 
        returnValue2 = fileChooser2.showOpenDialog(null); 
        if (returnValue2 == JFileChooser.APPROVE_OPTION) { 
         file2 = fileChooser2.getSelectedFile(); 
         fileName2 = file2.toString(); 
         fileName2 = fileName2.replace("\\", "\\\\"); 
         System.out.println(fileName2); 

         // !! added 
         mergeButton.setEnabled(bothFilesChosen()); 
        } 
       } 
      } 
     }); 

     // .... 

    } 

    // !! 
    private boolean bothFilesChosen() { 
     return (file1 != null && file1.exists() && file2 != null && file2.exists()); 
    } 
} 
4

那麼,當你有一個選擇按鈕,你也許應該顯示在Swing組件所選擇的文件,使用戶有一個視覺線索已被選中哪些文件。如果您不顯示該值,用戶如何知道他們點擊了兩個按鈕?另外,如果「合併」按鈕被禁用,用戶如何知道哪個「選擇」按鈕單擊以啓用合併按鈕?

因此,在這種情況下,您可以使用不可編輯的文本字段。然後,您可以爲每個文本字段添加DocumentListener。無論何時在任何一個字段中更改文字,然後檢查是否啓用/禁用「合併」按鈕。

下面是這種方法的一個例子:

import java.awt.*; 
import java.awt.event.*; 
import java.util.List; 
import java.util.ArrayList; 
import javax.swing.*; 
import javax.swing.event.*; 

public class DataEntered implements DocumentListener 
{ 
    private JButton button; 
    private List<JTextField> textFields = new ArrayList<JTextField>(); 

    public DataEntered(JButton button) 
    { 
     this.button = button; 
    } 

    public void addTextField(JTextField textField) 
    { 
     textFields.add(textField); 
     textField.getDocument().addDocumentListener(this); 
    } 

    public boolean isDataEntered() 
    { 
     for (JTextField textField : textFields) 
     { 
      if (textField.getText().trim().length() == 0) 
       return false; 
     } 

     return true; 
    } 

    @Override 
    public void insertUpdate(DocumentEvent e) 
    { 
     checkData(); 
    } 

    @Override 
    public void removeUpdate(DocumentEvent e) 
    { 
     checkData(); 
    } 

    @Override 
    public void changedUpdate(DocumentEvent e) {} 

    private void checkData() 
    { 
     button.setEnabled(isDataEntered()); 
    } 

    private static void createAndShowUI() 
    { 
     JButton submit = new JButton("Submit"); 
     submit.setEnabled(false); 

     JTextField textField1 = new JTextField(10); 
     JTextField textField2 = new JTextField(10); 

     DataEntered de = new DataEntered(submit); 
     de.addTextField(textField1); 
     de.addTextField(textField2); 

     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(textField1, BorderLayout.WEST); 
     frame.add(textField2, BorderLayout.EAST); 
     frame.add(submit, BorderLayout.SOUTH); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowUI(); 
      } 
     }); 
    } 
} 
相關問題