如何在用户单击SAVE -Java之前使空白字段有效

时间:2015-10-17 01:29:52

标签: java validation jdialog inputverifier

这是一个使用JDialog添加或编辑工单的简单程序。当对话框出现时,用户将在字段中输入信息(姓名,日期,职位,计费率等)。该程序必须验证每个字段。问题是如果我将一个字段留空并且我将标签留到下一个字段,则会弹出错误消息。实际上,这不是一个问题,它应该工作,但是,我想这样做,所有空白字段都有效,直到用户点击SAVE。有什么建议或想法吗?我已经粘贴了下面的整个程序,所以请自行编译并运行它,谢谢!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.lang.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
import static javax.swing.GroupLayout.Alignment.*;

public class WorkOrderProject
{
    public static void main (String args[])
    {
        new MyFrameClass();
    }
}

class MyFrameClass extends JDialog implements ActionListener
{
JButton addButton, editButton;
    JPanel buttonPanel;
    WorkOrder workOrderToEdit = new WorkOrder();

    MyFrameClass()
    {
        Container cp;

        addButton = new JButton("ADD");
        addButton.addActionListener(this);
        addButton.setActionCommand("ADD");

        editButton = new JButton("EDIT");
        editButton.addActionListener(this);
        editButton.setActionCommand("EDIT");

        buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);

        cp = getContentPane();
        cp.add(buttonPanel, BorderLayout.NORTH);

        setupMainFrame();
     }

    void setupMainFrame()
    {
        Toolkit    tk;
    Dimension   d;

    tk = Toolkit.getDefaultToolkit();
    d = tk.getScreenSize();
    setLayout(new FlowLayout());
    setTitle("Work Orders");
    setSize(d.width/2, d.height/2);
    setLocation(d.width/4, d.height/4);
    setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
    if(e.getActionCommand().equals("ADD"))
    {
        System.out.println("ADD");
        new MyDialog();
    }
    else if(e.getActionCommand().equals("EDIT"))
    {
        System.out.println("EDIT");
        new MyDialog(workOrderToEdit);
    }
    }
}

class MyDialog extends JDialog implements ActionListener
{
JPanel buttonPanel, fieldPanel;
JButton button1, button2, button3, button4;
GroupLayout layout;
WorkOrder workOrderToEdit;
String[] comboTypes = { "Sales", "Hardware", "Electronics" };
JComboBox<String> comboTypesList;

public MyDialog()
{
    Container cp;

    button1 = new JButton("SAVE");
    button1.addActionListener(this);
    button1.setActionCommand("SAVE");
    button2 = new JButton("CANCEL");
    button2.addActionListener(this);
    button2.setActionCommand("CANCEL");
    button2.setVerifyInputWhenFocusTarget(false);
    button3 = new JButton("SAVE AND NEW");
    button3.addActionListener(this);
    button3.setActionCommand("SAVE AND NEW");

    buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(button2);
    buttonPanel.add(button1);
    buttonPanel.add(button3);

    fieldPanel = setFields();

    cp = getContentPane();
    cp.add(fieldPanel, BorderLayout.NORTH);
    cp.add(buttonPanel, BorderLayout.SOUTH);

    setTitle("Add New Work Order");
    setupMainFrame();

}

public MyDialog(WorkOrder w)
{
    Container cp;

    button1 = new JButton("SAVE");
    button1.addActionListener(this);
    button1.setActionCommand("SAVE");
    button2 = new JButton("CANCEL");
    button2.addActionListener(this);
    button2.setActionCommand("CANCEL");
    button2.setVerifyInputWhenFocusTarget(false);

    buttonPanel = new JPanel(new FlowLayout());
    buttonPanel.add(button2);
    buttonPanel.add(button1);

    fieldPanel = setFields();

    getContentPane().add(fieldPanel, BorderLayout.NORTH);
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

    setTitle("Edit Work Order");
    setupMainFrame();

}


void setupMainFrame()
{
    Toolkit    tk;
    Dimension   d;

    tk = Toolkit.getDefaultToolkit();
    d = tk.getScreenSize();
    setLayout(new FlowLayout());
    setSize(d.width/3, d.height/3);
    setLocation(d.width/3, d.height/3);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setModal(true);
    setVisible(true);
}

JPanel setFields()
{
    GroupLayout layout;
    JPanel p;
    JLabel label1, label2, label3, label4, label5;
    JTextField text1, text2, text3, text4, text5;
    String[] comboTypes = { "-Select-" ,"Sales", "Hardware", "Electronics" };

    comboTypesList = new JComboBox<>(comboTypes);
    comboTypesList.addActionListener(this);

    label1 = new JLabel("Name: ");
    label2 = new JLabel("Department: ");
    label3 = new JLabel("Date of request: ");
    label4 = new JLabel("Date request was fulfilled: ");
    label5 = new JLabel("Billing rate: ");
    text1 = new JTextField(20);
    text1.setInputVerifier(new NameVerifier());
    text2 = new JTextField(20);
    text3 = new JTextField(20);
    text4 = new JTextField(20);
    text5 = new JTextField(20);

    p = new JPanel();

    layout = new GroupLayout(p);
    p.setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
    hGroup.addGroup(layout.createParallelGroup().addComponent(label1).addComponent(label2).addComponent(label3).addComponent(label4).addComponent(label5));
    hGroup.addGroup(layout.createParallelGroup().addComponent(text1).addComponent(comboTypesList).addComponent(text3).addComponent(text4).addComponent(text5));
    layout.setHorizontalGroup(hGroup);


    GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label1).addComponent(text1));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label2).addComponent(comboTypesList));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label3).addComponent(text3));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label4).addComponent(text4));
    vGroup.addGroup(layout.createParallelGroup(BASELINE).addComponent(label5).addComponent(text5));
    layout.setVerticalGroup(vGroup);

    return(p);
}

public void actionPerformed(ActionEvent e)
{
    if(e.getActionCommand().equals("SAVE"))
    {
        System.out.println("SAVED");
        dispose();
    }
    else if(e.getActionCommand().equals("CANCEL"))
    {
        System.out.println("CANCELED");
        dispose();
    }
    else if(e.getActionCommand().equals("SAVE AND NEW"))
    {
        System.out.println("SAVED AND NEW");
    }
}
}

class WorkOrder
{
String name;
int department;
Object dateRequested;
Object dateFulfilled;
String description;
double billingRate;
}

class NameVerifier extends InputVerifier
{
public boolean verify(JComponent input)
{
    String str;
    boolean isValid;
    int score;

    str = ((JTextField)input).getText().trim();

    if(str.equals(""))
    {
        isValid = false;
        JOptionPane.showMessageDialog(input.getParent(), "Name field is blank.", "ERROR", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
        isValid = true;
    }

    return(isValid);
}
}

1 个答案:

答案 0 :(得分:0)

  

问题是如果我将一个字段留空并且我将标签留到下一个字段,则会弹出错误消息。

不要使用InputVerifier。 InputVerifier的目的是在失去焦点时验证文本字段。

  

我想这样做,以便所有空白字段都有效,直到用户点击SAVE。

将验证逻辑(对于所有字段)添加到&#34; Save&#34;的ActionListener中。按钮。

因此,您将在每个文本字段上使用InputVerifier来验证输入数据的格式(输入数据时)。例如,对于&#34;结算率&#34;您将验证该值是否为双数,日期字段包含有效日期。

然后在&#34; Save&#34;监听器验证所有字段是否包含数据。因此,如果数据格式有效且所有字段都包含数据,那么您就知道可以提交论坛进行处理。