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

 Java Discussion :

Créer une JComboBox


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 39
    Par défaut Créer une JComboBox
    Bonjour,

    J'ai créer une jComboBox avec Eclipse, et je veux lui rentrer des valeurs dedans (1,2,5,10,100)...

    Voilà mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    private JComboBox getJComboBoxNbTour() {
    		if (jComboBoxNbTour == null) {
    			jComboBoxNbTour = new JComboBox();
    			jComboBoxNbTour.setPreferredSize(new Dimension(80, 25));
    			jComboBoxNbTour.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					String[] tourString = { "1", "2", "5", "10", "100" };
    					JComboBox tourList = new JComboBox(tourString);
    					tourList.setSelectedIndex(4);
    					tourList.addActionListener(this);}
    Cependant quand j'exécute cela, mon interface ne veut pas se lancer, il y a forcément une erreur, et en fait je ne suis pas sur de m'y être pris de la bonne façon...

    C'est pourquoi je vous demande votre aide si vous savez y répondre... Merci d'avance !

  2. #2
    Membre confirmé
    Profil pro
    Ingénieur
    Inscrit en
    Mars 2009
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Mars 2009
    Messages : 152
    Par défaut
    Pourquoi tu ne fais pas :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    private JComboBox maJComboBox;
     
    public void creationMaJComboBox(){
    	String[] tourString = { "1", "2", "5", "10", "100" };
    	maJComboBox=new JComboBox(tourString);
    }

  3. #3
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 39
    Par défaut
    Disons qu'avec ta méthode la jComboBox ne sera pas placer dans un jPanel...

    Moi j'ai insérer déjà ma jComboBox dans mon panel, j'ai pu la placer, lui donner des dimensions...
    Et du coup je ne comprend pas comment mettre un constructeur comme tu l'as fait pour cette même combobox que je viens de faire...

    En fait est-ce que je peux programmer pour mettre les valeurs (1,2,5,...) avec la méthode getJComboBox ?

  4. #4
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 39
    Par défaut
    C'est bon j'ai réussi !

    Je met ici mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    private JComboBox getJComboBoxNbTour() {
    		if (jComboBoxNbTour == null) {
    			String[] tourString = { "","1", "2", "5", "10", "100" };
    			jComboBoxNbTour = new JComboBox(tourString);
    			jComboBoxNbTour.setPreferredSize(new Dimension(80, 25));
    }}

  5. #5
    Membre confirmé
    Profil pro
    Ingénieur
    Inscrit en
    Mars 2009
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Mars 2009
    Messages : 152
    Par défaut
    Cela ne change rien tu peux toujours ajouter la JComboBox dans un JPanel... Je vois pas le rapport.

    Il te suffit de faire:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    tonPanel.add(maJComboBox);
    Tu peux aussi définir ses dimensions etc....

    la méthode que tu utilises m'a l'air bizarre (mais je suis pas expert ), il faudrait peut être que tu demandes à quelqu'un qui s'y connait mieux mais je comprends pas trés bien ce que tu fais.

    Par curiosité, tu peux me donner le code complet de la classe s'il te plait (Si il est pas trop long),

  6. #6
    Membre averti
    Inscrit en
    Mars 2010
    Messages
    39
    Détails du profil
    Informations forums :
    Inscription : Mars 2010
    Messages : 39
    Par défaut
    Voilà :

    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
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
     
    package com.eclipse;
     
    import javax.swing.SwingUtilities;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.GridBagLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.FlowLayout;
    import java.awt.CardLayout;
    import java.awt.Insets;
    import java.awt.SystemColor;
    import javax.swing.JTabbedPane;
    import java.awt.ComponentOrientation;
    import java.awt.Rectangle;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import java.awt.Font;
    import javax.swing.ImageIcon;
    import javax.swing.JToggleButton;
     
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.SwingConstants;
    import java.awt.Button;
    import java.awt.Point;
    import javax.swing.JComboBox;
    import java.awt.Graphics;
    import javax.swing.*;
    //import javax.swing.ButtonGroup
     
     
    import java.awt.Graphics;
    import java.awt.Graphics2D;
     
     
     
     
    public class MaClasse extends JFrame {
     
    	private static final long serialVersionUID = 1L;
    	private JPanel jContentPane = null;
    	private JPanel jPanelSimulation = null;
    	private JPanel jPanelConfiguration = null;
    	private JPanel jPanelCarte = null;
    	private JButton jButtonRemiseAZero = null;
    	private JTextArea jTextAreaAB = null;
    	private JToggleButton jToggleButtonAB1 = null;
    	private JButton jButtonGenerer = null;
    	private JComboBox jComboBoxNbTour = null;
     
     
     
    	private JPanel getJPanelSimulation() {
    		if (jPanelSimulation == null) {
    			GridBagConstraints gridBagConstraints = new GridBagConstraints();
    			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
    			gridBagConstraints.gridy = 1;
    			gridBagConstraints.weightx = 1.0;
    			gridBagConstraints.gridx = 0;
    			jPanelSimulation = new JPanel();
    			jPanelSimulation.setLayout(new GridBagLayout());
    			jPanelSimulation.setBackground(new Color(84, 57, 0));
    			jPanelSimulation.setBounds(new Rectangle(0, 600, 984, 115));
    			jPanelSimulation.add(getJComboBoxNbTour(), gridBagConstraints);
    		}
    		return jPanelSimulation;
    	}
     
     
    	private JPanel getJPanelConfiguration() {
    		if (jPanelConfiguration == null) {
    			jPanelConfiguration = new JPanel();
    			jPanelConfiguration.setBounds(new Rectangle(0, 0, 384, 600));
    			jPanelConfiguration.setLayout(null);
    			jPanelConfiguration.setBackground(SystemColor.activeCaption);
    			jPanelConfiguration.add(getJButtonRemiseAZero(), null);
    			jPanelConfiguration.add(getJToggleButtonAB1(), null);
    			jPanelConfiguration.add(getJButtonGenerer(), null);
     
    		}
    		return jPanelConfiguration;
    	}
     
     
     
    	private JPanel getJPanelCarte() {
    		if (jPanelCarte == null) {
    			jPanelCarte = new JPanel();
    			jPanelCarte.setLayout(new GridBagLayout());
    			jPanelCarte.setBounds(new Rectangle(384, 0, 600, 600));
    			jPanelCarte.setBackground(Color.green);
     
    		}
    		return jPanelCarte;
    	}
     
     
    	private JButton getJButtonRemiseAZero() {
    		if (jButtonRemiseAZero == null) {
    			jButtonRemiseAZero = new JButton();
    			jButtonRemiseAZero.setText("Remise à Zéro");
    			jButtonRemiseAZero.setSize(new Dimension(150, 40));
    			jButtonRemiseAZero.setLocation(new Point(120, 24));
    			jButtonRemiseAZero.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					jToggleButtonAB1.setSelected(false);
     
    				}
    			});
    ;
    		}
    		return jButtonRemiseAZero;
    	}
     
     
     
    	private JToggleButton getJToggleButtonAB1() {
    		if (jToggleButtonAB1 == null) {
    			jToggleButtonAB1 = new JToggleButton();
    			jToggleButtonAB1.setLocation(new Point(30, 395));
    			jToggleButtonAB1.setIcon(new ImageIcon("C:/Users/Sylvain/Documents/Eclipse workspace/Combat/Armée bleue 1.jpg"));
    			jToggleButtonAB1.setSize(new Dimension(100, 80));
     
    			});
    		}
    		return jToggleButtonAB1;
    	}
     
     
     
    	private JButton getJButtonGenerer() {
    		if (jButtonGenerer == null) {
    			jButtonGenerer = new JButton();
    			jButtonGenerer.setLocation(new Point(120, 520));
    			jButtonGenerer.setText("Générer");
    			jButtonGenerer.setSize(new Dimension(150, 40));
    		}
    		return jButtonGenerer;
    	}
     
     
     
     
    	private JComboBox getJComboBoxNbTour() {
    		if (jComboBoxNbTour == null) {
    			String[] tourString = { "","1", "2", "5", "10", "100" };
    			jComboBoxNbTour = new JComboBox(tourString);
    			jComboBoxNbTour.setPreferredSize(new Dimension(80, 25));
    			jComboBoxNbTour.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
     
    				JComboBox cb = (JComboBox)e.getSource();
    			     String jComboBoxNbTour = (String)cb.getSelectedItem().toString();
    						}}
    			);
    		}
    		return jComboBoxNbTour;
    	}
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				MaClasse thisClass = new MaClasse();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				thisClass.setVisible(true);
    			}
    		});
    	}
     
     
     
    	public MaClasse() {
    		super();
    		initialize();
    		Dessin panel = new Dessin();
    		add(panel);
    	}
     
     
     
    	private void initialize() {
    		this.setSize(1000, 750);
    		this.setContentPane(getJContentPane());
    		this.setTitle("JFrame");
    	}
     
     
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.setForeground(new Color(51, 102, 120));
    			jContentPane.add(getJPanelConfiguration(), null);
    			jContentPane.add(getJPanelCarte(), null);
    			jContentPane.add(getJPanelSimulation(), null);
     
    		}
    		return jContentPane;
     
     
    		}
     
    }


    Par contre cela m'a amené à un autre problème...
    Ma combobox permet à l'utilisateur de choisir le nombre de tour pour lequel il veut voir l'affichage du jpaneldessin...

    Et ce que j'ai mis dans la définition de ma jComboBox me suffit-elle pour pouvoir récupérer cette valeur, afin que je puisse l'insérer dans ma méthode de calcul ??
    Par ce qu'il s'agit en plus de valeur String, et non pas de chiffre à proprement parlé...

  7. #7
    Membre confirmé
    Profil pro
    Ingénieur
    Inscrit en
    Mars 2009
    Messages
    152
    Détails du profil
    Informations personnelles :
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Mars 2009
    Messages : 152
    Par défaut
    j'ai modifié ton code pour qu'il me soit plus clair, c'est perso, j'ai jamais vu ta façon de coder... C'était donc plus facile pour moi.

    Pour résoudre ton problème j'ai utilisé le type enumeration:

    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
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
     
     
     
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.GridBagLayout;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.awt.GridBagConstraints;
    import java.awt.SystemColor;
    import java.awt.Rectangle;
    import javax.swing.JButton;
    import javax.swing.JToggleButton;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Point;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.JComboBox;
     
     
     
     
     
     
    @SuppressWarnings("serial")
    public class MaClasse extends JFrame implements ActionListener {
     
    	// Enumeration 
    	public enum Tour {	
    		VIDE("",-1),
    		UN("1",1),
    		DEUX("2",2),
    		CINQ("5",5),
    		DIX("10",10),
    		CENT("100",100);
     
     
    		String[] tourString = { "","1", "2", "5", "10", "100" };
    		protected String label;
    		protected int valeur;
     
    		/** Constructeur */
    		Tour(String plabel, int valeur) {
    			this.label=plabel;
    			this.valeur = valeur;
    		}
     
    		public int getValeur() {
    			return this.valeur;
    		}
     
    		public String getLabel() {
    			return this.label;
    		}
    	}
     
     
    	private JButton jButtonRemiseAZero;
    	private JToggleButton jToggleButtonAB1;
    	private JButton jButtonGenerer = null;
    	private JComboBox jComboBoxNbTour;
     
    	public static void main(String[] args) {
    		new MaClasse();
    	}
     
    	/**
             * Constructeur
             */
    	public MaClasse(){
     
    		// jButtonGenerer
     
    		jButtonGenerer = new JButton();
    		jButtonGenerer.setLocation(new Point(120, 520));
    		jButtonGenerer.setText("Générer");
    		jButtonGenerer.setSize(new Dimension(150, 40));
     
     
    		//jToggleButtonAB1
     
    		jToggleButtonAB1 = new JToggleButton();
    		jToggleButtonAB1.setLocation(new Point(30, 395));
    //		jToggleButtonAB1.setIcon(new ImageIcon(
    //				"C:/Users/Sylvain/Documents/Eclipse workspace/Combat/Armée bleue 1.jpg"));
    		jToggleButtonAB1.setSize(new Dimension(100, 80));
     
     
    		// jButtonRemiseAZero
     
    		jButtonRemiseAZero = new JButton();
    		jButtonRemiseAZero.setText("Remise à Zéro");
    		jButtonRemiseAZero.setSize(new Dimension(150, 40));
    		jButtonRemiseAZero.setLocation(new Point(120, 24));
    		jButtonRemiseAZero.addActionListener(this);
     
     
    		// jComboBoxNbTour
     
    		List<String> tourString = new ArrayList<String>();
    		for(Tour tour : Tour.values()){
    			tourString.add(tour.getLabel());
    		}
    		jComboBoxNbTour = new JComboBox(tourString.toArray());
    		jComboBoxNbTour.setPreferredSize(new Dimension(80, 25));
    		jComboBoxNbTour.addActionListener(this);
     
     
    		// jPanelConfiguration
     
    		JPanel jPanelConfiguration = new JPanel();
    		jPanelConfiguration.setBounds(new Rectangle(0, 0, 384, 600));
    		jPanelConfiguration.setLayout(null);
    		jPanelConfiguration.setBackground(SystemColor.activeCaption);
    		jPanelConfiguration.add(jButtonRemiseAZero, null);
    		jPanelConfiguration.add(jToggleButtonAB1, null);
    		jPanelConfiguration.add(jButtonGenerer, null);
     
     
    		// jPanelSimulation
     
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.fill = GridBagConstraints.VERTICAL;
    		gridBagConstraints.gridy = 1;
    		gridBagConstraints.weightx = 1.0;
    		gridBagConstraints.gridx = 0;
     
    		JPanel jPanelSimulation = new JPanel();
    		jPanelSimulation.setLayout(new GridBagLayout());
    		jPanelSimulation.setBackground(new Color(84, 57, 0));
    		jPanelSimulation.setBounds(new Rectangle(0, 600, 984, 115));
    		jPanelSimulation.add(jComboBoxNbTour, gridBagConstraints);
     
     
    		// jPanelCarte
     
    		JPanel jPanelCarte = new JPanel();
    		jPanelCarte.setLayout(new GridBagLayout());
    		jPanelCarte.setBounds(new Rectangle(384, 0, 600, 600));
    		jPanelCarte.setBackground(Color.green);
     
     
    		// jContentPane
     
    		JPanel jContentPane = new JPanel();
    		jContentPane.setLayout(null);
    		jContentPane.setForeground(new Color(51, 102, 120));
    		jContentPane.add(jPanelConfiguration, null);
    		jContentPane.add(jPanelCarte, null);
    		jContentPane.add(jPanelSimulation, null);
     
     
    		// option de la JFrame
    		this.setSize(1000, 750);
    		this.setContentPane(jContentPane);
    		this.setTitle("JFrame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
     
     
     
    	@Override
    	public void actionPerformed(ActionEvent ae) {
    		if(ae.getSource().equals(jComboBoxNbTour)){
    			String tourLabel = (String) jComboBoxNbTour.getSelectedItem();
     
    			for(Tour tour : Tour.values()){
    				if(tour.getLabel().equals(tourLabel)){
    					System.out.println(tour.getValeur());
    				}
    			}
    		}
    	}
     
    }

Discussions similaires

  1. [Debutant] créer un JComboBox dans une JFrame
    Par sliderman dans le forum Agents de placement/Fenêtres
    Réponses: 8
    Dernier message: 15/01/2008, 21h26
  2. Couleur d'une JComboBox disabled
    Par ced dans le forum Composants
    Réponses: 6
    Dernier message: 06/01/2004, 15h33
  3. [Réseau] Créer une connexion Internet
    Par Tranber dans le forum VB 6 et antérieur
    Réponses: 11
    Dernier message: 17/10/2002, 17h01
  4. Créer une fenêtre flottante qui ne peut avoir le focus
    Par BestofMac dans le forum Composants VCL
    Réponses: 4
    Dernier message: 17/07/2002, 10h46
  5. Peux t'on créer une copie locale de l'objet partagé?
    Par Anonymous dans le forum CORBA
    Réponses: 8
    Dernier message: 16/04/2002, 16h20

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