单击JRadioButton时更改JLists

时间:2013-04-14 02:05:05

标签: java swing user-interface nullpointerexception

在我的GUI中,我有3个单选按钮,它们是同一个ButtonGroup的一部分。我试图得到它,以便当单击任何一个按钮时,将出现一个JList。在我的动作监听器中,我编写了JLists,但是当我尝试运行它时,我得到一个NullPointerException。我尝试将JList放在动作侦听器之外并且它可以工作,所以我很确定我的动作侦听器有问题。我的按钮出现在我想要的GUI中。

它是主GUI类中的内部类:

    rbGenCancers = new JRadioButton("genCan");
    rbRelCancers = new JRadioButton("relCan");
    rbCancers = new JRadioButton("nonC");
    ButtonGroup bg = new ButtonGroup();
    bg.add(rbGenCancers);
    bg.add(rbRelCancers);
    bg.add(rbCancers);

    JPanel rbButtons = new JPanel(new FlowLayout(3, 2, 2));
    rbButtons.add(rbGenCancers);
    rbButtons.add(rbRelCancers);
    rbButtons.add(rbCancers);
    //added to the JFrame correctly through GridBagLayout and GridBagConstraints

 public class RadioButtonListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        switch(e.getActionCommand()) {
            case "genCan":
                listModel = new DefaultListModel();
                for(int i = 0; i < 8; i++) {
                    cancerNames = _controller.selectGeneralCancer(i);
                    listModel.addElement(cancerNames);
                }
                cancerList = new JList(listModel);
                cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                cancerList.setSelectedIndex(-1);
                cancerList.setVisibleRowCount(3);
                cancerListScroller = new JScrollPane(cancerList);
                break;
            case "relCan":
                listModel = new DefaultListModel();
                for(int i = 0; i < 22; i++) {
                    split = _controller.readCancer(i);
                    cancerNames = split[0];
                    listModel.addElement(cancerNames);
                }
                cancerList = new JList(listModel);
                cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                cancerList.setSelectedIndex(-1);
                cancerList.setVisibleRowCount(3);
                cancerListScroller = new JScrollPane(cancerList);
                break;
            case "nonC":
                listModel = new DefaultListModel();
                for(int i = 22; i < 36; i++) {
                    split = _controller.readCancer(i);
                    cancerNames = split[0];
                    listModel.addElement(cancerNames);
                }
                cancerList = new JList(listModel);
                cancerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                cancerList.setSelectedIndex(-1);
                cancerList.setVisibleRowCount(3);
                cancerListScroller = new JScrollPane(cancerList);
                break;
        }
    }

}

以下是引发的异常:

Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1086)
at java.awt.Container.add(Container.java:998)
at javax.swing.JFrame.addImpl(JFrame.java:562)
at java.awt.Container.add(Container.java:966)
at Cancer.gui.CancerGUI.<init>(CancerGUI.java:306)
at Cancer.run.CancerApp.main(CancerApp.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

这是执行所有代码的类。 printstack中的第19行对应于CancerGUI _gui = new CancerGUI(); CancerGUI中的第306行对应于我通过GridBagConstraints将cancerListScroller添加到JFrame的位置。

public class CancerApp {
public static void main(String[] args) {
    CancerGUI _gui = new CancerGUI();
    Disease _disease = new Disease();
    CancerController _controller = new CancerController(_disease, _gui);
    _gui.setController(_controller);

    CancerApp _app = new CancerApp();
    _app.run(_gui);
}

public void run(CancerGUI _gui) {
    _gui.setVisible(true);   
}
}

1 个答案:

答案 0 :(得分:0)

我有同样的想法。单选按钮有效,但我的动作监听器似乎没有使用setModel(listmodel)设置新列表。我没有例外。当我得到异常时,这是因为类型不匹配。这些可能令人愤怒。我要仔细检查以确保消息正在通过,但它应该。

    else if(ae.getActionCommand().equals("chooseTitle"))
{
  jlst.setModel(lm);
}

它可能工作正常,但我必须做些什么才能让它显示出来?