Search code examples
javacombobox

How to fix Java error "Unresolved compilation problem" in Eclipse IDE?


I'm trying to dabble with a basic ComboBox (i hear these are very outdated). The goal is to create a box with options for the user and when they click said option in the ComboBox they will get information from an array of strings based on the case detected by the switch. This is my code:

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

public class SolicitorComBox extends JFrame implements ActionListener {
    String[] MuniArray = {"Allepo", "Avalon"};
    JComboBox MuniList = new JComboBox (MuniArray);
JLabel lblText = new JLabel();

public static void main (String[] args) { 
    SolicitorComBox fr = new SolicitorComBox();
    centerFrame(fr);
    fr.setVisible(true);


}
private static void centerFrame(SolicitorComBox fr) {
    // TODO Auto-generated method stub

}
public SolicitorComBox() {
    setLayout (new FlowLayout());
    setSize (400, 300);
    setTitle ("Solicitor Search");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    MuniList.setSelectedIndex(0);
    MuniList.addActionListener(this);
    add(MuniList);
    add(lblText);

}
public void actionPerformed(ActionEvent e) {
    if (e.getsource() == MuniList) {
        JComboBox cb = (JComboBox).e.getsource();
        String msg = (String)cb.getSelectedItem();
        switch (msg) {
            case "Allepo": lblText.setText("The attorney is Joe!");
            break;
            case "Avalon": lblText.setText("The attorney is Dana!");
            break;

This is the error I'm getting when I try to run the code:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
at SolicitorComBox.main(SolicitorCombobox.java:10)

Solution

  • beside the fact you didn't post your whole code, I was able to run the piece of code you put over there:

    enter image description here

    So or your issue is below the code you posted, or it is just a compilation problem in your IDE

    I would recommend to you manually refresh your project and force clean it using the menu Project > Clean in Eclipse. I will force your app to recompile.