-1

I have searched everywhere, but I just couldn't find the answer for this error.

I type this for example:

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.BevelBorder;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.*;

public class ComboBox {
public static void main(String[] args) {
Object[] obj = { "obj1", "obj2", "obj3", "obj4" };
    String initialSelection = "obj1";
    Object selection = JOptionPane.showInputDialog(null, "Please select an option.",
 "ComboBox", JOptionPane.QUESTION_MESSAGE, null, obj, initialSelection);
 if(obj.getSelectedItem().toString().equals("obj1")) {
     JFrame testframe = new JFrame();
                JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
 }
}
}       

And I get the following error message:

"ComboBox.java:14: error: cannot find symbol if(obj.getSelectedItem().toString().equals("obj1")) {

symbol: method getSelectedItem() location: variable obj of type Object[]"

Did I forget to

import

something?

Vena
  • 35
  • 5

1 Answers1

0

getSelectedItem() is not present in array so you are getting the error.

I think you need

    if(obj[0].equals("obj1")

 {
     JFrame testframe = new JFrame();
     JOptionPane.showMessageDialog(testframe, "testing", "test screen", JOptionPane.INFORMATION_MESSAGE);
 }
SpringLearner
  • 13,738
  • 20
  • 78
  • 116