Java JComboBox Incompatible Types: Cannot be converted to string

Justine P

I have this error when i try to add items in the JComboBox

incompatible types: ComboBox cannot be converted to String

This is my method to load the data from database to the JComboBox...

public final void loadProducts()
{
    try 
    {
        String sql = "SELECT * from product";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();

        while (rs.next())
        {
            combobox.addItem(new ComboBox(rs.getString(2), rs.getString(1)));
        }
        combobox.setSelectedIndex(-1);
    } 
    catch (SQLException ex) 
    {
        System.out.println(ex);
    }
}

And this is the class

public class ComboBox
{
    private String key;
    private String value;

public ComboBox(String key, String value)
{
    this.key = key;
    this.value = value;
}

@Override
public String toString()
{
    return key;
}

public String getKey()
{
    return key;
}

public String getValue()
{
    return value;
}
}

I have no idea what's causing it! Can someone point out my mistake?

user85421

It is hard to be certain without knowing how combobox is declared and at which line the Exception is being thrown...

My guess: combobox is declared as a JComboBox that takes a String and you the Exception is being thrown since a ComboBox is being added instead of a String.

Possible correction: declare the JComboBox to hold instances of ComboBox:

private JComboBox<ComboBox> combobox;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Java : incompatible types; int cannot be converted to string

From Dev

JAVA: Incompatible types: int cannot be converted to java.lang.String

From Dev

Java: toString error "error: incompatible types: int cannot be converted to String"

From Dev

incompatible types: String[] cannot be converted to String

From Java

Java incompatible types: int cannot be converted to int[]

From Dev

Java incompatible types: int cannot be converted to int[]

From Dev

incompatible types - int cannot be converted in to java.lang.string -java - bluej

From Dev

Why am I getting, "incompatible types: Object cannot be converted to String" with this?

From Dev

JAVA incompatible types: Object cannot be converted to my type

From Dev

Incompatible types : Object cant not be converted to String in java netbeans

From Dev

java8 stream apis throwing incompatible types: Object[] cannot be converted to Integer[] when converting String array to Integer array

From Dev

incompatible types: Node cannot be converted to Tab

From Java

incompatible types: int cannot be converted to T

From Dev

incompatible types: HomeFragment cannot be converted to Fragment in Android

From Dev

Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

From Dev

Error: incompatible types: double cannot be converted to JTextField

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

incompatible types: void cannot be converted to int

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

Incompatible types: Object cannot be converted to ParseObject

From Dev

incompatible types: char[] cannot be converted to CharSequence

From Dev

error: incompatible types: MainFragment cannot be converted to Activity

From Dev

incompatible types: FragmentDark cannot be converted to Fragment in Android

From Dev

Error: incompatible types: Object cannot be converted to char

From Dev

incompatible types: BigInteger cannot be converted to int

From Dev

incompatible types: Object cannot be converted to CoreLabel

From Dev

error: incompatible types: Object cannot be converted to MyClass

From Dev

incompatible types: MainActivity cannot be converted to LifecycleOwner

From Dev

error: incompatible types: int[][] cannot be converted to int

Related Related

  1. 1

    Java : incompatible types; int cannot be converted to string

  2. 2

    JAVA: Incompatible types: int cannot be converted to java.lang.String

  3. 3

    Java: toString error "error: incompatible types: int cannot be converted to String"

  4. 4

    incompatible types: String[] cannot be converted to String

  5. 5

    Java incompatible types: int cannot be converted to int[]

  6. 6

    Java incompatible types: int cannot be converted to int[]

  7. 7

    incompatible types - int cannot be converted in to java.lang.string -java - bluej

  8. 8

    Why am I getting, "incompatible types: Object cannot be converted to String" with this?

  9. 9

    JAVA incompatible types: Object cannot be converted to my type

  10. 10

    Incompatible types : Object cant not be converted to String in java netbeans

  11. 11

    java8 stream apis throwing incompatible types: Object[] cannot be converted to Integer[] when converting String array to Integer array

  12. 12

    incompatible types: Node cannot be converted to Tab

  13. 13

    incompatible types: int cannot be converted to T

  14. 14

    incompatible types: HomeFragment cannot be converted to Fragment in Android

  15. 15

    Incompatible types: Fragment cannot be converted to NavigationDrawerFragment

  16. 16

    Error: incompatible types: double cannot be converted to JTextField

  17. 17

    Error: incompatible types: Object cannot be converted to char

  18. 18

    incompatible types: void cannot be converted to int

  19. 19

    incompatible types: BigInteger cannot be converted to int

  20. 20

    Incompatible types: Object cannot be converted to ParseObject

  21. 21

    incompatible types: char[] cannot be converted to CharSequence

  22. 22

    error: incompatible types: MainFragment cannot be converted to Activity

  23. 23

    incompatible types: FragmentDark cannot be converted to Fragment in Android

  24. 24

    Error: incompatible types: Object cannot be converted to char

  25. 25

    incompatible types: BigInteger cannot be converted to int

  26. 26

    incompatible types: Object cannot be converted to CoreLabel

  27. 27

    error: incompatible types: Object cannot be converted to MyClass

  28. 28

    incompatible types: MainActivity cannot be converted to LifecycleOwner

  29. 29

    error: incompatible types: int[][] cannot be converted to int

HotTag

Archive