Search code examples
javaappletawt

Can not get item from List in java


So I m making an applet and it has to get the item and say at what age is the person in something like alert window . I get error for trying to get an selected item by the index in the list . I was reviewing some of the topics in stackoverflow , but I was finding only getting item from ArrayList and I m using List(I think there is difference) and find that getSelectedIndex(index) from a site for applets . But I have no clue why it gives me the error on that line : The method getSelectedIndex() in the type List is not applicable for the arguments (int). I just need to get that selected index and that way I ll get the item and when I know it is selected I ll run my logic . Sorry I m not that good in java . Here is the code:

package course;
import java.awt.*;
import java.net.*;
import java.applet.*;
import java.applet.Applet;
import java.awt.event.*;//vnimavai kak go pishesh
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.List;
import javax.swing.JOptionPane;



public class applet extends Applet implements ActionListener
{
    int fontStyle = Font.PLAIN;
    TextField name_enter;
    TextField pass_enter;
    TextField pass_re;
    List age;
    CheckboxGroup radioGroup;
    Checkbox radio1;
    Checkbox radio2;
    Button send;
    Button clear;

    public void init()
    {
        radioGroup = new CheckboxGroup();
        radio1 = new Checkbox("male",radioGroup,true);
        radio2 = new Checkbox("female",radioGroup,false);
        send = new Button("send");
        clear = new Button("clear");
        name_enter = new TextField("",20);
        pass_enter = new TextField("",14);
        pass_re = new TextField("",14);
        age = new List();
        name_enter.setBounds(100, 100, 130, 70);
        pass_enter.setBounds(100, 110, 130, 80);
        pass_re.setBounds(100, 120, 130, 70);
        age.setBounds(100, 130, 130, 70);
        radio1.setBounds(100, 140, 130, 70);
        radio2.setBounds(100, 150, 130, 70);
        send.setBounds(100, 160, 130, 70);
        clear.setBounds(105, 160, 130, 70);
        add(new Label("Enter name:"));
        add(name_enter);
        add(new Label("Enter password >=6 char:"));
        add(pass_enter);
        add(new Label("Reenter password:"));
        add(pass_re);
        add(new Label("Enter age:"));
        add(age);
        age.add("-30");
        age.add("31-50");
        age.add("51+");
        add(new Label("You are:"));
        add(radio1);
        add(radio2);
        add(send);
        add(clear);
        send.addActionListener(this);
        clear.addActionListener(this);
    }
    public void actionPerformed(ActionEvent evnt)
    {
        if(evnt.getSource()==send)
        {
            if(age.getSelectedIndex(0))
            {
                JOptionPane.showMessageDialog(null,"Send button pushed.");
            }
            else if()
            {

            }
            else if()
            {

            }
                repaint();
        }
        else if(evnt.getSource()==clear)
        {
            name_enter.setText(null);
            pass_enter.setText(null);
            pass_re.setText(null);
            repaint();
        }
    }
}

Solution

  • 1) The java.awt.List has no parameters in getSelectedIndex();

    Please refer to documentation for full information: Reference Documentation

    2) You are trying to pass the function with return type int to if( ) block which works only with boolean values.

    So, this part shoud look like this:

    if (age.getSelectedIndex() == 0) {
       JOptionPane.showMessageDialog(null, "Send button pushed.");
    }
    

    Or:

    if (age.isIndexSelected(0)) {
       JOptionPane.showMessageDialog(null, "Send button pushed.");
    }
    

    isIndexSelected(int index) - Determines if the specified item in this scrolling list is selected

    .