简体   繁体   中英

I can't get the string from ActionListener to different class in Java

I created a JComboBox and JButton to submit information. I need the information to be sent to a different class to sort it out with a switch method. But it looks like the string created inside the ActionListener is not recognized by a different class.

public Main() {
    final JComboBox comboB = new JComboBox(b);          //add int b in here for array
    comboB.setBounds(50, 30, 123, 20);
    contentPane.add(comboB);

    JButton btnTest = new JButton("Test");
    btnTest.setBounds(300, 350, 89, 23);
    contentPane.add(btnTest);

    btnTest.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String s = (String)comboB.getSelectedItem();
        }
    });
 }

How do I make it so that String s can be recognized by other classes? I have a separate class that will change action depending on what is selected from ComboBox, but I just can't seem to get this information out. Thank you.

Firstly, other objects need some way to register an ActionListener to the combo box. I would suggest providing a addActionListener method to your class, this would act as a proxy method and simple pass the call onto comboB

Secondly, this means comboB is going to need to be a class instance variable

Thirdly, the other classes are going to need to determine if the action originiated from the combo box or not, for example.

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() instanceof JComboBox) {
        JComboBox cb = (JComboBox)e.getSource();
        String s = (String)cb.getSelectedItem();
    }
}

Now, there's not a lot of context available to the question, but, personally, I would normally either use a model of some kind that your UI class would update and/or a PropertyChangeListener that other classes could register against and monitor for changes to the "properties" of the your main class.

You just need to create a private method and have the combo call that. Then you just navigate to your component/class, and perform the action.

    public Main() {
    final JComboBox comboB = new JComboBox(b);          //add int b in here for array
    comboB.setBounds(50, 30, 123, 20);
    contentPane.add(comboB);

    JButton btnTest = new JButton("Test");
    btnTest.setBounds(300, 350, 89, 23);
    contentPane.add(btnTest);

    btnTest.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String s = (String)comboB.getSelectedItem();
            myMethodThatProcessesS(s);

        }
    });
 }

private void myMethodThatProcessesS(String s) {
     contentPane.getSomeOtherComponent().doSOmething(s);
}

Since java swing implements the MVC pattern you can pass the JComboBox's model reference to other objects.

Models implement the observer pattern and therefore the other objects can register themself if they need to get notified immediatly when the model changes.

public class Main {
    public initializeComponent(OtherClass otherClass) {
        ...
        JComboBox comboBox = ...;
        ComboBoxModel comboBoxModel = comboBox.getModel();
        otherClass.setComboBoxModel(comboBoxModel);
    }
}

public class OtherClass {

    private ComboBoxModel comboBoxModel;

    public void setComboBoxModel(ComboBoxModel comboBoxModel) {
        this.comboBoxModel = comboBoxModel;
        ListDataListener listener = ...;
        comboBoxModel.addListDataListener(listener);
    }

    public String getSelectedItem(){
        Object selectedItem = comboBoxModel.getSelectedItem();
        ...
    }
}

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