Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help to fix this code. It will not run and has compile errors. package ne

ID: 3811701 • Letter: P

Question

Please help to fix this code. It will not run and has compile errors.

package net.codejava.swing.mail;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.util.Properties;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import net.codejava.swing.JFilePicker;

/**

* A Swing application that allows sending e-mail messages from a SMTP server.

* @author www.codejava.net

*

*/

public class SwingEmailSender extends JFrame {

    private ConfigUtility configUtil = new ConfigUtility();

     

    private JMenuBar menuBar = new JMenuBar();

    private JMenu menuFile = new JMenu("File");

    private JMenuItem menuItemSetting = new JMenuItem("Settings..");

     

    private JLabel labelTo = new JLabel("To: ");

    private JLabel labelSubject = new JLabel("Subject: ");

     

    private JTextField fieldTo = new JTextField(30);

    private JTextField fieldSubject = new JTextField(30);

     

    private JButton buttonSend = new JButton("SEND");

     

    private JFilePicker filePicker = new JFilePicker("Attached", "Attach File...");

     

    private JTextArea textAreaMessage = new JTextArea(10, 30);

     

    private GridBagConstraints constraints = new GridBagConstraints();

     

    public SwingEmailSender() {

        super("Swing E-mail Sender Program");

         

        // set up layout

        setLayout(new GridBagLayout());

        constraints.anchor = GridBagConstraints.WEST;

        constraints.insets = new Insets(5, 5, 5, 5);

     

        setupMenu();

        setupForm();

         

        pack();

        setLocationRelativeTo(null);    // center on screen

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

    }

    private void setupMenu() {

        menuItemSetting.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent event) {

                SettingsDialog dialog = new SettingsDialog(SwingEmailSender.this, configUtil);

                dialog.setVisible(true);

            }

        });

         

        menuFile.add(menuItemSetting);

        menuBar.add(menuFile);

        setJMenuBar(menuBar);      

    }

     

    private void setupForm() {

        constraints.gridx = 0;

        constraints.gridy = 0;

        add(labelTo, constraints);

         

        constraints.gridx = 1;

        constraints.fill = GridBagConstraints.HORIZONTAL;

        add(fieldTo, constraints);

         

        constraints.gridx = 0;

        constraints.gridy = 1;

        add(labelSubject, constraints);

         

        constraints.gridx = 1;

        constraints.fill = GridBagConstraints.HORIZONTAL;

        add(fieldSubject, constraints);

         

        constraints.gridx = 2;

        constraints.gridy = 0;

        constraints.gridheight = 2;

        constraints.fill = GridBagConstraints.BOTH;

        buttonSend.setFont(new Font("Arial", Font.BOLD, 16));

        add(buttonSend, constraints);

         

        buttonSend.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent event) {

                buttonSendActionPerformed(event);

            }

        });

         

        constraints.gridx = 0;

        constraints.gridy = 2;

        constraints.gridheight = 1;

        constraints.gridwidth = 3;

        filePicker.setMode(JFilePicker.MODE_OPEN);

        add(filePicker, constraints);

         

        constraints.gridy = 3;

        constraints.weightx = 1.0;

        constraints.weighty = 1.0;

         

        add(new JScrollPane(textAreaMessage), constraints);    

    }

     

    private void buttonSendActionPerformed(ActionEvent event) {

        if (!validateFields()) {

            return;

        }

         

        String toAddress = fieldTo.getText();

        String subject = fieldSubject.getText();

        String message = textAreaMessage.getText();

         

        File[] attachFiles = null;

         

        if (!filePicker.getSelectedFilePath().equals("")) {

            File selectedFile = new File(filePicker.getSelectedFilePath());

            attachFiles = new File[] {selectedFile};

        }

         

        try {

            Properties smtpProperties = configUtil.loadProperties();

            EmailUtility.sendEmail(smtpProperties, toAddress, subject, message, attachFiles);

             

            JOptionPane.showMessageDialog(this,

                    "The e-mail has been sent successfully!");

             

        } catch (Exception ex) {

            JOptionPane.showMessageDialog(this,

                    "Error while sending the e-mail: " + ex.getMessage(),

                    "Error", JOptionPane.ERROR_MESSAGE);

        }

    }

     

    private boolean validateFields() {

        if (fieldTo.getText().equals("")) {

            JOptionPane.showMessageDialog(this,

                    "Please enter To address!",

                    "Error", JOptionPane.ERROR_MESSAGE);

            fieldTo.requestFocus();

            return false;

        }

         

        if (fieldSubject.getText().equals("")) {

            JOptionPane.showMessageDialog(this,

                    "Please enter subject!",

                    "Error", JOptionPane.ERROR_MESSAGE);

            fieldSubject.requestFocus();

            return false;

        }

         

        if (textAreaMessage.getText().equals("")) {

            JOptionPane.showMessageDialog(this,

                    "Please enter message!",

                    "Error", JOptionPane.ERROR_MESSAGE);

            textAreaMessage.requestFocus();

            return false;

        }

         

        return true;

    }

     

    public static void main(String[] args) {

        // set look and feel to system dependent

        try {

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch (Exception ex) {

            ex.printStackTrace();

        }

         

        SwingUtilities.invokeLater(new Runnable() {

            @Override

            public void run() {

                new SwingEmailSender().setVisible(true);

            }

        });

    }

}

