Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Default Help with Java Image ViewerGUI

    I have written a program that lets a user load an image of choice to be displayed on the GUI on a push of a button

    but how do i get the Next and Previous buttons to work, I want them to display the next image in a directory for example

    //main class ...............................

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    import java.awt.event.*;
    import java.IO.*;
    import javax.swing.JFileChooser;
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.File;
    import java.io.FileFilter;
    import java.net.URL;
    import javax.swing.*;
    import javax.swing.filechooser.*;
    import java.awt.event.*;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;

    class TutorialImageViewer2 extends JPanel implements ActionListener
    {
    private JLabel label; //contains the image

    JButton button, next, previous;

    private boolean fFirstDone;

    static int fileNumber = 0;

    static int totalFiles;

    public TutorialImageViewer2(){

    this.setLayout(new BorderLayout ());

    JPanel northPanel = new JPanel(new GridLayout(1,1));

    button = new JButton("upload photo");
    northPanel.add(button);

    JPanel centerPanel = new JPanel(new GridLayout(1,1));

    label = new JLabel();
    label.setHorizontalAlignment(SwingConsta…
    label.setVerticalAlignment(SwingConstant…
    centerPanel.add(label);

    JPanel southPanel = new JPanel(new GridLayout(1,1,2,2));

    next = new JButton("next");
    southPanel.add(next);

    previous = new JButton("previous");
    southPanel.add(previous);

    button.addActionListener(this);
    next.addActionListener(this);
    previous.addActionListener(this);
    this.add(northPanel, BorderLayout.NORTH);
    this.add(southPanel, BorderLayout.SOUTH);
    this.add(centerPanel, BorderLayout.CENTER);
    }



    public void actionPerformed(ActionEvent e){

    if (e.getSource() == button)
    {


    JFileChooser chooser = new JFileChooser(".");

    int status = chooser.showOpenDialog(TutorialImageView…

    if(status == JFileChooser.APPROVE_OPTION){

    try{

    fFirstDone = true;

    ImageIcon icon = new ImageIcon(chooser.getSelectedFile().toUR…

    label.setIcon(icon);

    }
    catch (Exception e2){

    System.err.println("Error: " + e2);

    }
    }


    }

    else if (e.getSource() == next)
    {

    if (fFirstDone) {

    try{

    System.err.println("image uploaded ");
    // File[] images = dir.listFiles( new FileFilter() {
    // @Override
    // public boolean accept( File pathname ) {
    // String name = pathname.getName();
    // return name.endsWith( ".png" ) || name.endsWith( ".jpg" );
    // }
    //} );

    }
    catch (Exception e2){

    System.err.println("Error: " + e2);

    }
    }
    else
    {
    System.err.println("image NOT uploaded");
    }
    }

    else if (e.getSource() == previous)
    {
    if (fFirstDone)
    {
    try
    {
    System.err.println("image uploaded ");


    }
    catch (Exception e2)
    {
    System.err.println("Error: " + e2);
    }

    }
    else
    {
    System.err.println("image NOT uploaded");
    }
    }

    }

    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Help with Java Image ViewerGUI

    Please edit the code in your post, add proper indentation and enclose it in code tags:
    BB Code List - Java Programming Forum

    Where is the list of images that you want the code to go through? You need a current image pointer into the list so you know which image is next and which is previous.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Help with Java Image ViewerGUI

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 12-07-2012, 11:44 AM
  2. Replies: 4
    Last Post: 08-09-2012, 06:18 PM
  3. Replies: 1
    Last Post: 07-28-2011, 08:48 AM
  4. Replies: 12
    Last Post: 04-14-2011, 01:58 PM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •