Sunday 15 March 2015

java - Get the next image in directory on clicking JButton - Swing -


I'm displaying an image on the screen. Now I want to display the next image in that directory by pressing the button. And other buttons (Previous) should display the previous image. This is all in the loop so that the first image is displayed after the final image of the directory. This is my code so far:

  import java.awt. *; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.awt.event.KeyEvent; Import java.awt.image.BufferedImage; Import java.io.file; Import java.io.IOException; Import javax.imageio.ImageIO; Import javax.swing *; Import javax.swing.filechooser.FileFilter; Import net.miginfocom.swing.MigLayout; Class image file filter filter extension {@ Override accepts Public Boolean (file F) {if (f.is directory ()) {return true; } String name = f.getAbsolutePath (); If (name.matches (". * ((.jpg) | (.gif) | (.png))")) return true; Second false return; } @ Override public string mill decryption () {"image files (* .jpg, * .gif, * .png)"; }} @SuppressWarnings ("serial") increases towards the bottom of the square JPanel {JButton prev, next; Downpennial () {this.setLayout (new Migabout ("debug", "45% [center] [center]", "")); Prev = new pocket ("back"); Next = new pocket ("next"); Next.addActionListener (New Actionist) {Public Zero Action Action (ActionEvent ae) {}}); This.add (Previous, "100!"); This.add (next, "100!"); }} @SuppressWarnings ( "extends serial") square imgpanel JPanel {imgpanel (JLabel image) {this.setLayout (New MigLayout ( "debug", "", "")); This.add (image, "push, align center"); }} @SuppressWarnings ("serial") class DispImg extension of JFrame {Gemeinberger jemenubar; Jemmenu Jaenmenu; Gemmenitompin, Jaxit; JLabel image; Buffed image img, dmg; DispImg () {// Start the frame this.setTitle ("Display Test"); This.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); This.setExtendedState (JFrame.MAXIMIZED_BOTH); This.setLayout (new Migabout ("debug", "[Filling, Grow]", "[] Push []")); This.setLocationByPlatform (right); This.set minimum size (new dimensions (800, 600)); This.setVisible (true); This.setResizable (wrong); // Create label image = new JLab (""); // Add label to this.add (new imgpanel (image), "wrap"); // Add this pin to the bottom of this pin. (New subparent (), "gaptop 10"); // Creating MenuBar GemmenBar = newJMUR (); Jmenu = new jmnu ("file"); Jopen = new gemenuitime ("Open"); Jopen.setMnemonic (KeyEvent.VK_O); Key Stroke key1 = KeyStroke.getKeyStroke (KeyEvent.VK_O, Event.CTRL_MASK); Jopen.setAccelerator (Key 1); JopenkaddActionListener (new ActionListener () {public void actionPerformed (ActionEvent ae) {JFileChooser fc = new JFileChooser (); fc.setFileFilter (New ImageFilter ()); int result = FckshowOpenDialog (zero), if (result == JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile (); try {img = ImageIO.read (file); float width = img.getWidth (); float height = img.getHeight (); if (img.getHeight () & gt; 500 & amp; & amp; (width / height) & gt; 1) {image thumb = img.getScaledInstance (-1, 620, Image.SCALE_SMOOTH); image.setIcon (new ImageIcon (thumb));} else if (img .getHeight () & gt; 500 & amp; & amp; (width / height) & lt; = 1) {image thumb = img.getScaledInstance (460, -1, Image.SCALE_SMOOTH) ; Image.setIcon (New ImageIcon (thumb));} other {image.setIcon (New Image Icon (img))}}} catch (IOException e) {e.printStackTrace ()}}}}}); Jexit = new JMenuItem ("Exit"); Jexit.addActionListener (new executable () {executed public zero verb (ActionEvent AE) {System.exit (0);}}); Jmenu.add (jopen); Jmenu.addSeparator (); Jmenu.add (jexit); Jmenubar.add (jmenu); This.setJMenuBar (jemenubar); } Public static zero main (string s []) {SwingUtilities.invokeLater (new runnab) (Public run zero () {New DispImg ();}}); }}    

When I added the ActionListener () on the next button nothing The new image has not been displayed

There is no code in your action, so I'm not sure what you expect to happen.

You will probably use JFileChooser to select the directory (and perhaps the initial image to display).

Once you have a directory, you can use the File.listFiles (...) method to get a list of all image files in the directory. Then your next / previous button will have to add / subtract the next / previous file to access the array.

No comments:

Post a Comment