0

What I want to do is to display an image when a specified item is selected from the comboBox, knowing that the items of the comboBox change and the number is not fix. This code shows how I add the items which depends on my "for"

for (int j = 1; j <= 6; j++) {
    if (condition) {
    System.out.println(result); 
        combo.addItem("component N°" + j);
    }
}

What I need is to display a certain image when a an item is selected! I really don't know how to do it. I've tried actionperformed with actionlistener, but I didn't know how to relate the item choosing with my images.

I use this method to display an image into JPanel

public static void display(String path, JPanel panel) {
    BufferedImage image = null;
    try {
        image = ImageIO.read(new File(path));
    } catch (IOException e2) {

        e2.printStackTrace();
    }
    Image dimg = image.getScaledInstance(panel.getWidth(), panel.getHeight(),
            Image.SCALE_SMOOTH);
    panel.add(new JLabel(new ImageIcon(dimg)));

}
Braj
  • 46,415
  • 5
  • 60
  • 76
user3475463
  • 45
  • 2
  • 9
  • How do you want to relate the images with the value of combo box? Is the name of images is derived from the selected item? – Braj May 21 '14 at 22:47
  • For example if I select (component N° 2) from the JcomboBox then the image that I need to display is image2.png (j=2) – user3475463 May 21 '14 at 22:51
  • How is the content in the combo related to the images? If you don't know how this works, then nothing we suggest will help. One possibility is to devise a `Object` which contains a `String` and a reference to the image to be loaded. The `String` been used as the content displayed in the combobox and the image so you can load/display it – MadProgrammer May 21 '14 at 23:44
  • 1
    @MadProgrammer I am trying to investigate the issue but OP is sharing the code in chunks. I have wasted more than a hour to solve it but I don't know What OP is doing. Now please you proceed. I am going to sleep. For more info read comments of my post. – Braj May 21 '14 at 23:47
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) Don't add multiple labels to the same space, but just one. Keep a reference to it. On action performed, set a new icon to the single label. – Andrew Thompson May 22 '14 at 00:31
  • @Braj Thank you for your help..When I added "panel.removeAll()" it worked just fine :) – user3475463 May 22 '14 at 07:11
  • I hope you got the issue now. Where were you doing wrong? Basically images are added but they are in the background of first added image because the images is scaled horizontally as well as vertically to fit the whole panel. – Braj May 22 '14 at 07:12
  • Yes I do understand but now the panel SHOULD take the size of the image instead it shows an incomplete image or in a bad quality – user3475463 May 22 '14 at 07:33

2 Answers2

1

For example if I select (component N° 2) from the JcomboBox then the image that I need to display is image2.png (j=2)

Simply add ActionListener and retrieve the number from the selected item. You can use JLabel to show the images. Simply call setIcon() to change the icon.

Sample code:

//final JLabel label = new JLabel();

final ComboBox<String> jComboBox = new JComboBox<String>();
jComboBox.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String value=(String)jComboBox.getSelectedItem();
        int digit =Integer.valueOf(value.replaceAll("component N°","").trim());
        String imageName="image"+digit+".png";

        // show the image  
        // label.setIcon(...);          
    }
});

Please have a look at my another post. It might help you to form the correct image path.


I just had one image displayed but when I change the combobox item nothing happens

EDIT (remove already added image before adding new one)

 public static void display(String path, JPanel panel){
    ...
    panel.removeAll();                          // Remove already added image
    panel.add(new JLabel(new ImageIcon(dimg))); // Add new image
    panel.revalidate();
    panel.repaint();
 }
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
0

To work with JCombobox you need to use the ItemStateChanged method. In that method you can get the selected index. Then from the index you can get the data in the index. Now you have the chosen data and can do what you want to do.

Jamal
  • 763
  • 7
  • 22
  • 32