1

I have written a code that tells you whether you are eligible to vote or not.. but when I compile it it shows an error or warning the message says:

Vote.java uses unsafe or unchecked operations. Recompile with -Xlint:unchecked for details.

this is my code..

I did more research but it didn't help me... now I can't compile it

`

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

public class Vote extends JFrame {
    JLabel ageEnquiry, result;
    JComboBox<String> ageList;
    JTextField results;
    JButton val;

    public Vote() {

        String[] ages = new String[] {"10-17", "18-30", "31-40", "41-50", "51-60", "61-70", "71-80", "81-90", "91-100"};

        setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        ageEnquiry = new JLabel("Select your age range: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        add(ageEnquiry, c);

        ageList = new JComboBox<>(ages);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 5;
        c.gridy = 0;
        add(ageList, c);

        result = new JLabel("Result: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 2;
        add(result, c);

        results = new JTextField(10);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 5;
        c.gridy = 2;
        add(results, c);

        ageList.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JComboBox<String> combo = (JComboBox<String>) event.getSource();
                String selectedAge = (String) combo.getSelectedItem();

                if(selectedAge.equals("10-17")) {
                    results.setText("Not Eligible");
                } else {
                    results.setText("Eligible");
                }
            }
        });
    }


    public static void main(String[] args) {
        Vote gui = new Vote();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.setSize(400, 400);
        gui.setTitle("Vote");
    }
}

`

please help me.. :(

Indrajeet
  • 5,490
  • 2
  • 28
  • 43
Aadi Shah
  • 99
  • 1
  • 8

1 Answers1

5

Just compile your code via:

javac -Xlint:unchecked Vote.java

You will get more detailed information about the warning you received. The warning occurs because of this line.

JComboBox<String> combo = (JComboBox<String>) event.getSource();

To get rid of the warning see this approach: Handle generics in ActionListener

You would then have to edit your code as following:

  • Create a custom ActionListener
  • Use this ActionListener for your JComboBox

ActionListener

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

class CustomActionListener implements ActionListener
{
  private JComboBox<String> comboBox;
  private JTextField textField;
  public CustomActionListener(JComboBox<String> comboBox, JTextField textField){
    this.comboBox = comboBox;
    this.textField = textField;
  }
  @Override
  public void actionPerformed(ActionEvent event)
  {
    // Just use the comboBox
    ComboBoxModel<String> model = comboBox.getModel();
    int index = comboBox.getSelectedIndex();
    String selectedAge = model.getElementAt(index);
                if(selectedAge.equals("10-17")) {
                    textField.setText("Not Eligible");
                } else {
                    textField.setText("Eligible");
                }
  }
}

Changed Vote:

ageList.addActionListener(new CustomActionListener(ageList,results));            
Community
  • 1
  • 1
dehlen
  • 7,325
  • 4
  • 43
  • 71
  • I am sorry but i can't understand the link 'Handle generics in ActionListener..' i mean what improvement should i make to my code.. could you just show me the re-written code which works properly? thank you – Aadi Shah Jan 02 '15 at 12:35
  • see my edited answer and if it helped you I would appreciate to mark the answer as accepted :) – dehlen Jan 02 '15 at 12:44
  • and one last question related to this topic.. where should i put this code.. i am really a beginner.. sorry to ask such dumb questions.. thank again – Aadi Shah Jan 02 '15 at 14:47
  • If you were able to figure out how to create a JFrame and how to use the GridBagLayout you should know where to put it don't you ? Did you write this code by yourself ? Like i wrote create a new class for the ActionListener and replace your current ActionListener-implementation for the CheckBox with the one above. – dehlen Jan 02 '15 at 14:49