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

avec Java Discussion :

Cast String to Image


Sujet :

avec Java

  1. #1
    Membre confirmé
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mai 2014
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : BTS IRIS
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mai 2014
    Messages : 68
    Par défaut Cast String to Image
    Bonjour, j'ai un petit soucis. Alors j'ai une JCombobox qui comporte des Icons, cependant je voudrais que ces icônes ce place sur le titre de la prochaines fenetre. J'ai dont Cast mais icon en string mais je ne vois pas comment casté un string en une image.

    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
            boutonOk.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    String title = comboPlot.getSelectedItem().toString();
                    String icon = comboIcon.getSelectedItem().toString();
     
                    comboIcon.addItem(icon);
                    comboPlot.addItem(title);
                    ChartViewFrame chartViewf = new ChartViewFrame();
                    chartViewf.setTitle(title);
                    System.out.println(icon);
     
     
                }
            });
    J'ai essayé plusieurs manière de récuperer l'icone comme
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
                    // chartViewf.setIconImage(new ImageIcon(icon).getImage());
                    // chartViewf.setIconImage(new ImageIcon(getClass().getResource(icon)));
    // chartViewf.setIconImage(Toolkit.getDefaultToolkit().getImage(icon));
    J'imagine que je dois pas être loin de la solution. Mon seul problème c'est que icon est de type string :/
    Merci d'avance !

  2. #2
    Membre éclairé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2007
    Messages
    77
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Royaume-Uni

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Août 2007
    Messages : 77
    Par défaut
    Pourquoi est ce que vous passez pas un String?

    Est ce que la methode
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().getClass()
    renvoi String comme résultat?

    Si oui est ce que créer une ImageIcon marche?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new ImageIcon("/icon.png")
    Mais regardez le type de l'Item, car il est possible que le cast ne soit pas nécessaire.

  3. #3
    Membre confirmé
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mai 2014
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : BTS IRIS
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mai 2014
    Messages : 68
    Par défaut
    La méthode
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().getClass()
    Renvois un String, car si je fais un system.out.println(icon);
    On voit ça : jar:file:/C:/Users/rodrigues/.m2/repository/com/famfamfam/silk/1.3/silk-1.3.jar!/com/famfamfam/silk/chart_pie.png


    Créer une ImageIcon ? En fait j'ai une Combobox dans laquel j'intègre plusieurs icone grâce à un CellListRenderer. Cependant lorsque j'ai choisie mon icon j'appuis sur un bouton "ok". Quand j'appuis une fenêtre s'ouvre, mais je voudrais que le titre soit l'icone que j'ai sélectionnez. Donc je pense qu'il faut juste que je récupère la chaîne de caractère, de réussir à le cast en image afin de pouvoir de l'utiliser...
    Merci de me consacré du temps !

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Montre nous déjà comment tu as créé ton Combobox et tu l'a affiché car,

    soit getSelectedItem te retourne une Icon -> Alors utilise la
    soit il te retourne une String -> Alors ton ListRenderer répond à la question "comment le transformer en image" puisqu'il le fait
    soit c'est autre chose -> Même réponse qu'au dessus.

  5. #5
    Membre Expert Avatar de Uther
    Homme Profil pro
    Tourneur Fraiseur
    Inscrit en
    Avril 2002
    Messages
    4 690
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pyrénées Orientales (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Tourneur Fraiseur

    Informations forums :
    Inscription : Avril 2002
    Messages : 4 690
    Par défaut
    Citation Envoyé par kewti Voir le message
    La méthode
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().getClass()
    Renvois un String, car si je fais un system.out.println(icon);
    On voit ça : jar:file:/C:/Users/rodrigues/.m2/repository/com/famfamfam/silk/1.3/silk-1.3.jar!/com/famfamfam/silk/chart_pie.png
    Pas forcément.

    System.out.println(icon) fonctionne sur tous les objets qu'ils soient des chaines de caractère ou pas. Dans ce cas là il affiche le résultat de la méthode toString() de l'objet, pour être sur de ça il faut vraiment utiliser getClass() comme le suggère kewti ou mieux un débogueur

  6. #6
    Modérateur
    Avatar de Alkhan
    Homme Profil pro
    ingénieur full stack
    Inscrit en
    Octobre 2006
    Messages
    1 232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : ingénieur full stack

    Informations forums :
    Inscription : Octobre 2006
    Messages : 1 232
    Par défaut
    bonjour,
    Citation Envoyé par kewti Voir le message
    La méthode
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().getClass()
    Renvois un String, car si je fais un system.out.println(icon);
    On voit ça : jar:file:/C:/Users/rodrigues/.m2/repository/com/famfamfam/silk/1.3/silk-1.3.jar!/com/famfamfam/silk/chart_pie.png
    je doute que le getClasse t'ai retourné ca !!

    en revanche ce que tu avais fait avant oui :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().toString()
    mais le fait que tu es une chaine ne veux pas dire que "comboIcon.getSelectedItem()" est une chaine, mais simplement un objet qui, lorsque l'on appel "toString" dessus affiche :
    jar:file:/C:/Users/rodrigues/.m2/repository/com/famfamfam/silk/1.3/silk-1.3.jar!/com/famfamfam/silk/chart_pie.png

    si tu avais effectivement fait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    comboIcon.getSelectedItem().getClass().toString()
    tu aurais obtenus le nom de la classe de l'objet retourné par "comboIcon.getSelectedItem()"

    Edit : tu as été plus rapide que moi Uther
    Il n'y a pas de problème, il n'y a que des solutions.
    Cependant, comme le disaient les shadoks, s'il n'y a pas de solution, c'est qu'il n'y a pas de problème.
    Si toutefois le problème persiste, la seule solution restante est de changer le périphérique qui se trouve entre la chaise et l'écran

    Mes Articles : Mon premier article est sur le language D
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  7. #7
    Membre confirmé
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mai 2014
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : BTS IRIS
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mai 2014
    Messages : 68
    Par défaut
    Merci pour toutes cette aide. Voici le code de toutes ma fenetre, elle comporte 2 Combobox, une pour définir le titre de la prochaines fenêtre (il n'y pas de soucis dessus) l'autre pour sélectionnez l'icones !

    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    public class ChartOption extends JFrame {
        private static final long serialVersionUID = 1L;
     
        private final Map<String, JFrame> chartFrameMap = new HashMap<String, JFrame>();
     
        public JPanel panelMain = new JPanel(new BorderLayout());
        public JPanel panelCombo = new JPanel(new MigLayout("fill, wrap 2, insets 2"));
        public JPanel panelBtn = new JPanel();
        public JLabel labelTitle = new JLabel("Title : ");
        public JLabel labelIcon = new JLabel("Icon : ");
        public JButton boutonOk = new JButton("OK");
        public JButton boutonCancel = new JButton("Cancel");
     
        JComboBox comboPlot;
        JComboBox comboIcon;
        private final ImageIcon CHART_LINE = new ImageIcon(getClass().getResource("/com/famfamfam/silk/chart_line.png"));
        private final ImageIcon CHART_CURVE = new ImageIcon(getClass().getResource("/com/famfamfam/silk/chart_curve.png"));
        private final ImageIcon CHART_PIE = new ImageIcon(getClass().getResource("/com/famfamfam/silk/chart_pie.png"));
        private final ImageIcon CHART_BAR = new ImageIcon(getClass().getResource("/com/famfamfam/silk/chart_bar.png"));
        private final ImageIcon ASTERISK_YELLOW = new ImageIcon(getClass().getResource(
                "/com/famfamfam/silk/asterisk_yellow.png"));
        private final ImageIcon WORLD_ICON = new ImageIcon(getClass().getResource("/com/famfamfam/silk/world.png"));
        private final ImageIcon FLAG_BLUE = new ImageIcon(getClass().getResource("/com/famfamfam/silk/flag_blue.png"));
        private final ImageIcon FLAG_GREEN = new ImageIcon(getClass().getResource("/com/famfamfam/silk/flag_green.png"));
        private final ImageIcon RAINBOW_ICON = new ImageIcon(getClass().getResource("/com/famfamfam/silk/rainbow.png"));
        private final ImageIcon SHIELD = new ImageIcon(getClass().getResource("/com/famfamfam/silk/shield.png"));
        public final ImageIcon[] images = { CHART_LINE, CHART_PIE, CHART_CURVE, CHART_BAR, ASTERISK_YELLOW, WORLD_ICON,
                FLAG_BLUE, FLAG_GREEN, SHIELD, RAINBOW_ICON };
     
        public ChartOption() {
     
            this.setTitle("Chart Option");
            this.setSize(250, 250);
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setLocationRelativeTo(null);
            this.addWindowListener(new WindowListener() {
     
                @Override
                public void windowOpened(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowIconified(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowDeiconified(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowDeactivated(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowClosing(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowClosed(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
     
                @Override
                public void windowActivated(WindowEvent e) {
                    // TODO Auto-generated method stub
     
                }
            });
            this.setVisible(true);
     
            comboPlot = new JComboBox();
            comboPlot.setEditable(true);
            comboIcon = new JComboBox();
            for (ImageIcon icon : images) {
                comboIcon.addItem(icon);
            }
     
            ListCellRenderer renderer = comboIcon.getRenderer();
            ComboIconRenderer iconRenderer = new ComboIconRenderer(renderer);
            comboIcon.setRenderer(iconRenderer);
     
            panelMain.add(panelCombo, BorderLayout.CENTER);
            panelCombo.add(labelTitle);
            panelCombo.add(comboPlot);
     
            panelCombo.add(labelIcon);
            panelCombo.add(comboIcon);
     
            panelMain.add(panelBtn, BorderLayout.SOUTH);
            panelBtn.add(boutonOk);
            panelBtn.add(boutonCancel);
     
            boutonOk.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    String title = comboPlot.getSelectedItem().toString();
                    String icon = comboIcon.getSelectedItem().toString();
     
                    // chartFrameMap.put("Title", frame);
                    // chartFrameMap.keySet();
     
     
                    comboPlot.addItem(title);
                    ChartViewFrame chartViewf = new ChartViewFrame();
                    chartViewf.setTitle(title);
     
     
                }
            });
            boutonCancel.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
     
                    setVisible(false);
                }
            });
     
            this.setContentPane(panelMain);
     
        }
     
        private class ComboIconRenderer extends DefaultListCellRenderer {
     
            ListCellRenderer renderer;
     
            public ComboIconRenderer(ListCellRenderer renderer) {
                this.renderer = renderer;
            }
     
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (comp instanceof JLabel && value instanceof ImageIcon) {
                    ((JLabel) comp).setIcon((ImageIcon) value);
                }
                return comp;
            }
        }
     
        public static void main(String[] args) {
            ChartOption chart = new ChartOption();
            chart.setVisible(true);
        }
     
    }
    Si je fais String icon = comboIcon.getSelectedItem().getClass();

    Le soucis est le même, je ne peux pas récupérer une images si ?

  8. #8
    Membre confirmé
    Homme Profil pro
    BTS IRIS
    Inscrit en
    Mai 2014
    Messages
    68
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 31
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : BTS IRIS
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Mai 2014
    Messages : 68
    Par défaut
    C'est bon j'ai trouver, j'ai fait :

    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
        public void actionPerformed(ActionEvent e) {
                    String title = comboPlot.getSelectedItem().toString();
                    ImageIcon icon = (ImageIcon) comboIcon.getSelectedItem();
     
                    // chartFrameMap.put("Title", frame);
                    // chartFrameMap.keySet();
     
                    // comboIcon.addItem(icon);
                    comboPlot.addItem(title);
                    ChartViewFrame chartViewf = new ChartViewFrame();
                    chartViewf.setTitle(title);
                    chartViewf.setIconImage(icon.getImage());
                    chartViewf.setVisible(true);
                    chartViewf.toFront();
                    // chartViewf.setIconImage(new ImageIcon(icon).getImage());
                    // chartViewf.setIconImage(new ImageIcon(getClass().getResource(icon)));
     
                }
    Merci pour votre aide, et désolé pour ma stupidité

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Cast String vers Number
    Par romain_ci dans le forum ActionScript 3
    Réponses: 2
    Dernier message: 14/08/2008, 13h40
  2. Réponses: 2
    Dernier message: 23/10/2007, 17h33
  3. Cast string vers vertor<string>
    Par franc82 dans le forum C++
    Réponses: 4
    Dernier message: 10/11/2006, 00h03
  4. [débutant] cast string->int
    Par gigigao dans le forum Visual C++
    Réponses: 8
    Dernier message: 23/08/2006, 11h47
  5. Convertir une string en image
    Par worldchampion57 dans le forum AWT/Swing
    Réponses: 2
    Dernier message: 16/05/2005, 09h55

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