开发者

adding an action listener to a JComboBox

开发者 https://www.devze.com 2023-01-23 00:19 出处:网络
I just want to print the selected option in the combo box, to a textfield. Please explain what\'s wrong because i have to complete it & explain it in class. Any help would be greatly appreciated.

I just want to print the selected option in the combo box, to a textfield. Please explain what's wrong because i have to complete it & explain it in class. Any help would be greatly appreciated. Thanks in advance.


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

public class App3 extends JFrame implements ActionListener
{
    private JPanel boxPanel,textPanel;
    private JLabel selectName,selectedName;
    private JComboBox nameCombo;
    private JTextField valueOfSelectedName;
    private Container c;

    public App3()
    {
        super("Combo example");
        setup();
        setSize(200,200);
        setLocation(50,50);
        show();
    }

    public void setup()
    {
        c = getContentPane();

        boxPanel = new JPanel();
        c.add(boxPanel,BorderLayout.NORTH);

        selectName = new JLabel("Select Name : ");
        selectedName = new JLabel("The selected Name : ");

        String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
        nameCombo = new JComboBox(names);
        nameCombo.addActionListener(this);

        valueOfSelectedName = new JTextField(10);

        boxPanel.add(selectName);
        boxPanel.add(nameCombo);

        c.add(textPanel,BorderLayout.CENTER);

        textPanel.add(selectedName);
        textPanel.add(valueOfSelectedName);

    }

    public void actionPerformed(ActionEvent e) 
    {
        JComboBox nameCombo = (JComboBox)e.getSource();
        String newSelection = (Stri开发者_Go百科ng)nameCombo.getSelectedItem();
        valueOfSelectedName.setText(newSelection);
    }

    public static void main(String args[])
    {
        App3 a = new App3();
    }
}

i don't get any compile time errors, i get these errors when i run it.

Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:927)
    at App3.setup(App3.java:42)
    at App3.(App3.java:16)
    at App3.main(App3.java:58)

Process completed.


private JPanel boxPanel,textPanel;
...
textPanel = new JPanel();

You have not created the JPanel object hence, textPanel is pointing to null which is why the Exception is being thrown. Create the object and everything should work fine


textPanel is null when you try to add it to the content pane:

c.add(textPanel, BorderLayout.CENTER);


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class App3 extends JFrame implements ActionListener
{
    private JPanel boxPanel,textPanel;

    private JLabel selectName,selectedName;

    private JComboBox nameCombo;

    private JTextField valueOfSelectedName;

    private Container c;


    public App3()
    {
        super("Combo example");
        setup();
        setSize(200,200);
        setLocation(50,50);
        show();
    }

    public void setup()
    {
        c = getContentPane();

        boxPanel = new JPanel();
        c.add(boxPanel,BorderLayout.NORTH);

        selectName = new JLabel("Select Name : ");
        selectedName = new JLabel("The selected Name : ");

        String[] names = {"Ramila","Hashan","Shaad","Gus","Mahasen","Hasaru","Shabba"};
        nameCombo = new JComboBox(names);
        nameCombo.addActionListener(this);

        valueOfSelectedName = new JTextField(10);

        boxPanel.add(selectName);
        boxPanel.add(nameCombo);

        textPanel = new JPanel(); // panel is not init that why its give error

        c.add(textPanel,BorderLayout.CENTER);

        textPanel.add(selectedName);
        textPanel.add(valueOfSelectedName);

    }

    public void actionPerformed(ActionEvent e) 
    {
        JComboBox nameCombo = (JComboBox)e.getSource();
        String newSelection = (String)nameCombo.getSelectedItem();
        valueOfSelectedName.setText(newSelection);
    }

    public static void main(String args[])
    {
        App3 a = new App3();
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消