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 = (String)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.