Search code examples
javalisteneractioncall

JAVA | My action listener isn't being called


I'm using an action listener to change a JLabel icon linked to a combo box. I got it working but after adding some code to open a dialog box it stopped calling the action listener (IDK how that broke it). I tried it again from a backup that I saved but couldn't get it to work.

At the moment it loads the default icon but changing the value in the combo box doesn't call the listener like it did before.

This is what my program looks like, it should be showing the German flag here: demo_Image

This is my code:

package LTranslator;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class LangTranslator extends javax.swing.JFrame {

public LangTranslator() {
    initComponents();
}

/**Calls the action listener*/
public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String flagImage = (String)cb.getSelectedItem();
    updateLabel(flagImage);

}

/**Changes the icon */
protected void updateLabel(String name) {
    ImageIcon icon = createImageIcon("/images/" + name + ".png");
    imgOne.setIcon(icon);
    imgOne.setToolTipText("A drawing of a " + name.toLowerCase());
    if (icon != null) {
        imgOne.setText(null);
    } else {
        imgOne.setText("Image not found");
    }
}

/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = LangTranslator.class.getResource(path);
    if (imgURL != null) {
        return new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: " + path);
        return null;
    }
}

private void initComponents() {

    comboOne = new javax.swing.JComboBox<>();
    btnGetLanguage = new javax.swing.JButton();
    btnTranslate = new javax.swing.JButton();
    txtGetLanguage = new javax.swing.JTextField();
    cbxOne = new javax.swing.JCheckBox();
    cbxTwo = new javax.swing.JCheckBox();
    cbxThree = new javax.swing.JCheckBox();
    cbxFour = new javax.swing.JCheckBox();
    imgOne = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    taOne = new javax.swing.JTextArea();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Translator");

    comboOne.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Japanese", "Swedish", "German", "Dutch" }));
    comboOne.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            comboOneActionPerformed(evt);
        }
    });

    btnGetLanguage.setText("Get Language");
    btnGetLanguage.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnGetLanguageActionPerformed(evt);
        }
    });

    btnTranslate.setText("Translate");
    btnTranslate.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            btnTranslateActionPerformed(evt);
        }
    });

    cbxOne.setText("Hello World");

    cbxTwo.setText("How are you?");

    cbxThree.setText("What day is it today?");

    cbxFour.setText("Goodbye");

    imgOne.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Japanese.png"))); // NOI18N

    taOne.setColumns(20);
    taOne.setRows(5);
    taOne.setEnabled(false);
    jScrollPane1.setViewportView(taOne);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(46, 46, 46)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(comboOne, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(35, 35, 35)
                    .addComponent(btnGetLanguage)
                    .addGap(43, 43, 43)
                    .addComponent(txtGetLanguage))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cbxFour)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(cbxTwo)
                                .addComponent(cbxOne)
                                .addComponent(cbxThree))
                            .addGap(31, 31, 31)
                            .addComponent(btnTranslate, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)))
            .addContainerGap())
        .addGroup(layout.createSequentialGroup()
            .addGap(140, 140, 140)
            .addComponent(imgOne)
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(35, 35, 35)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(comboOne, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnGetLanguage)
                .addComponent(txtGetLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(imgOne)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(cbxOne)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(cbxTwo)
                        .addComponent(btnTranslate, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(cbxThree)
                    .addGap(17, 17, 17)
                    .addComponent(cbxFour))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(17, 17, 17))
    );

    pack();
}// </editor-fold>

Solution

  • You add an ActionListener to your ComboBox that is calling comboOneActionPerformed(evt) but the method that triggers the change is named actionPerformed(ActionEvent e). Change this and it should work. I wonder that this does compile at all. You also could implement ActionListener and then add the ActionListener like this: comboOne.addActionListener(this);