Click to See Complete Forum and Search --> : Passing JLabel back to main GUI Panel!!!


carrotjava
11-08-2004, 01:29 PM
Hi,

Im trying to add a JLabel into a panel, the JLabel contains the result of three drop down boxes brought together by a button event. The program compiles although i get an error message when trying to run it.

Program looks like this at the mo:

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

public class testpanel extends JFrame implements ActionListener
{

public JComboBox days;
public JComboBox months;
public JComboBox years;
public JLabel dateresult; //set JLabel for dateresult
public String date, day, month, year;

public static void main(String[] args)

{
testpanel frame = new testpanel();
}

public testpanel() {

String[] month = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String[] year = { "2004"};
String [] day = new String[31];
for(int i = 1; i < 31; i++){
day[i] = Integer.toString(i);}

JButton okButton;

//set up main frame
JFrame frame = new JFrame();
frame.setTitle("");
frame.setLocation(100,100);
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

//set up panel with compound border
JPanel subpanel2 = new JPanel();

subpanel2.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.red, 3),
BorderFactory.createTitledBorder("Combo Box Test")));

subpanel2.setBackground (Color.lightGray);
subpanel2.setPreferredSize(new Dimension(500,500));

//set up three combo boxes
days = new JComboBox(day);
//days.addItemListener(new Daycombobox());

months = new JComboBox(month);
//months.addItemListener(new Monthcombobox());

years = new JComboBox(year);
//years.addItemListener(new Yearcombobox());

//attach combo boxes too panel
subpanel2.add (days);
subpanel2.add (months);
subpanel2.add (years);

//I think the problem is this.
//add dateresult label to panel
subpanel2.add (dateresult);

//set up panel for ok button
JPanel okPanel = new JPanel();
okButton = new JButton("SELECT");
okButton.addActionListener(this);
okPanel.add(okButton);

//attach panels to frame
frame.getContentPane().add(subpanel2, BorderLayout.NORTH);
frame.getContentPane().add(okPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

}
//deals with ok button
public void actionPerformed(ActionEvent event) {

//get selection from combo boxes and assign to a variable
day = (String) days.getSelectedItem();
month = (String) months.getSelectedItem();
year = (String) years.getSelectedItem();

//JLabel given setText as Day Month Year
dateresult.setText(day+""+month+""+year);

}

public void itemStateChanged(ItemEvent event)
{ }

}
Error Message:

java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:625)
at java.awt.Container.add(Container.java:307)
at testpanel.<init>(testpanel.java:63)
at testpanel.main(testpanel.java:18)
Exception in thread "main"

Anyone got any ideas ?
Thanks
:)

carrotjava
11-08-2004, 01:44 PM
DONT WORRY I SOLVED IT.

I forgot to instantiate the JLabel above where i added it.....woops.

:)