简体   繁体   中英

Display Images in JFrame from JComboBox event

I want to achieve following functionality :

Eg :

  • When user selects "Profile Pic"item from JComboBox releted images from "Profile Pic" folder should be loaded on same frame.
  • Again when user selects "Product Img" item related images from "Product Img" folder should be loaded replacing previous images.

Following is code snippet , please suggest any changes

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class NewClass1 {

    public static void main(String[] args) {
        createAndShowJFrame();
    }

    public static void createAndShowJFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                JFrame frame = createJFrame();
                frame.setVisible(true);

            }
        });
    }

    private static JFrame createJFrame() {
        JFrame frame = new JFrame();
        //frame.setResizable(false);//make it un-resizeable
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Test");

        ArrayList<BufferedImage> images = null;

        try {
            images = getImagesArrayList();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        final ImageViewPanel imageViewPanel = new ImageViewPanel(images);
        JScrollPane jsp = new JScrollPane(imageViewPanel);
        jsp.setPreferredSize(new Dimension(400, 400));
        frame.add(jsp);

       final  javax.swing.JComboBox filter = new javax.swing.JComboBox<>();
        filter.addItem("All");
        filter.addItem("Profile Pic");
        filter.addItem("Company Logo");
        filter.addItem("Product Img");


        JPanel controlPanel = new JPanel();
        JButton addLabelButton = new JButton("Delete Selected Image");
        addLabelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                imageViewPanel.removeFocusedImageLabel();
            }
        });
        JLabel label =new JLabel("Filter By :");
        filter.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String cat=(String) filter.getSelectedItem();

               createJFrame(cat);
            }
        });
        controlPanel.add(addLabelButton);
        controlPanel.add(label);
        controlPanel.add(filter);
        frame.add(controlPanel, BorderLayout.NORTH);
        frame.pack();



        return frame;
    }

    private static ArrayList<BufferedImage> getImagesArrayList(String cat) throws Exception {
        System.out.println(cat);

        ArrayList<BufferedImage> images = new ArrayList<>();
        if(cat.equals("Profile Pic"))
        images.add(resize(ImageIO.read(new URL("http://192.168.1.25:8080/pic/ProfilePic/1.jpg")), 100, 100));
        else if(cat.equals("Product Img"))
        {
        images.add(resize(ImageIO.read(new URL("http://192.168.1.25:8080/pic/ProductImg/2.jpg")), 100, 100));

        }
        return images;
    }

private static ArrayList<BufferedImage> getImagesArrayList() throws Exception {
       ArrayList<BufferedImage> images = new ArrayList<>();
       images.add(resize(ImageIO.read(new URL("http://localhost:8080/pic/All/a.jpg")), 100, 100));
       images.add(resize(ImageIO.read(new URL("http://localhost:8080/pic/All/b.jpg")), 100, 100));
           return images;
   }
    public static BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(image, 0, 0, width, height, null);
        g2d.dispose();
        return bi;
    }
}

这应该在选择“Profile Pic”项目时显示此图像应在选择“Product Img”项目时显示

I would urge you to have another look at the code I posted (which you seem to be using) Deleting images from JFrame .

However:

In your code I see:

  filter.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String cat=(String) filter.getSelectedItem();

                createJFrame(cat);
            }
        });

I cannot even find the method createJFrame(String cat) ;?

As far as I see you should be doing this:

filter.addActionListener(new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent e) {

        String cat=(String) filter.getSelectedItem();

        ArrayList<BufferedImage> images=getImagesArrayList(cat);//get the new images for the selected item in combo

        //refresh the layout by removing old pics and itertating the new array and adding pics to the panel as you iterate
        layoutLabels(images);
    }

});

  ....

 private JLabel NO_IMAGES=new JLabel("No Images");

 private void layoutLabels(ArrayList<BufferedImage> images) {
        removeAll();//remove all components from our panel (the panel should only have the images on if not use setActionCommand("Image") on your images/JLabels and than use getComponents of JPanel and iterate through them looking for getActionCommand.equals("Image")

        if (images.isEmpty()) {//if the list is empty
            add(NO_IMAGES);//add Jlabel to show message of no images
        } else {
            remove(NO_IMAGES);
            for (BufferedImage i : images) {//iterate through ArrayList of images
                add(new JLabel(new ImageIcon(i)));//add each to the panel using JLabel as container for image
            }
        }

        revalidate();
        repaint();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM