2

I want to add an image in the panel together with the description but the description appear only in the list whenever I choose a year in my combobox, the problem is that the image didn't show in the lower part of the panel. I guess somethings wrong with my code. Could someone help me about this?

This is what I've tried so far :

public class Main extends JApplet
{
private String[] description;
private JList list = new JList();
private DefaultListModel defaultListModel = new DefaultListModel();
private JComboBox c = new JComboBox();
private JButton b = new JButton("Ok");
private ImageIcon image;

public void init()
{
    try
    {
        description = new String[22];
        description[0] = "1990";
        description[1] = "1991";
        description[2] = "1992";
        description[3] = "1993";
        description[4] = "1994";
        description[5] = "1995";
        description[6] = "1996";
        description[7] = "1997";
        description[8] = "1998";
        description[9] = "1999";
        description[10] = "2000";
        description[11] = "2001";
        description[12] = "2002";
        description[13] = "2003";
        description[14] = "2004";
        description[15] = "2005";
        description[16] = "2006";
        description[17] = "2007";
        description[18] = "2008";
        description[19] = "2009";
        description[20] = "2010";
        description[21] = "2011";
        description[22] = "2012";
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
        e.printStackTrace();
    }

    c = new JComboBox(description);
    list = new JList(defaultListModel);

    list.setBorder(BorderFactory.createLineBorder(Color.black, 1));
    b.setText("<html><b><u>Click</click></b></html>");
    list.setFont(new Font("Garamond", Font.BOLD, 17));
    list.setForeground(Color.BLUE);

    JLabel label = new JLabel(image);

    JPanel down = new JPanel();
    down.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
    down.add(label);

    JPanel panel = new JPanel();

    panel.add(c);
    panel.add(b);

    Container cp = getContentPane();

    cp.add(list, BorderLayout.CENTER);
    cp.add(panel, BorderLayout.NORTH);
    cp.add(down, BorderLayout.SOUTH);

    this.setVisible(true);

    b.addActionListener(
            new ActionListener()
            {
                public void actionPerformed(ActionEvent
                                                    event)
                {
                    int select;
                    select = c.getSelectedIndex();
                    defaultListModel.clear();
                    if (select == 0)
                    {
                        defaultListModel.addElement("the year of 1990");

                        image = new ImageIcon("chicken.gif");
                    }
              }
            });
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
papski
  • 1,241
  • 5
  • 28
  • 52
  • How are you running the code? Are you checking the Java console? Why code an applet rather than a frame? I can probably guess why it is failing, but this will be more instructive for you. BTW - replace the `String[]` with a [`DefaultComboBoxModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html) and loop to `addElement()` 22 times. – Andrew Thompson Mar 16 '12 at 11:31
  • In your `ActionListener` try to use `label.setIcon(new ImageIcon("chicken.gif"))` I'm not sure that create a new Image is enough. (the label should be declare in the class not in init() if you do that) – alain.janinm Mar 16 '12 at 11:39

3 Answers3

2

First of all, you have a mistake at the beginning of init() method, which is not related with your actual question. You have an array of 22 strings and you are trying to assign a value to 23rd index, that's wrong, you will get an error unless you abandon it.

For your actual question, changing the value of an image doesn't change/update the label. Try the code snippet below in actionPerformed() method, however you need to make your label a final or a global variable.

if (select == 0)
{
    try
    {
        label.setIcon(new ImageIcon(ImageIO.read(new File("chicken.gif"))));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Juvanis
  • 25,802
  • 5
  • 69
  • 87
2

There are a number of things you are doing wrong in your code.

  1. The size of the String Array description is 22, and you are adding a value to the index 22, this will lead to ArrayIndexOutOfBoundsException.
  2. The ImageIcon created by you has nothing it's null, so when you add it to JLabel, it will be showing nothing, as expected.
  3. A BorderLayout object has five areas. These areas are specified by the BorderLayout constants: Namely PAGE_START, PAGE_END, LINE_START, LINE_END and CENTER. But you are using NORTH, EAST, WEST, SOUTH approach, this is old.

Here I had Modified your code a bit, have a look, is your image coming or not now.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main extends JApplet
{
    private String[] description;

    private JList list = new JList();

    private DefaultListModel defaultListModel = new DefaultListModel();

    private JComboBox c = new JComboBox();

    private JButton b = new JButton("Ok");

    private ImageIcon image;

    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");


    public void init()
    {


        try
        {


            description = new String[22];

            description[0] = "1990";
            description[1] = "1991";
            description[2] = "1992";
            description[3] = "1993";
            description[4] = "1994";
            description[5] = "1995";
            description[6] = "1996";
            description[7] = "1997";
            description[8] = "1998";
            description[9] = "1999";
            description[10] = "2000";
            description[11] = "2001";
            description[12] = "2002";
            description[13] = "2003";
            description[14] = "2004";
            description[15] = "2005";
            description[16] = "2006";
            description[17] = "2007";
            description[18] = "2008";
            description[19] = "2009";
            description[20] = "2010";
            description[21] = "2011";
            //description[22] = "2012";
        }
        catch (ArrayIndexOutOfBoundsException e)
        {
            e.printStackTrace();
        }

        c = new JComboBox(description);
        list = new JList(defaultListModel);

        list.setBorder(BorderFactory.createLineBorder(Color.black, 1));
        b.setText("<html><b><u>Click</click></b></html>");
        list.setFont(new Font("Garamond", Font.BOLD, 17));
        list.setForeground(Color.BLUE);

        final JLabel label = new JLabel(image);


        JPanel down = new JPanel();
        down.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
        down.add(label);

        JPanel panel = new JPanel();

        panel.add(c);
        panel.add(b);

        Container cp = getContentPane();

        cp.add(list, BorderLayout.CENTER);
        cp.add(panel, BorderLayout.PAGE_START);
        cp.add(down, BorderLayout.PAGE_END);

        this.setVisible(true);


        b.addActionListener(

            new ActionListener()
            {

                public void actionPerformed(ActionEvent
                                                    event)
                {
                    int select;
                    select = c.getSelectedIndex();
                    defaultListModel.clear();
                    if (select == 0)
                    {
                        defaultListModel.addElement("the year of 1990");

                        label.setIcon(infoIcon);

                    }
                    else
                    {
                        label.setIcon(null);
                    }
              }
            });
    }
}

A better approach to access an Image using ImageIO with URL, since image is an Application Resource, so it's much wiser to access it via a URL instead of a File is shown in this post of mine : Access Images via ImageIO

Community
  • 1
  • 1
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
2

I use this to resize my ImageIcon :

               if (select == 0)
                {
                    defaultListModel.addElement("the year of 1990");
                    image = new ImageIcon("chicken.gif") 
                    label.setIcon(new ImageIcon(getScaledImage(image.getImage(), 32, 32))))
                }
  ....

    /**
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
alain.janinm
  • 19,951
  • 10
  • 65
  • 112