Explanation / Answer

You are missing the whole package of classes.

Here are the classes you need.

PROGRAM CODE:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class SettingsDialog extends JDialog {
private ConfigUtility configUtil;
private JLabel labelHost = new JLabel("Host name: ");
private JLabel labelPort = new JLabel("Port number: ");
private JLabel labelUser = new JLabel("Username: ");
private JLabel labelPass = new JLabel("Password: ");
private JTextField textHost = new JTextField(20);
private JTextField textPort = new JTextField(20);
private JTextField textUser = new JTextField(20);
private JTextField textPass = new JTextField(20);
private JButton buttonSave = new JButton("Save");
public SettingsDialog(JFrame parent, ConfigUtility configUtil) {
super(parent, "SMTP Settings", true);
this.configUtil = configUtil;
setupForm();
loadSettings();
pack();
setLocationRelativeTo(null);
}
private void setupForm() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
  
add(labelHost, constraints);
  
constraints.gridx = 1;
add(textHost, constraints);
  
constraints.gridy = 1;
constraints.gridx = 0;
add(labelPort, constraints);
  
constraints.gridx = 1;
add(textPort, constraints);

constraints.gridy = 2;
constraints.gridx = 0;
add(labelUser, constraints);
  
constraints.gridx = 1;
add(textUser, constraints);

constraints.gridy = 3;
constraints.gridx = 0;
add(labelPass, constraints);
  
constraints.gridx = 1;
add(textPass, constraints);
  
constraints.gridy = 4;
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
  
buttonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
buttonSaveActionPerformed(event);
}
});
}

private void loadSettings() {
Properties configProps = null;
try {
configProps = configUtil.loadProperties();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error reading settings: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
  
textHost.setText(configProps.getProperty("mail.smtp.host"));
textPort.setText(configProps.getProperty("mail.smtp.port"));
textUser.setText(configProps.getProperty("mail.user"));
textPass.setText(configProps.getProperty("mail.password"));
}

private void buttonSaveActionPerformed(ActionEvent event) {
try {
configUtil.saveProperties(textHost.getText(),
textPort.getText(),
textUser.getText(),
textPass.getText());
JOptionPane.showMessageDialog(SettingsDialog.this,
"Properties were saved successfully!");
dispose();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error saving properties file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}

EmailUtility.java

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailUtility {
public static void sendEmail(Properties smtpProperties, String toAddress,
String subject, String message, File[] attachFiles)
throws AddressException, MessagingException, IOException {
final String userName = smtpProperties.getProperty("mail.user");
final String password = smtpProperties.getProperty("mail.password");
  
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(smtpProperties, auth);

// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");

// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (File aFile : attachFiles) {
MimeBodyPart attachPart = new MimeBodyPart();

try {
attachPart.attachFile(aFile);
} catch (IOException ex) {
throw ex;
}

multipart.addBodyPart(attachPart);
}
}

msg.setContent(multipart);

Transport.send(msg);

}
}

FileTypeFilter.java
package studentsupport9.blogspot.com;

