1919import java .awt .*;
2020import java .awt .event .MouseAdapter ;
2121import java .awt .event .MouseEvent ;
22+ import java .awt .image .BufferedImage ;
2223import java .io .IOException ;
2324import java .net .URISyntaxException ;
2425import java .util .ArrayList ;
@@ -48,12 +49,12 @@ public ProgramWindow() {
4849 frame .setLayout (new BorderLayout ());
4950
5051 JTabbedPane tabbedPane = new JTabbedPane ();
51- JPanel tablePanel = tablePanel ();
52+ JPanel previewPanel = previewPanel ();
5253 JPanel findCDPanel = searchPanel ();
5354 JPanel settingsPanel = settingsPanel ();
5455
5556 tabbedPane .addTab ("Search" , findCDPanel );
56- tabbedPane .addTab ("Table " , tablePanel );
57+ tabbedPane .addTab ("Preview " , previewPanel );
5758 tabbedPane .addTab ("Settings" , settingsPanel );
5859
5960 frame .add (tabbedPane , BorderLayout .CENTER );
@@ -66,16 +67,50 @@ public ProgramWindow() {
6667 * Gets a JPanel for the table panel. This is a helper method.
6768 * @return A JPanel with the table panel.
6869 */
69- private JPanel tablePanel () {
70+ private JPanel previewPanel () {
71+ // Set up panels
7072 JPanel panel = new JPanel (new BorderLayout ());
73+ JPanel imagePanel = new JPanel ();
74+ imagePanel .setLayout (new BoxLayout (imagePanel , BoxLayout .Y_AXIS ));
75+ JPanel buttonPanel = new JPanel (new FlowLayout (FlowLayout .LEFT ));
76+
77+ // Set up buttons
78+ JButton refreshButton = new JButton ("Refresh Preview" );
79+ JButton printButton = new JButton ("Print" );
80+ refreshButton .addActionListener (_ -> {
81+ imagePanel .removeAll ();
82+ BufferedImage [] images = labelGenerator .getAsBufferedImages ();
83+ for (BufferedImage image : images ) {
84+ JLabel label = new JLabel (new ImageIcon (image ));
85+ label .setAlignmentX (Component .CENTER_ALIGNMENT );
86+ imagePanel .add (label );
87+ imagePanel .add (Box .createRigidArea (new Dimension (0 , 10 )));
88+ }
89+ imagePanel .repaint ();
90+ imagePanel .revalidate ();
91+ });
92+ printButton .addActionListener (_ -> {
93+ try {
94+ labelGenerator .printLabel ();
95+ } catch (Exception e ) {
96+ JOptionPane .showMessageDialog (null , "An error occurred while printing. More info: " + e , "Error" , JOptionPane .ERROR_MESSAGE );
97+ }
98+ });
99+
100+ // Make a JSCrollPane for the image panel
101+ JScrollPane imageScrollPane = new JScrollPane (imagePanel );
102+
103+ // Add buttons to the button panel
104+ buttonPanel .add (refreshButton );
105+ buttonPanel .add (printButton );
71106
72- // Set up all the tables for the cd
73- String [] columnNames = {"CD Name" , "Artist" , "Genre" , "Year" , "Track Count" };
74- JTable table = new JTable (new String [][] {new String [] {"None" , "" , "" , "" , "" }}, columnNames );
75- JScrollPane scrollPane = new JScrollPane (table );
107+ // Set up the image panel
108+ imagePanel .setBorder (BorderFactory .createTitledBorder ("Preview" ));
76109
77- // Set up the panel
78- panel .add (scrollPane , BorderLayout .CENTER );
110+
111+ // Add subpanels to main panel
112+ panel .add (imageScrollPane , BorderLayout .CENTER );
113+ panel .add (buttonPanel , BorderLayout .NORTH );
79114
80115 return panel ;
81116 }
@@ -589,5 +624,4 @@ private void validateAndSetDoubleField(JTextField field, Consumer<Double> setter
589624 private void showError (String message ) {
590625 JOptionPane .showMessageDialog (null , message , "Error" , JOptionPane .ERROR_MESSAGE );
591626 }
592-
593627}
0 commit comments