ComboBoxModel事件不起作用 [英] ComboBoxModel events not working

查看:118
本文介绍了ComboBoxModel事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎不了解事件"之类的概念.在阅读了有关如何实现侦听器的一段时间之后,我遇到了Java教程,说我应该扩展AbstractListModel来触发数据事件.由于某些原因,它仍然无法正常工作.

I seem not to grasp the concept of Events and such. After reading a while on how to implement the listeners and such I came across the Java tutorial saying I should extend AbstractListModel to get the data event firing. For some reason it still doesn't work.

我在做错什么吗?

addListDataListener(ListDataListener l)应该使用哪种代码才能工作?因为我也不明白.

And what kind of code is expected at addListDataListener(ListDataListener l) for it to work? Since I don't understand that either.

public class CarComboBox extends AbstractListModel<Object> implements ComboBoxModel<Object> {

    private JdbcRowSet jdbc;
    private int size = 0;
    private String selection;

    public CarComboBox() {
        try {
            jdbc = new Query().getCarInfo();

            jdbc.beforeFirst();
            while (jdbc.next()) {
                size++;
            }
            jdbc.beforeFirst();
        }
        catch (SQLException ex) {
            System.err.println(ex.toString());
        }
    }

    @Override
    public void setSelectedItem(Object anItem) {
        selection = (String) anItem;
    }

    @Override
    public Object getSelectedItem() {
        return selection;
    }

    @Override
    public void addListDataListener(ListDataListener l) {
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
    }

    @Override
    public int getSize() {
    return size;
    }

    @Override
    public String getElementAt(int index) {
        try {
            jdbc.absolute(index + 1);
            return jdbc.getString(2);
        }
        catch (SQLException ex) {
            System.out.println(ex.toString());
        }
        return null;
    }
}

然后将侦听器添加到CarComboBox中:

And to add a listener to the CarComboBox I do:

CarComboBox ccb = new CarComboBox();
ccb.addListDataListener(new ListDataListener()

推荐答案

我猜您正在使用官方的

I'm guessing that you are using the official tutorial.

但是,您不应该触摸ListModel和ComboBoxModel.这些是您可能不需要的更高级的功能. 本教程中的4个示例不使用ListModel和ComboBoxModel.

However you should not touch ListModel and ComboBoxModel. Those are more advanced features you probably do not need. The 4 examples in the tutorial do NOT use ListModel and ComboBoxModel.

如果使用标准的JComboBox(没有ListModel或ComboBoxModel),则发生的情况是,当有人进行选择时,会触发ActionEvent.此事件由Swing神奇地触发;您不必担心它是如何生成的.但是,您的责任是使某些(零个,一个或多个)对象能够接收并执行有关ActionEvent的操作:

If you use a standard JComboBox (no ListModel or ComboBoxModel), what happens is that when someone makes a selection, an ActionEvent is fired. This event is magically fired by Swing; you don't have to worry about how it is generated. However what is your responsibility is to have some (zero, one or more) objects being able to receive and do something about the ActionEvent:

public class MyClass implements ActionListener {
   JComboBox comboBox = ...;

   ...
       // You must register explicitly every ActionListener that you
       // want to receive ActionEvent's from comboBox.
       // Here we register this instance of MyClass.
       comboBox.addActionListener(this);
   ...

   @Override 
   public void actionPerformed(ActionEvent e) {
      if (e.getSource() instanceof JComboBox) {
         System.out.println("MyClass registered an ActionEvent from a JComboBox.");
         System.out.println("Selected: " + 
               ((JComboBox) e.getSource()).getSelectedItem());
      }
   }
}

请注意,如果您没有其他不同的Swing组件触发其他ActionEvent, 可以跳过if (e.getSource() instanceof JComboBox),因为您知道ActionEvent始终来自JComboBox.

Note that if you don't have any other ActionEvent's fired by different Swing components you can skip the if (e.getSource() instanceof JComboBox) since you know your ActionEvent always comes from a JComboBox.

在我的示例中,JComboBox位于MyClass内部,但不必一定是:

In my example the JComboBox is inside MyClass, but it does not have to be:

JComboBox comboBox = ...;
MyClass myClass = ...;
comboBox.addActionListener(myClass);
...
comboBox.addActionListener(someOtherActionListener);

这篇关于ComboBoxModel事件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