简体   繁体   中英

Attaching ActionListener to JComboBox

I am working on a program that has several functions that need to happen based on a user selection from a JComboBox.

Ideally, the user selection would be pulled out and used in a series of if/else statements to change the necessary values.

This is the error I am getting:

method addActionListener in class JComboBox<E> cannot be applied to given types;
        classJComboBox.addActionListener(this);

required: ActionListener

found: DWTools

reason: actual argument DWTools cannot be converted to ActionListener by method invocation   conversion
  where E is a type-variable:
  E extends Object declared in class JComboBox

And here is my code (At the moment, all I have it set to do is put the chosen class in the name JTextField for testing purposes):

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

public class DWTools extends JFrame
{
private String[] classes = { "Bard", "Cleric", "Druid",
"Fighter", "Paladin", "Thief", "Ranger", "Wizard" };
public JTabbedPane mainPane;
public JPanel basic;
public JPanel moves;
public JPanel spells;
private JLabel nameJLabel;
private JLabel classJLabel;
private JTextField nameJTextField;
private JComboBox classJComboBox;

public DWTools()
{
    createUserInterface();
}

public void createUserInterface()
{
    Container contentPane = getContentPane();

    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    contentPane.add(topPanel);

    createBasic();
    createMoves();
    createSpells();

    mainPane = new JTabbedPane();
    mainPane.addTab("Basic Information", basic);
    mainPane.addTab("Character Moves", moves);
    mainPane.addTab("Character Spells", spells);
    topPanel.add(mainPane, BorderLayout.CENTER);

    setTitle("Dungeon World Tools");
    setSize(500, 500);
    setVisible(true);
}

public void createBasic()
{
    basic = new JPanel();
    basic.setLayout( null );

    JLabel nameJLabel = new JLabel("Name:");
    nameJLabel.setBounds(10, 10, 50, 25);
    basic.add(nameJLabel);

    JTextField nameJTextField = new JTextField();
    nameJTextField.setBounds( 60, 10, 100, 25 );
    basic.add( nameJTextField );

    JLabel classJLabel = new JLabel("Class:");
    classJLabel.setBounds(10, 60, 50, 25);
    basic.add(classJLabel);

    Arrays.sort(classes);

    JComboBox classJComboBox = new JComboBox(classes);
    classJComboBox.setBounds( 60, 60, 100, 25 );
    classJComboBox.setMaximumRowCount( 8 );
    basic.add(classJComboBox);
    classJComboBox.addActionListener(this);

public void classJComboBoxActionPerformed(ActionEvent event)
{
    JComboBox box = (JComboBox)event.getSource();
    String chosenClass = (String)box.getSelectedItem();
    updateInfo(chosenClass);
}

protected void updateInfo(String cclass)
{
    String chosen = cclass;
    nameJTextField.setText("Chosen:" + chosen);
}

public void createMoves()
{
    moves = new JPanel();
    moves.setLayout( null );
}

public void createSpells()
{
    spells = new JPanel();
    spells.setLayout( null );
}

public static void main(String[] args)
{
    DWTools application = new DWTools();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

addActionListener() takes an ActionListener as argument. You're passing this as argument. this is an instance of DWTools . And DWTools does not implement the ActionListener interface. Hence the compilation error.

Make DWTools implement ActionListener , or better, pass an instance of an anonymous inner class implementing ActionListener .

In addition to JBNizets answer, I'd like to emphasize that the solution should be a dedicated ActionListener for this combo box. Implementing this interface in the "main class" can quickly lead to a mess when multiple action listeners are required. So one solution could simply be

classJComboBox.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e)
    {
         // Call the already existing method 
         // for handling this event:
         classJComboBoxActionPerformed(e);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM