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 :

Alimenter JComboBox ERREUR


Sujet :

Composants Java

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    200
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 200
    Par défaut Alimenter JComboBox ERREUR
    Bonjour a tous,
    Debutant en Java Swing (Visual class de websphere)... quand je rempli le comboBox je reçoi une erreur, mais mon code fonctionne bien. voici le code

    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
    package day1;
     
    import javax.swing.*;
    import javax.swing.border.Border;
     
    import java.util.*;
    import java.sql.*;
     
     
    public class Test3 extends JFrame {
     
    	private JPanel jContentPane = null;
    	//	JComboBox jComboBox = new JComboBox();
    	private JComboBox jComboBox = null;
    	private JButton jButton = null;
     
    	public ArrayList countryTab = new ArrayList();
    	public String[] country = new String[136];
     
    	private javax.swing.JLabel jLabel = null;
    	public static void main(String[] args) {
    		Test3 ts = new Test3();
    	}
    	/**
             * This is the default constructor
             */
    	public Test3() {
    		super("World");
    		initialize();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		//getDataCombo();		// set up the combo country
     
    		JComboBox jcb = new JComboBox();
    		jContentPane.add(jcb);
     
    		setVisible(true);
    	}
    	/**
             * This method initializes this
             * 
             * @return void
             */
    	private void initialize() {
    		this.setContentPane(getJContentPane());
    		this.setSize(548, 349);
    		this.setTitle("My Swing");
    	}
    	/**
             * This method initializes jContentPane
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    			jContentPane.add(getJComboBox(), null);
    			jContentPane.add(getJButton(), null);
     
    			jContentPane.add(getJLabel(), null);
    		}
    		return jContentPane;
    	}
    	/**
             * This method initializes jComboBox
             * 
             * @return javax.swing.JComboBox
             */
    	private JComboBox getJComboBox() {
     
    		getDataCombo(); // set up the combo country
    		//JComboBox jComboBox = new JComboBox(country);
    			if (jComboBox == null) {
    			jComboBox = new JComboBox(country);
     
    			jComboBox.setBounds(256, 44, 182, 24);
    			jComboBox.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					System.out.println(
    						jComboBox.getItemCount()
    							+ "/"
    							+ jComboBox.getSelectedItem()
    							+ "/"
    							+ jComboBox.getSelectedIndex());
    					String ns = (String) jComboBox.getSelectedItem();
    					if (jComboBox.getSelectedItem().equals("Canada")) {
    						jButton.setEnabled(false);
    						jButton.setText("Ok");
    					} else {
    						jButton.setEnabled(true);
    						jButton.setText("Oui");
    					}
    				}
    			});
    			System.out.println("Null");
    		}
     
    		return jComboBox;
    	}
    	/**
             * This method initializes jButton
             * 
             * @return javax.swing.JButton
             */
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    			jButton.setBounds(446, 10, 69, 58);
    			jButton.setText("Go");
    			jButton.setIcon(
    				new ImageIcon(getClass().getResource("/images/pc.gif")));
    			jButton.setHorizontalTextPosition(
    				javax.swing.SwingConstants.CENTER);
    		}
    		return jButton;
    	}
     
    	public void getDataCombo() {
    		int i = 0;
    		String data = "jdbc:odbc:WorldEnergy";
    		try {
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    			Connection conn = DriverManager.getConnection(data, "", "");
    			Statement st = conn.createStatement();
    			ResultSet rec =
    				st.executeQuery("SELECT DISTINCT Country " + "FROM Coal  " +
    				//"WHERE " + 	"(Country='" + arguments[0] + "') " +
    	"ORDER BY Country");
    			while (rec.next()) {
    				String str = rec.getString(1);
    				//country[i] = rec.getString(1);
    				country[i] = str;
    				countryTab.add(rec);
    				i++;
    			}
    			//	jComboBox = new JComboBox(country);
    			st.close();
    		} catch (SQLException s) {
    			System.out.println(
    				"SQL Error: "
    					+ s.toString()
    					+ " "
    					+ s.getErrorCode()
    					+ " "
    					+ s.getSQLState());
    		} catch (Exception e) {
    			System.out.println("Error: " + e.toString() + e.getMessage());
    		}
    	}
     
    	/**
             * This method initializes jLabel
             * 
             * @return javax.swing.JLabel
             */
    	private JLabel getJLabel() {
    		if(jLabel == null) {
    			jLabel = new JLabel();
    			jLabel.setBounds(405, 12, 36, 22);
    			jLabel.setText("");
    			jLabel.setIcon(new ImageIcon(getClass().getResource("/images/icon_cry.gif")));
    			jLabel.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabel.setHorizontalTextPosition(SwingConstants.CENTER);
    		}
    		return jLabel;
    	}
    }  //  @jve:visual-info  decl-index=0 visual-constraint="18,8"
    et voici ce a quoi ressemble l'erreur sur l'image attachée...
    Images attachées Images attachées  

Discussions similaires

  1. Réponses: 4
    Dernier message: 11/07/2015, 22h37
  2. Erreur dans l'alimentation d'une liste de class
    Par m_brun dans le forum VB.NET
    Réponses: 2
    Dernier message: 21/06/2012, 11h10
  3. Erreur au niveau de jComboBox
    Par mdh12 dans le forum Débuter
    Réponses: 2
    Dernier message: 22/12/2010, 09h33
  4. [JComboBox] Erreur lors de la recupération d'une valeur
    Par patriot dans le forum Composants
    Réponses: 44
    Dernier message: 09/09/2010, 17h30
  5. Erreur d'affichage de mes JCombobox
    Par diditin dans le forum AWT/Swing
    Réponses: 4
    Dernier message: 08/06/2006, 19h54

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