Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:35:25+00:00 2026-06-17T12:35:25+00:00

I work JComboBox es and DefaultComboboxModel that I fill with my database. Now I

  • 0

I work JComboBoxes and DefaultComboboxModel that I fill with my database. Now I would like to save a Enregistrement in my database. But I get this error message

java.lang.String cannot be cast to metier.Motif

This is my class Motif :

package metier;


public class Motif  { 

private Integer id;
private String libelle;
private Boolean recetteDepense;
Enregistrement enregistrements[];

public Motif() {
    this.id = null;
    this.libelle = "";
    this.recetteDepense = null;
}

public Motif(Integer id, String libelle, Boolean recetteDepense, Enregistrement[] enregistrements) {
    this.id = id;
    this.libelle = libelle;
    this.recetteDepense = recetteDepense;
    this.enregistrements = enregistrements;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getLibelle() {
    return libelle;
}

public void setLibelle(String libelle) {
    this.libelle = libelle;
}

public Boolean getRecetteDepense() {
    return recetteDepense;
}

public void setRecetteDepense(Boolean recetteDepense) {
    this.recetteDepense = recetteDepense;
}

public Enregistrement[] getEnregistrement() {
    return enregistrements;
}

public void setEnregistrement(Enregistrement[] enregistrements) {
    this.enregistrements = enregistrements;
}

@Override
public String toString() {
    return "Motif{" + "id=" + id + ", libelle=" + libelle + ", recetteDepense=" + recetteDepense + ", enregistrements=" + enregistrements + '}';
}



}

And this is my function to save the Enregistrement :

public void enregistrerEnregistrement() throws DaoException {
    // Déclarations de variables locales
    dao = new DaoH2("gestComptes", "sa", "");
     try {
        dao.connecter();

        Motif motif = (Motif)(((VueAjouterEnregistrement)vue).getModeleJComboBoxMotif().getSelectedItem());            
        System.out.print(motif.getId());

        //dao.ajouterUnEnregistrement(id, idLibelle, idModeReglement, idCompte, idEtat, idMotif, PreLEnc, RecDep, DateEnr, montant, ancienSolde, nouveauSolde, dateFacture, numCHQ, anticipation);
    } catch (DaoException ex) {
       JOptionPane.showMessageDialog(vue, "CtrlAjouterEnregistrement - instanciation - " + ex.getMessage(), "Enregistrement", JOptionPane.ERROR_MESSAGE);
    }

    }

My Comboboxes work but I don’t know how to recover the element with my getters and setters of my object Motif.

I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to metier.Motif
at controleurs.CtrlAjouterEnregistrement.enregistrerEnregistrement(CtrlAjouterEnregistrement.java:235)
at vues.VueAjouterEnregistrement.jButtonValiderActionPerformed(VueAjouterEnregistrement.java:1288)
at vues.VueAjouterEnregistrement.access$000(VueAjouterEnregistrement.java:31)
at vues.VueAjouterEnregistrement$1.actionPerformed(VueAjouterEnregistrement.java:187)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:35:26+00:00Added an answer on June 17, 2026 at 12:35 pm

    In this line

    Motif motif = (Motif)(((VueAjouterEnregistrement)vue)
                       .getModeleJComboBoxMotif().getSelectedItem());
    

    you asume that getSelectedItem() returns a Motif where in fact it returns a String.

    You shouldn’t add strings to the combo box, but Motifs, and have Motif implement toString to be properly represented in the GUI. Then the getSelectedItem method will get you the selected Motif.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JComboBox already created but I would like to update the information
got anyways to dynamically generate JComponent like JTextField, JCombobox? What i try is work..,
Hello I'd like to get this swing layout inside a JPanel: JLabel - JTextField
Work on asp.net vs05. I have three type of value Like:IsDesign,IsPrinting,IsInstall they are bit
Work on this small test application to learn threading/locking. I have the following code,
I work with unordered_set . Here it's written that it has a reserve function
I have a Utils class such that: public static void setComboBoxDefaultWidth(JComboBox cb) { cb.setPrototypeDisplayValue(mmmmmmmmmmmmmmmmmmmmmmmmmm);
I downloaded Typesafe's Scala IDE bundle, and now I try to make it work
I can't get QComboBox::findData to work properly in my PyQt application. It seems as
I am trying to work out a style for a ComboBox that has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.