1

The warning show up after i use JComboBox i tried to hide it with @SuppressWarnings("deprecation") but it didn't worked after i recompiled it with -Xlint:unchecked it showed Anpr.java:61: warning: [unchecked] unchecked call to JComboBox(E[]) as a member of the raw type JComboBox gen = new JComboBox(new String[] {"Male","Female"}); where E is a type-variable:E extends Object declared in class JComboBox

import javax.swing.*;
@SuppressWarnings("deprecation")
public class Anpr extends JFrame{
            ImageIcon icon; 
            ImageIcon img3;
            JLabel imglab3,label3,label4,label5,label6,label7,label8,label9,label10;
            Dimension dimension;
            JTextField pid,name,cno,age,bgrp,addr,anydis;
            JComboBox gen; 
     Anpr(){    
                dimension = Toolkit.getDefaultToolkit().getScreenSize();      
                int x=(int)((dimension.getWidth() - 650)/2);
                int y=(int)((dimension.getHeight() - 650)/2);
                setLocation(x, y);
                setSize(600,600);
                setLayout(null);
                icon = new ImageIcon("image/medical-record.png");
                setIconImage(icon.getImage());
                setTitle("Add New Patient Record");
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                label3 = new JLabel("Patient ID");
                label3.setBounds(150,50,80,30);
                label4 = new JLabel("Name");
                label4.setBounds(150,90,80,30);
                label5 = new JLabel("Contact No");
                label5.setBounds(150,130,80,30);
                label6 = new JLabel("Age");
                label6.setBounds(150,170,80,30);
                label7 = new JLabel("Gender");
                label7.setBounds(150,210,80,30);
                label8 = new JLabel("Blood Group");
                label8.setBounds(150,250,80,30);
                label9 = new JLabel("Address");
                label9.setBounds(150,290,80,30);
                label10 = new JLabel("Any Major Disease Suffered Earlier");
                label10.setBounds(150,330,200,30);
                add(label3);
                add(label4);
                add(label5);
                add(label6);
                add(label7);
                add(label8);
                add(label9);
                add(label10);
                JButton btnsa=new JButton("Save");
                JButton btnco=new JButton("Close");
                btnsa.setBounds(150,450,80,30);
                btnco.setBounds(350,450,80,30);
                add(btnsa);
                add(btnco);
                pid = new JTextField();
                pid.setBounds(270,50,180,30);
                name = new JTextField();
                name.setBounds(270,90,180,30);
                cno = new JTextField();
                cno.setBounds(270,130,180,30);
                age = new JTextField();
                age.setBounds(270,170,180,30);
                gen = new JComboBox(new String[] {"Male","Female"});
                gen.setBounds(270,210,180,30);
                bgrp = new JTextField();
                bgrp.setBounds(270,250,180,30);
                addr = new JTextField();
                addr.setBounds(270,290,180,30);
                anydis = new JTextField();
                anydis.setBounds(150,370,300,30);
                 add(pid);
                 add(name);
                 add(cno);
                 add(age);
                 add(gen);
                 add(bgrp);
                 add(addr);
                 add(anydis);
                /*Background image 
                img3 = new ImageIcon("image/m_back2.jpg");
                imglab3 = new JLabel(img3);
                imglab3.setSize(600,600);
                add(imglab3);*/
            }     
    public static void main(String[] args){
         Anpr apr=new Anpr();
        apr.setVisible(true); 
        
     }
  }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user14389228
  • 73
  • 1
  • 8

1 Answers1

2

The compiler is warning you that you have defined your JComboBox in a way that you can add any Object to the combo box.

Since generics were added to Java it is safer to specify the type of data you want to display in the combo box, then then compiler can check to make sure you only add that specific data type to the combo box.

So to get rid of the warning message you use:

JComboBox<String> gen;

when you define the combo box.

And when you create an instance of the combo box you can use:

gen = new JComboBox<String>(new String[] {"Male","Female"});

Or, even simpler is to just use "<>" and the String will default from the variable declaration:

gen = new JComboBox<>(new String[] {"Male","Female"});

Read up on "generics" for more information.

camickr
  • 321,443
  • 19
  • 166
  • 288