import java.io.File;
import javax.swing.filechooser.FileFilter;

public class FileTypeFilter extends FileFilter {

private String extension;
private String description;

public FileTypeFilter(String extension, String description) {
this.extension = extension;
this.description = description;
}

@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
}
return file.getName().toLowerCase().endsWith(extension);
}

public String getDescription() {
return description + String.format(" (*%s)", extension);
}
}

JFilePicker.java

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class JFilePicker extends JPanel {
private String textFieldLabel;
private String buttonLabel;
private JLabel label;
private JTextField textField;
private JButton button;
private JFileChooser fileChooser;
private int mode;
public static final int MODE_OPEN = 1;
public static final int MODE_SAVE = 2;

public JFilePicker(String textFieldLabel, String buttonLabel) {
this.textFieldLabel = textFieldLabel;
this.buttonLabel = buttonLabel;
fileChooser = new JFileChooser();
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
// creates the GUI
label = new JLabel(textFieldLabel);
  
textField = new JTextField(30);
button = new JButton(buttonLabel);
  
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
buttonActionPerformed(evt);
}
});
  
add(label);
add(textField);
add(button);
  
}

private void buttonActionPerformed(ActionEvent evt) {
if (mode == MODE_OPEN) {
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
} else if (mode == MODE_SAVE) {
if (fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
textField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
}

public void addFileTypeFilter(String extension, String description) {
FileTypeFilter filter = new FileTypeFilter(extension, description);
fileChooser.addChoosableFileFilter(filter);
}

public void setMode(int mode) {
this.mode = mode;
}

public String getSelectedFilePath() {
return textField.getText();
}

public JFileChooser getFileChooser() {
return this.fileChooser;
}
}


SettingsDialog.java

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class SettingsDialog extends JDialog {
private ConfigUtility configUtil;
private JLabel labelHost = new JLabel("Host name: ");
private JLabel labelPort = new JLabel("Port number: ");
private JLabel labelUser = new JLabel("Username: ");
private JLabel labelPass = new JLabel("Password: ");
private JTextField textHost = new JTextField(20);
private JTextField textPort = new JTextField(20);
private JTextField textUser = new JTextField(20);
private JTextField textPass = new JTextField(20);
private JButton buttonSave = new JButton("Save");
public SettingsDialog(JFrame parent, ConfigUtility configUtil) {
super(parent, "SMTP Settings", true);
this.configUtil = configUtil;
setupForm();
loadSettings();
pack();
setLocationRelativeTo(null);
}
private void setupForm() {
setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(10, 10, 5, 10);
constraints.anchor = GridBagConstraints.WEST;
  
add(labelHost, constraints);
  
constraints.gridx = 1;
add(textHost, constraints);
  
constraints.gridy = 1;
constraints.gridx = 0;
add(labelPort, constraints);
  
constraints.gridx = 1;
add(textPort, constraints);

constraints.gridy = 2;
constraints.gridx = 0;
add(labelUser, constraints);
  
constraints.gridx = 1;
add(textUser, constraints);

constraints.gridy = 3;
constraints.gridx = 0;
add(labelPass, constraints);
  
constraints.gridx = 1;
add(textPass, constraints);
  
constraints.gridy = 4;
constraints.gridx = 0;
constraints.gridwidth = 2;
constraints.anchor = GridBagConstraints.CENTER;
add(buttonSave, constraints);
  
buttonSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
buttonSaveActionPerformed(event);
}
});
}

private void loadSettings() {
Properties configProps = null;
try {
configProps = configUtil.loadProperties();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error reading settings: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
  
textHost.setText(configProps.getProperty("mail.smtp.host"));
textPort.setText(configProps.getProperty("mail.smtp.port"));
textUser.setText(configProps.getProperty("mail.user"));
textPass.setText(configProps.getProperty("mail.password"));
}

private void buttonSaveActionPerformed(ActionEvent event) {
try {
configUtil.saveProperties(textHost.getText(),
textPort.getText(),
textUser.getText(),
textPass.getText());
JOptionPane.showMessageDialog(SettingsDialog.this,
"Properties were saved successfully!");
dispose();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
"Error saving properties file: " + ex.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote