My combo box always returns null value. In my Project "Attendance Management System", I need to validate the selected item in the combo box, but it returns null. How can I get it to return the item selected from the combo box?
I use Eclipse IDE to run my project.
Here is my code:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class admin extends javax.swing.JFrame {
private static final long serialVersionUID=1L;
@SuppressWarnings({ "unchecked", "rawtypes" })
admin(){
//setResizable(false);
JFrame newFrame=new JFrame("Admin");
newFrame.getContentPane().setForeground(new Color(0, 0, 0));
newFrame.getContentPane().setFont(new Font("Arial Black", Font.BOLD, 15));
newFrame.getContentPane().setBackground(new Color(0, 191, 255));
newFrame.setSize(1000,600);
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Welcome Admin");
lblNewLabel.setBounds(416, 75, 185, 28);
lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
newFrame.getContentPane().setLayout(null);
newFrame.getContentPane().add(lblNewLabel);
JButton btnNewButton = new JButton("Go");
btnNewButton.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 255), null,
Color.BLUE, null));
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
btnNewButton.setBounds(471, 343, 85, 35);
btnNewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(comboBox_1.getSelectedItem().toString().equals("Student"))
{
JOptionPane.showMessageDialog(null, "You can Login !","", JOptionPane.ERROR_MESSAGE);
//new Register_Student().setVisible(true);
}
}
});
newFrame.getContentPane().add(btnNewButton);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(0, 0, 255), null, new
Color(0, 0, 255), null));
comboBox_1.setFont(new Font("Arial Black", Font.BOLD, 14));
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Teacher", "Parent"}));
comboBox_1.setBounds(359, 179, 291, 80);
newFrame.getContentPane().add(comboBox_1);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new admin().setVisible(true);
}
});
}
private javax.swing.JComboBox<String> comboBox_1;
}
You have created two combo box objects. Please remove the ComboBox declaration on the top or JComboBox comboBox_1 =
to comboBox_1 =
in the constructor.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
public class admin extends javax.swing.JFrame {
private static final long serialVersionUID=1L;
@SuppressWarnings({ "unchecked", "rawtypes" })
admin(){
//setResizable(false);
JFrame newFrame=new JFrame("Admin");
newFrame.getContentPane().setForeground(new Color(0, 0, 0));
newFrame.getContentPane().setFont(new Font("Arial Black", Font.BOLD, 15));
newFrame.getContentPane().setBackground(new Color(0, 191, 255));
newFrame.setSize(1000,600);
newFrame.setVisible(true);
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("Welcome Admin");
lblNewLabel.setBounds(416, 75, 185, 28);
lblNewLabel.setFont(new Font("Arial Black", Font.BOLD, 18));
newFrame.getContentPane().setLayout(null);
newFrame.getContentPane().add(lblNewLabel);
JComboBox comboBox_1 = new JComboBox();
comboBox_1.setBorder(new BevelBorder(BevelBorder.LOWERED, new Color(0, 0, 255), null, new
Color(0, 0, 255), null));
comboBox_1.setFont(new Font("Arial Black", Font.BOLD, 14));
comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Teacher", "Parent"}));
comboBox_1.setBounds(359, 179, 291, 80);
JButton btnNewButton = new JButton("Go");
btnNewButton.setBorder(new BevelBorder(BevelBorder.RAISED, new Color(0, 0, 255), null,
Color.BLUE, null));
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
btnNewButton.setBounds(471, 343, 85, 35);
btnNewButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
if(comboBox_1.getSelectedItem().toString().equals("Student"))
{
JOptionPane.showMessageDialog(null, "You can Login !","", JOptionPane.ERROR_MESSAGE);
//new Register_Student().setVisible(true);
}
}
});
newFrame.getContentPane().add(btnNewButton);
newFrame.getContentPane().add(comboBox_1);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(admin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new admin().setVisible(true);
}
});
}
private javax.swing.JComboBox<String> comboBox_1;
}