1   package wbKerho;
2   
3   import java.awt.BorderLayout;
4   import java.awt.FlowLayout;
5   
6   import javax.swing.JButton;
7   import javax.swing.JDialog;
8   import javax.swing.JPanel;
9   import javax.swing.WindowConstants;
10  import javax.swing.border.EmptyBorder;
11  import javax.swing.JTextArea;
12  import java.awt.event.ActionListener;
13  import java.awt.event.ActionEvent;
14  import java.awt.print.PrinterException;
15  
16  import javax.swing.JScrollPane;
17  
18  /**
19   * Dialogi, johon on tarkoitus tulostaa
20   * @author vesal
21   * @version 4.1.2011
22   */
23  public class TulostusDialog extends JDialog {
24  
25      private static final long serialVersionUID = 1L;
26      private final JPanel contentPanel = new JPanel();
27      private JTextArea textArea  = new JTextArea();
28      private JScrollPane scrollPane = new JScrollPane(textArea);
29      private final JButton buttonTulosta = new JButton("Tulosta");
30  
31  
32      /**
33       * Create the dialog.
34       */
35      public TulostusDialog() {
36          setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
37          setBounds(100, 100, 450, 510);
38          getContentPane().setLayout(new BorderLayout());
39          contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
40          getContentPane().add(contentPanel, BorderLayout.CENTER);
41          contentPanel.setLayout(new BorderLayout(0, 0));
42          contentPanel.add(scrollPane, BorderLayout.CENTER);
43          JPanel buttonPane = new JPanel();
44          buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
45          getContentPane().add(buttonPane, BorderLayout.SOUTH);
46          JButton okButton = new JButton("OK");
47          okButton.addActionListener(new ActionListener() {
48              @Override
49              public void actionPerformed(ActionEvent arg0) {
50                  dispose();
51              }
52          });
53          buttonTulosta.addActionListener(new ActionListener() {
54              @Override
55              public void actionPerformed(ActionEvent arg0) {
56                  try {
57                      getTextArea().print();
58                  } catch (PrinterException e) {
59                      // e.printStackTrace();
60                  }
61              }
62          });
63          buttonTulosta.setActionCommand("OK");
64  
65          buttonPane.add(buttonTulosta);
66          okButton.setActionCommand("OK");
67          buttonPane.add(okButton);
68          getRootPane().setDefaultButton(okButton);
69      }
70  
71  
72      /**
73       * Palautetaan alue, johon saapi tulostaa
74       * @return alue johon saa tulostaa.
75       */
76      public JTextArea getTextArea() {
77          return textArea;
78      }
79  }
80