KeyPressed调用JInternalFrame返回Null Exception

时间:2012-12-22 16:45:19

标签: java keyevent jinternalframe

我有一个JInternalFrame,我想在JTextField中按F1时调用另一个JInternalFrame,但是我得到了NullPointerException。这是我的代码

GUI:

jLabel2.setText("ID:");
    getContentPane().add(jLabel2);
    jLabel2.setBounds(10, 240, 15, 25);
    getContentPane().add(jTextField11);
    jTextField11.setBounds(30, 240, 70, 25);
    jTextField11.addKeyListener(this);

KeyEvent的:

public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub
    if( arg0.getKeyCode() == KeyEvent.VK_F1 ){
        listProducto.setVisible(true);******ERROR******
        Main.getInstance().getPortada().getDesktop().add(listProducto);
        Main.getInstance().getPortada().getDesktop()
                .moveToFront(listProducto);
        try {
            listProducto.setSelected(true);
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

问题是我忘了在initComponents();

中初始化searchForm

这是我失踪的代码。

listProducto = new ListProducto();
    listProducto.addSearchEventListener(new searchEventListener() {
        public void resultFound(EventObject e) {
            listProductoSearchPerformed();
        }
    });

对于我的搜索,我创建了一个searchForm来处理searchListeners。我也添加了它的代码。

package com.mbpe.search;

import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JInternalFrame;
import javax.swing.JInternalFrame;
import com.mbpe.gui.Portada;
import com.mbpe.main.Main;
import com.mbpe.main.searchEvent;
import com.mbpe.main.searchEventListener;



@SuppressWarnings("serial")
public class searchForm extends JInternalFrame {
public searchForm(String title, boolean resizable, boolean closable,
        boolean maximizable, boolean iconifiable) {
    super(title, resizable, closable, maximizable, iconifiable);
}

private List _listeners = new ArrayList();
public synchronized void addSearchEventListener(searchEventListener listener) {
    _listeners.add(listener);
}
public synchronized void removeSearchEventListener(searchEventListener listener) {
    _listeners.remove(listener);
}
public synchronized void fireEvent() {
    searchEvent event = new searchEvent(this);
    Iterator i = _listeners.iterator();
    while (i.hasNext()) {
        ((searchEventListener) i.next()).resultFound(event);
    }
}

public void onClose() {

}

public void onClosing() {
    dispose();
}

}