IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants Java Discussion :

Serialiser un Jcombobox d'image


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    676
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 676
    Par défaut Serialiser un Jcombobox d'image
    Bonjour,

    Dans le but d'enregistrer mes environnements de travail, je dois serialiser un JComboBox, jusque là rien d'alarmant mais mon JComboBox liste des images. Pour le réalisé j'ai fait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Image[] figures={getToolkit().getImage(("img/carre.gif")),getToolkit().getImage(("img/triangleBas.gif")),getToolkit().getImage(("img/triangleHaut.gif")),getToolkit().getImage(("img/rond.gif")),getToolkit().getImage(("img/rien.gif"))};
    			forme=new JComboBox(figures);
    Le soucis, c'est que je viens d'apprendre que Image est une classe non serialisable. Du coup j'ai essayé de faire un JComboBox avec des ImageIcon (eux serialisable) sauf qu'on ne peut pas. Alors comment puis-je faire ?

  2. #2
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    47
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2010
    Messages : 47
    Par défaut
    Je ne sais pas si il existe de solutions plus faciles, mais sinon tu peux créer une classe image dérivée qui implémente Serializable, mais ça risque de faire beaucoup de boulot...
    Sinon tu ne peux pas simplement sérialiser l'emplacement de l'image sur le disque? Et lorsque tu recharges ton application tu va récupérer l'image à l'emplacement sérialisé?

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    676
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 676
    Par défaut
    Si j'avais des idée du style "je serialise le chemin et je m'arrange comme ça" mais le soucis, c'est que je peux serialiser tous les chemins que je veux, il faudra qu'à un moment j'injecte tout ça dans le JComboBox et là il prendra les images. Et quand je serialiserais mon environnement, il traitera le problème comme avant

  4. #4
    Rédacteur/Modérateur
    Avatar de Logan Mauzaize
    Homme Profil pro
    Architecte technique
    Inscrit en
    Août 2005
    Messages
    2 894
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Transports

    Informations forums :
    Inscription : Août 2005
    Messages : 2 894
    Par défaut
    La sérialisation de composants Swing n'est que partiellement géré. N'est-il pas possible d'enregistrer les états fonctionnels pour ensuite reconstruire l'IHM dans le bon état ?

    Sinon comme dit précédemment créer proxy serializable. C'est-à-dire une sous-classe qui corresponde à l'interface/classe nécessaire à la JCombobox, avec des attributs pour "reconstruire" l'image, un attribut transient dont la classe correspond à l'interface/classe nécessaire à la JCombobox qui serait initialisé en lazy loading
    Exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
     
    public class SerializableImage implements Image, Serializable
    {
      private String imageName;
      private transient Image proxied = null;
     
      public SerializableImage(String imageName)
      {
       this.imageName = imageName;
      }
     
      protected Image getProxied()
      {
        if (proxied == null)
        {
          proxied = Toolkit.getInstance().getImage(imageName);
        }
        return proxied;
      }
     
      @Override
      public void draw(Graphics g)
      {
         getProxied().draw(g);
      }
    }
    Java : Cours et tutoriels - FAQ - Java SE 8 API - Programmation concurrente
    Ceylon : Installation - Concepts de base - Typage - Appels et arguments

    ECM = Exemple(reproduit le problème) Complet (code compilable) Minimal (ne postez pas votre application !)
    Une solution vous convient ? N'oubliez pas le tag
    Signature par pitipoisson

  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    676
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 676
    Par défaut
    Alors pour le moment j'ai créée cette classe (j'ai du ajouter les méthode non implémentées) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    class SerializableImage extends Image implements Serializable
    {
      private String imageName;
      private transient Image proxied = null;
      private Component compo;
     
      public SerializableImage(String imageName,Component compo)
      {
       this.imageName = imageName;
       this.compo = compo;
      }
     
      protected Image getProxied()
      {
        if (proxied == null)
        {
          proxied = compo.getToolkit().getImage(imageName);
        }
        return proxied;
      }
     
      public void draw(Graphics g)
      {
         ((SerializableImage) getProxied()).draw(g);
      }
     
    @Override
    public Graphics getGraphics() {
    	// TODO Auto-generated method stub
    	return null;
    }
     
    @Override
    public int getHeight(ImageObserver arg0) {
    	// TODO Auto-generated method stub
    	return 0;
    }
     
    @Override
    public Object getProperty(String arg0, ImageObserver arg1) {
    	// TODO Auto-generated method stub
    	return null;
    }
     
    @Override
    public ImageProducer getSource() {
    	// TODO Auto-generated method stub
    	return null;
    }
     
    @Override
    public int getWidth(ImageObserver arg0) {
    	// TODO Auto-generated method stub
    	return 0;
    }
    }
    et j'execute le code suivant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    			SerializableImage[] figures={new SerializableImage("img/carre.gif",this),new SerializableImage("img/triangleBas.gif",this),new SerializableImage("img/triangleHaut.gif",this),new SerializableImage("img/rond.gif",this),new SerializableImage("img/rien.gif",this)};
    			forme=new JComboBox(figures);
    Lui n'a pas l'air de beaucoup apprécier ma petite création :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid Image variant
    	at sun.awt.image.SurfaceManager.getManager(Unknown Source)
    	at sun.java2d.SurfaceData.getSourceSurfaceData(Unknown Source)
    	at sun.java2d.pipe.DrawImage.renderImageCopy(Unknown Source)
    	at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    	at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    	at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    	at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    	at javax.swing.ImageIcon.paintIcon(Unknown Source)
    	at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.CellRendererPane.paintComponent(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboBoxUI.paintCurrentValue(Unknown Source)
    	at javax.swing.plaf.metal.MetalComboBoxUI.paintCurrentValue(Unknown Source)
    	at javax.swing.plaf.basic.BasicComboBoxUI.paint(Unknown Source)
    	at javax.swing.plaf.metal.MetalComboBoxUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JViewport.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JSplitPane.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JSplitPane.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JSplitPane.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JLayeredPane.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
    	at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  6. #6
    Rédacteur/Modérateur
    Avatar de Logan Mauzaize
    Homme Profil pro
    Architecte technique
    Inscrit en
    Août 2005
    Messages
    2 894
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Transports

    Informations forums :
    Inscription : Août 2005
    Messages : 2 894
    Par défaut
    Faudrait "proxir" toutes les méthodes ...
    Java : Cours et tutoriels - FAQ - Java SE 8 API - Programmation concurrente
    Ceylon : Installation - Concepts de base - Typage - Appels et arguments

    ECM = Exemple(reproduit le problème) Complet (code compilable) Minimal (ne postez pas votre application !)
    Une solution vous convient ? N'oubliez pas le tag
    Signature par pitipoisson

  7. #7
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Mai 2010
    Messages
    47
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mai 2010
    Messages : 47
    Par défaut
    Quand tu dis "l'erreur", tu veux dire une erreur de syntaxe sous-lignée en rouge par l'EDI?

  8. #8
    Rédacteur/Modérateur
    Avatar de Logan Mauzaize
    Homme Profil pro
    Architecte technique
    Inscrit en
    Août 2005
    Messages
    2 894
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Architecte technique
    Secteur : Transports

    Informations forums :
    Inscription : Août 2005
    Messages : 2 894
    Par défaut
    Il faudrait que le proxy étende BufferedImage, ca se complique drôlement ...

    Par ailleurs il faudrait redéfinir toutes les méthodes, pas uniquement celles abstraites ...

    Il doit exister de meilleurs façons de faire un proxy
    Java : Cours et tutoriels - FAQ - Java SE 8 API - Programmation concurrente
    Ceylon : Installation - Concepts de base - Typage - Appels et arguments

    ECM = Exemple(reproduit le problème) Complet (code compilable) Minimal (ne postez pas votre application !)
    Une solution vous convient ? N'oubliez pas le tag
    Signature par pitipoisson

  9. #9
    Membre éclairé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    676
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 676
    Par défaut
    Oui l'erreur c'est ce qu'il y a souligné en rouge par l'EDI. C'est la fameuse "java.lang.IllegalArgumentException: Invalid Image variant".
    Ce problème se complique drôlement en effet.

Discussions similaires

  1. Serialisation de la classe Image
    Par Ceubex dans le forum Interfaces Graphiques en Java
    Réponses: 6
    Dernier message: 21/04/2011, 20h28
  2. Serialisation JSON d'une image
    Par smarties dans le forum Silverlight
    Réponses: 4
    Dernier message: 19/04/2011, 10h47
  3. [C#] Serialisation images DB access
    Par Tips dans le forum Windows Forms
    Réponses: 5
    Dernier message: 29/12/2006, 22h08
  4. [Serialisation]Image et sérialisation
    Par Spoutnik dans le forum Général Java
    Réponses: 3
    Dernier message: 03/08/2005, 15h34
  5. [JComboBox] inserer une icone ou image
    Par just1980 dans le forum Composants
    Réponses: 1
    Dernier message: 11/04/2005, 21h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo