Implementing ItemListener

Pickle picture Pickle · Sep 16, 2012 · Viewed 10.2k times · Source

i've been having problem in the itemStateChanged section. when I compiled the program i get the "cannot find variable" error and I can't seem to find out where I did wrong. Any help is much appreciated. Thank You.

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

public class Postcode extends JFrame implements ItemListener {

    public static void main(String[] arg) {
        JFrame fr = new JFrame("Melaka Postcode");
        fr.setSize(240, 125);
        fr.setVisible(true);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public Postcode() {
        String[] code = {"75450", "77000", "78000"};
        JLabel lb1 = new JLabel("Postcode");
        JLabel lb2 = new JLabel("District?");
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JComboBox cb = new JComboBox(code);
        cb.addItemListener(this);
        Font f = new Font("Verdana", Font.BOLD, 14);
        lb2.setFont(f);
        p1.add(lb1);
        p1.add(cb);
        p2.add(lb2);
    }

    public void itemStateChanged(ItemEvent e) {
        if (code.getSelectedItem().equals("75450")) {
            lb2.setText = "Bukit Beruang";
        }
        if (code.getSelectedItem().equals("77000")) {
            lb2.setText = "Jasin";
        }
        if (code.getSelectedItem().equals("75450")) {
            lb2.setText = "Alor Gajah";
        }
    }
}

Answer

Dan D. picture Dan D. · Sep 16, 2012

Your code had a lot of problems, so I have to post here the solution entirely. The fixes:

Declared the variables as member variables

Called getSelectedItem() on the combobox, not on the array of String

Use the JLabel.setText() correctly

import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Postcode extends JFrame implements ItemListener {
    String[] code = { "75450", "77000", "78000" };

    JLabel lb1 = new JLabel("Postcode");

    JLabel lb2 = new JLabel("District?");

    JPanel p1 = new JPanel();

    JPanel p2 = new JPanel();

    JComboBox cb = new JComboBox(code);

    public static void main(String[] arg) {
        JFrame fr = new JFrame("Melaka Postcode");
        fr.setSize(240, 125);
        fr.setVisible(true);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public Postcode() {
        cb.addItemListener(this);

        Font f = new Font("Verdana", Font.BOLD, 14);

        lb2.setFont(f);

        p1.add(lb1);
        p1.add(cb);

        p2.add(lb2);

    }

    public void itemStateChanged(ItemEvent e) {
        if (cb.getSelectedItem().equals("75450"))
            lb2.setText("Bukit Beruang");
        if (cb.getSelectedItem().equals("77000"))
            lb2.setText("Jasin");
        if (cb.getSelectedItem().equals("75450"))
            lb2.setText("Alor Gajah");
    }

}