0
votes

This is a simple program that uses a JDialog to add or edit work orders. When the dialog appears, the user will enter information into the fields (name, date, position, billing rate, etc.). The program must validate each of these fields. The problem is that if I leave a field blank and I tab to the next field, the error message pops up. Actually, this isn't a problem, its working as it should, however, I would like to make it so that all blank fields are valid until the user clicks SAVE. Any suggestions or ideas? I have pasted the entire program below so feel free to compile and run it yourself, thank you!

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

1 Answers

0
votes

The problem is that if I leave a field blank and I tab to the next field, the error message pops up.

Don't use an InputVerifier. The purpose of the InputVerifier is to validate a text field when it loses focus.

I would like to make it so that all blank fields are valid until the user clicks SAVE.

Add your validation logic (for all the fields) to the ActionListener of the "Save" button.

So you would use the InputVerifier on each text field to validate the format of the data entered (when the data is entered). For example, for the "billing rate" you would verify that the value is a double number, the date fields contain a valid date.

Then in the "Save" listener you validate that all fields contain data. So if the data is in a valid format and all fields contain data, then you know the forum can be submitted for processing.