Search code examples
javaswingisenabled

JAVA disable button while the user has not selected a file


I want to disable my mergeButton when the user has not selected both PDF'S. How would I go about doing this? I tried to do a while loop to check if file1 and file2 are null but the loop does not terminate.

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!");
                        }
                    }
                }
        );


    }
    }

Solution

  • Simple: Set the mergeButton or its Action disabled to begin with:

    mergeButton.setEnabled(false);
    

    Then in the action listeners of your file choosing buttons, set the mergeButton enabled if 2 files have been chosen.

    e.g. note major comments marked by // !!,

    // !! 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());
        }
    }