用户未选择文件时 JAVA 禁用按钮

标签 java swing isenabled

当用户没有选择两个 PDF 时,我想禁用我的合并按钮。 我该怎么做呢?我试图做一个 while 循环来检查 file1file2 是否为 null 但循环没有终止。

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


    }
    }

最佳答案

简单:首先将 mergeButton 或其 Action 设置为禁用:

mergeButton.setEnabled(false);

然后在您的文件选择按钮的 Action 监听器中,如果已选择 2 个文件,则将 mergeButton 设置为启用。

例如注意用 //!!,

标记的主要注释
// !! 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());
    }
}

关于用户未选择文件时 JAVA 禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253040/

相关文章:

用于素数和因式分解的 Java API

java - Cassandra 服务失败

java - 游戏中的按钮未显示正确的消息

java - 将此 Java Swing 应用程序转变为沙盒可嵌入(小程序或 Web Start)的最佳方法是什么?

java - 将数据类型 TYPE_4BYTE_ABGR 的字节数组转换为 BufferedImage

java - 将不同 swing 组件的类实例添加到 JPanel

jquery - 检测单选按钮是禁用还是启用

WPF RadioButtonList 从集合中设置 IsEnabled 和 IsChecked 属性

windows - CheckBox disabled - 仅禁用复选框,不禁用标签

java - 简化比较两个数据集的逻辑