Java sample(ComboBox & Label)


run

---(Source List)---

//Example12.java
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Example12{
  private static final String image01_filename = "bk079.jpg";
  private static final String image02_filename = "bk089.jpg";
  private static final String image03_filename = "bk160.jpg";
  private static final String window_title = "ComboBox & Label";
  private static final int locateX = 50;
  private static final int locateY = 50;
  private static final int window_width = 640;
  private static final int window_height = 400;
  private static final long serialVersionUID = 1L;
  JComboBox cb;
  JLabel lbl;
  Icon i01;
  Icon i02;
  Icon i03;
  // Main
  public static void main(final String args[]) {
    final Example12 sample = new Example12();
  }
  // Frame
  public Example12() {
    final JFrame f = new JFrame(Example12.window_title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.i01 = new ImageIcon(Example12.image01_filename);
    this.i02 = new ImageIcon(Example12.image02_filename);
    this.i03 = new ImageIcon(Example12.image03_filename);
    this.cb = new JComboBox();
    final String[] strmenu = { "1999", "2000", "2001" };
    for (final String element : strmenu) {
      this.cb.addItem(element);
    }
    this.lbl = new JLabel("", this.i01, SwingConstants.CENTER);
    this.cb.addItemListener(new SelectItemListener());
    f.getContentPane().add(this.cb, BorderLayout.NORTH);
    f.getContentPane().add(this.lbl, BorderLayout.CENTER);
    f.setBounds(Example12.locateX, Example12.locateY, Example12.window_width,
        Example12.window_height);
    f.setVisible(true);
  }
  // Event
  class SelectItemListener implements ItemListener {
    public void itemStateChanged(final ItemEvent ie) {
      final int imgidx = Example12.this.cb.getSelectedIndex();
      if (imgidx == 0) {
        Example12.this.lbl.setIcon(Example12.this.i01);
      } else if (imgidx == 1) {
        Example12.this.lbl.setIcon(Example12.this.i02);
      } else if (imgidx == 2) {
        Example12.this.lbl.setIcon(Example12.this.i03);
      }
    }
  }
}