Hi guys.
I decided to post this simple Photo viewer application.
Its strictly for beginners, and cab be improved.
/**
*
* @author Neon Tetras
*/
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class ImageViewer extends javax.swing.JFrame {
/**
* Class constructor: ImageViewer
*/
public ImageViewer() {
initComponents();
listFiles(Path); //Lists all the files in the directory on window opening
setLabelIcon(Path,filenames[position]); //sets the label to display the first
//image in the directory on window opening.
PreviousButton.setEnabled(false);
}
/**
*Initialize components
*/
private void initComponents() {
setTitle("Image Viewer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new java.awt.BorderLayout());// The layout is BorderLayout
//setBorder(javax.swing.BorderFactory.createEtchedBorder());
setBackground(java.awt.Color.GRAY);
picLabel = new javax.swing.JLabel(); //Create the Label to display the picture
picLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
picLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
PreviousButton = new javax.swing.JButton();
PreviousButton.setText("Previous");
PreviousButton.setIconTextGap(10); //Distance between the icon and text is 10
PreviousButton.addActionListener(new java.awt.event.ActionListener() { //Register an actionListener for the PreviousButton
public void actionPerformed(java.awt.event.ActionEvent evt) {
PreviousButtonActionPerformed(evt);
}
});
NextButton = new javax.swing.JButton();
NextButton.setPreferredSize(PreviousButton.getPreferredSize());
NextButton.setText("Next");
NextButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
NextButton.setIconTextGap(10); //Distance between the icon and text is 10
NextButton.addActionListener(new java.awt.event.ActionListener() { //Registers an actionListener for the NextButton
public void actionPerformed(java.awt.event.ActionEvent evt) {
NextButtonActionPerformed(evt);
}
});
javax.swing.Box vbox = javax.swing.Box.createVerticalBox(); //VerticalBox to hold the pictureLabel and the buttons
vbox.add(javax.swing.Box.createVerticalStrut(30));
vbox.add(picLabel);
vbox.add(javax.swing.Box.createVerticalStrut(50));
javax.swing.JPanel prev_next_pane = new javax.swing.JPanel(); //Panel to hold the Previous and Next buttons.
java.awt.FlowLayout flow = new java.awt.FlowLayout(java.awt.FlowLayout.CENTER);
flow.setHgap(30);
prev_next_pane.setLayout(flow);
prev_next_pane.add(PreviousButton);
prev_next_pane.add(NextButton);
prev_next_pane.setOpaque(false);
vbox.add(prev_next_pane); //Add the panel to the verticalBox
add(vbox);
java.awt.Toolkit theKit = getToolkit();
java.awt.Dimension size = theKit.getScreenSize();
setLocation(size.width/5,size.height/10);
setMinimumSize(new java.awt.Dimension(900,600));
//setMaximumSize(new Dimension(size.width/4 + 50,size.height/4));
//pack();
}//END:initComponents
/**
*Handler for previous button
*/
private …