Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hr.fer.linolada.artbook.gui;
- import hr.fer.linolada.artbook.application.ArtBook;
- import java.awt.Component;
- 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 javax.swing.BorderFactory;
- import javax.swing.JButton;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- public class Email extends JFrame {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private GridBagConstraints constraints = new GridBagConstraints();
- private File attachedFile = null;
- private ArtBook myArtBook;
- private String receiver;
- public Email(ArtBook myArtBook, String receiver) {
- this.myArtBook = myArtBook;
- this.receiver = receiver;
- setLayout(new GridBagLayout());
- constraints.insets = new Insets(5, 5, 5, 5);
- this.setBounds(500, 100, 650, 550);
- this.setResizable(false);
- placeComponents();
- setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- }
- private void placeComponents() {
- JLabel labelTo = new JLabel("To: ");
- JLabel labelSubject = new JLabel("Subject: ");
- JTextField fieldTo = new JTextField(30);
- final JTextField fieldSubject = new JTextField(30);
- JButton buttonSend = new JButton("Send");
- final JTextArea textAreaMessage = new JTextArea(10, 30);
- final JTextField filePath = new JTextField(30);
- constraints.gridx = 0;
- constraints.gridy = 0;
- labelTo.setFont(new Font(Font.SERIF, Font.BOLD, 16));
- add(labelTo, constraints);
- constraints.gridx = 1;
- constraints.fill = GridBagConstraints.HORIZONTAL;
- add(fieldTo, constraints);
- constraints.gridx = 0;
- constraints.gridy = 1;
- labelSubject.setFont(new Font(Font.SERIF, Font.BOLD, 16));
- 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(Font.SERIF, Font.BOLD, 16));
- add(buttonSend, constraints);
- buttonSend.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String subject = fieldSubject.getText();
- String message = textAreaMessage.getText();
- myArtBook.sendEmailTo(receiver, subject, message, attachedFile);
- }
- });
- constraints.gridx = 0;
- constraints.gridy = 2;
- constraints.gridheight = 1;
- constraints.gridwidth = 1;
- final JFileChooser fileChooser = new JFileChooser("Attach File...");
- JButton filePicker = new JButton("Attach file... ");
- filePicker.setFont(new Font(Font.SERIF, Font.BOLD, 16));
- filePicker.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- int returnVal = fileChooser.showOpenDialog((Component) e
- .getSource());
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- try {
- filePath.setText(file.getPath());
- attachedFile = file;
- } catch (Exception ex) {
- System.out.println("problem accessing file"
- + file.getAbsolutePath());
- }
- } else {
- System.out.println("File access cancelled by user.");
- attachedFile = null;
- }
- }
- });
- add(filePicker, constraints);
- constraints.gridx = 1;
- constraints.gridwidth = 2;
- constraints.gridheight = 1;
- // constraints.fill = GridBagConstraints.HORIZONTAL;
- add(filePath, constraints);
- constraints.gridwidth = 3;
- constraints.gridx = 0;
- constraints.gridy = 3;
- constraints.weightx = 1.0;
- constraints.weighty = 1.0;
- textAreaMessage.setBorder(BorderFactory.createBevelBorder(1));
- add(new JScrollPane(textAreaMessage), constraints);
- }
- public void messageSent(Boolean sent) {
- if (sent) {
- JOptionPane.showMessageDialog(null, "Poruka je uspješno poslana!");
- } else {
- JOptionPane.showMessageDialog(null,
- "Poruka nije uspješno poslana!", "Error",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement