Advertisement
bojjenclon

Image Viewer - MainWindow

Nov 23rd, 2011
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.File;
  5.  
  6. import javax.swing.BoxLayout;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JFileChooser;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JScrollBar;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.SwingConstants;
  16.  
  17. public class MainWindow extends JFrame implements ActionListener {
  18.     final JFileChooser fc = new JFileChooser();
  19.     JScrollPane scroll;
  20.     JLabel image;
  21.    
  22.     public static void main( String[] args ) {
  23.         new MainWindow();
  24.     }
  25.    
  26.     MainWindow() {
  27.         setTitle( "Image Viewer" );
  28.        
  29.         fc.setFileFilter( new ImageFilter() );
  30.         fc.setAcceptAllFileFilterUsed( false );
  31.        
  32.         image = new JLabel( "No Image Loaded" );
  33.         image.setHorizontalAlignment( SwingConstants.CENTER );
  34.        
  35.         scroll = new JScrollPane( image );
  36.         scroll.setAlignmentX( JScrollPane.CENTER_ALIGNMENT );
  37.        
  38.         JButton load = new JButton( "Load Image" );
  39.         load.setAlignmentX( JButton.CENTER_ALIGNMENT );
  40.         load.setActionCommand( "loadImage" );
  41.         load.addActionListener( this );
  42.        
  43.         JPanel main = new JPanel();
  44.         main.setLayout( new BoxLayout( main, BoxLayout.PAGE_AXIS ) );
  45.         main.add( scroll );
  46.         main.add( load );
  47.        
  48.         add( main, BorderLayout.CENTER );
  49.         pack();
  50.        
  51.         setSize( 640, 480 );
  52.         setLocationRelativeTo( null );
  53.         setVisible( true );
  54.     }
  55.    
  56.     public void actionPerformed( ActionEvent event ) {
  57.         if( event.getActionCommand().equals( "loadImage" ) ) {
  58.             int val = fc.showOpenDialog( this );
  59.            
  60.             if( val == JFileChooser.APPROVE_OPTION ) {
  61.                 image.setIcon( new ImageIcon( fc.getSelectedFile().getAbsolutePath() ) );
  62.                 image.setText( "" );
  63.                
  64.                 JScrollBar vbar = scroll.getVerticalScrollBar();
  65.                 JScrollBar hbar = scroll.getHorizontalScrollBar();
  66.                 vbar.setValue( vbar.getMinimum() );
  67.                 hbar.setValue( hbar.getMinimum() );
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement