Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package hr.fer.linolada.artbook.gui;
  2.  
  3. import hr.fer.linolada.artbook.application.ArtBook;
  4.  
  5. import java.awt.Component;
  6. import java.awt.Font;
  7. import java.awt.GridBagConstraints;
  8. import java.awt.GridBagLayout;
  9. import java.awt.Insets;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.io.File;
  13.  
  14. import javax.swing.BorderFactory;
  15. import javax.swing.JButton;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JOptionPane;
  20. import javax.swing.JScrollPane;
  21. import javax.swing.JTextArea;
  22. import javax.swing.JTextField;
  23.  
  24. public class Email extends JFrame {
  25.  
  26. /**
  27. *
  28. */
  29. private static final long serialVersionUID = 1L;
  30. private GridBagConstraints constraints = new GridBagConstraints();
  31. private File attachedFile = null;
  32.  
  33. private ArtBook myArtBook;
  34. private String receiver;
  35.  
  36. public Email(ArtBook myArtBook, String receiver) {
  37.  
  38. this.myArtBook = myArtBook;
  39. this.receiver = receiver;
  40.  
  41. setLayout(new GridBagLayout());
  42.  
  43. constraints.insets = new Insets(5, 5, 5, 5);
  44. this.setBounds(500, 100, 650, 550);
  45. this.setResizable(false);
  46.  
  47. placeComponents();
  48.  
  49. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  50. }
  51.  
  52. private void placeComponents() {
  53.  
  54. JLabel labelTo = new JLabel("To: ");
  55. JLabel labelSubject = new JLabel("Subject: ");
  56.  
  57. JTextField fieldTo = new JTextField(30);
  58. final JTextField fieldSubject = new JTextField(30);
  59.  
  60. JButton buttonSend = new JButton("Send");
  61.  
  62. final JTextArea textAreaMessage = new JTextArea(10, 30);
  63.  
  64. final JTextField filePath = new JTextField(30);
  65.  
  66. constraints.gridx = 0;
  67. constraints.gridy = 0;
  68. labelTo.setFont(new Font(Font.SERIF, Font.BOLD, 16));
  69. add(labelTo, constraints);
  70.  
  71. constraints.gridx = 1;
  72. constraints.fill = GridBagConstraints.HORIZONTAL;
  73. add(fieldTo, constraints);
  74.  
  75. constraints.gridx = 0;
  76. constraints.gridy = 1;
  77. labelSubject.setFont(new Font(Font.SERIF, Font.BOLD, 16));
  78. add(labelSubject, constraints);
  79.  
  80. constraints.gridx = 1;
  81. constraints.fill = GridBagConstraints.HORIZONTAL;
  82. add(fieldSubject, constraints);
  83.  
  84. constraints.gridx = 2;
  85. constraints.gridy = 0;
  86. constraints.gridheight = 2;
  87. constraints.fill = GridBagConstraints.BOTH;
  88.  
  89. buttonSend.setFont(new Font(Font.SERIF, Font.BOLD, 16));
  90. add(buttonSend, constraints);
  91.  
  92. buttonSend.addActionListener(new ActionListener() {
  93.  
  94. @Override
  95. public void actionPerformed(ActionEvent e) {
  96. String subject = fieldSubject.getText();
  97. String message = textAreaMessage.getText();
  98. myArtBook.sendEmailTo(receiver, subject, message, attachedFile);
  99.  
  100. }
  101. });
  102.  
  103. constraints.gridx = 0;
  104. constraints.gridy = 2;
  105. constraints.gridheight = 1;
  106. constraints.gridwidth = 1;
  107.  
  108. final JFileChooser fileChooser = new JFileChooser("Attach File...");
  109. JButton filePicker = new JButton("Attach file... ");
  110. filePicker.setFont(new Font(Font.SERIF, Font.BOLD, 16));
  111.  
  112. filePicker.addActionListener(new ActionListener() {
  113.  
  114. @Override
  115. public void actionPerformed(ActionEvent e) {
  116. int returnVal = fileChooser.showOpenDialog((Component) e
  117. .getSource());
  118. if (returnVal == JFileChooser.APPROVE_OPTION) {
  119. File file = fileChooser.getSelectedFile();
  120. try {
  121.  
  122. filePath.setText(file.getPath());
  123. attachedFile = file;
  124. } catch (Exception ex) {
  125. System.out.println("problem accessing file"
  126. + file.getAbsolutePath());
  127. }
  128. } else {
  129. System.out.println("File access cancelled by user.");
  130. attachedFile = null;
  131. }
  132. }
  133. });
  134.  
  135. add(filePicker, constraints);
  136.  
  137. constraints.gridx = 1;
  138. constraints.gridwidth = 2;
  139. constraints.gridheight = 1;
  140. // constraints.fill = GridBagConstraints.HORIZONTAL;
  141.  
  142. add(filePath, constraints);
  143.  
  144. constraints.gridwidth = 3;
  145. constraints.gridx = 0;
  146. constraints.gridy = 3;
  147. constraints.weightx = 1.0;
  148. constraints.weighty = 1.0;
  149. textAreaMessage.setBorder(BorderFactory.createBevelBorder(1));
  150. add(new JScrollPane(textAreaMessage), constraints);
  151. }
  152.  
  153. public void messageSent(Boolean sent) {
  154. if (sent) {
  155. JOptionPane.showMessageDialog(null, "Poruka je uspješno poslana!");
  156. } else {
  157. JOptionPane.showMessageDialog(null,
  158. "Poruka nije uspješno poslana!", "Error",
  159. JOptionPane.ERROR_MESSAGE);
  160. }
  161. }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement