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 :

JTable n'affiche pas le vecteur entete.


Sujet :

Composants Java

  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 JTable n'affiche pas le vecteur entete.
    Bonjour A vous,

    Avez vous une idée???
    le code suivant ne doit pas exister, c'est juste pour un test.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Vector cl = new Vector();
    		cl.addElement("Column AFC");
    		cl.addElement("Column Country");
    		cl.addElement("Column Year");
    J'ai une table qui est supposé affiché les données provenant d'un sql.
    la table ne fonctionne pas sauf si je remplace dans
    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
    private JTable getJTable() {
     
    		//DefaultTableModel td = new DefaultTableModel(columnData);
     
    		Vector cl = new Vector();
    		cl.addElement("Column AFC");
    		cl.addElement("Column Country");
    		cl.addElement("Column Year");
     
    		if(jTable == null) {
    			jTable = new JTable();
    			//jTable = new JTable(columnData,columnNames);
    			jTable = new JTable(columnData,cl);
    		}
    		return jTable;
    	}
    la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    jTable = new JTable(columnData,columnNames);
    par la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    jTable = new JTable(columnData,cl);
    voici le code complet.
    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
    package day1;
     
    import java.sql.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.table.*;
     
    import com.sun.jndi.ldap.EntryChangeResponseControl;
     
     
    public class AccessConnect1 extends JFrame {
     
    	private JPanel jContentPane = null;
    	private JComboBox jComboBox = null;
     
    	Vector dataCombo = new Vector();
    	private Vector columnNames = new Vector();
    	Vector columnData = new Vector();
     
     
    	private JScrollPane jScrollPane = null;
    	private JTable jTable = null;
     
    	public static void main(String[] args) {
    		AccessConnect1 cn = new AccessConnect1();
     
    	}
    	/**
             * This is the default constructor
             */
    	public AccessConnect1() {
    		super();
    		initialize();
    		getDataListCombo();
    		getDataList();
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	/**
             * This method initializes this
             * 
             * @return void
             */
    	private void initialize() {
    		this.setSize(432, 200);
    		this.setContentPane(getJContentPane());
    	}
    	/**
             * 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(getJScrollPane(), null);
    		}
    		return jContentPane;
    	}
     
    	public void getDataListCombo() {
    		//item = (String) jComboBox.getSelectedItem();
     
    		int countRows = 0;
    		//System.out.println(countRows + "----" + item);
    		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  ORDER BY Country");
     
    			while (rec.next()) {
    				countRows++;
    				dataCombo.addElement(rec.getObject(1));
    			}
    			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());
    		}
     
    		getJComboBox();
     
    	}
     
    	public void getDataList() {
    			//item = (String) jComboBox.getSelectedItem();
     
    			int countRows = 0;
    			//System.out.println(countRows + "----" + item);
    			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 FIPS, Country, Year FROM Coal  " +
    					//"WHERE " + 	"(Country='" + item + "') " +
    					"ORDER BY Country");
     
    				ResultSetMetaData md = rec.getMetaData();
    				int columns = md.getColumnCount();
    				Vector columnNames = new Vector(columns);
    				for (int i = 1; i <= columns; i++) {
    					columnNames.addElement(md.getColumnName(i).toString());
    				}
     
    				while (rec.next()) {
    					countRows++;
    					Vector row = new Vector(columns);
    					for (int i = 1; i <= columns; i++) {
    						row.addElement(rec.getObject((i)));
    					}
    					columnData.addElement(row);
     
    				}
    				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());
    			}
     
    			getJTable();		
    		}
     
     
    	/**
             * This method initializes jComboBox
             * 
             * @return javax.swing.JComboBox
             */
    	private JComboBox getJComboBox() {
    		if(jComboBox == null) {
    			jComboBox = new JComboBox();
    			final DefaultComboBoxModel model = new DefaultComboBoxModel(dataCombo);
    			jComboBox = new JComboBox(model);
    			jComboBox.setBounds(185, 17, 223, 25);
    			jComboBox.addActionListener(new java.awt.event.ActionListener() { 
    				public void actionPerformed(java.awt.event.ActionEvent e) {    
    					if (jComboBox.getSelectedItem().equals("USA")){
    						//getDataList();
    						System.out.println(columnData.get(2));
    					}
    				}
    			});
    		}
    		return jComboBox;
    	}
    	/**
             * This method initializes jTable
             * 
             * @return javax.swing.JTable
             */
    	private JTable getJTable() {
     
    		//DefaultTableModel td = new DefaultTableModel(columnData);
     
    		Vector cl = new Vector();
    		cl.addElement("Column AFC");
    		cl.addElement("Column Country");
    		cl.addElement("Column Year");
     
    		if(jTable == null) {
    			jTable = new JTable();
    			//jTable = new JTable(columnData,columnNames);
    			jTable = new JTable(columnData,cl);
    		}
    		return jTable;
    	}
    	/**
             * This method initializes jScrollPane
             * 
             * @return javax.swing.JScrollPane
             */
    	private JScrollPane getJScrollPane() {
    		//getDataList();
    		if(jScrollPane == null) {
    			jScrollPane = new JScrollPane();
    			jScrollPane.setBounds(15, 61, 394, 99);
    			jScrollPane.setViewportView(getJTable());
    			repaint();
     
    		}
    		//jScrollPane.setViewportView(getJTable());
    		return jScrollPane;
    	}
     
     
    } //  @jve:visual-info  decl-index=0 visual-constraint="12,4"

  2. #2
    Membre très actif
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
      //Vector columnNames = new Vector(columns);                                                                    for (int i = 1; i <= columns; i++) {                                                                                    columnNames.addElement(md.getColumnName(i).toString());                                                                    }

  3. #3
    Membre très actif
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Par défaut
    tu as un menbre columnNames


    et une variable locale columnNames

    bref tu n'est pas au clair à ce niveau là.

  4. #4
    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
    meme si je commente cette ligne, ça ne fonctionne tjs pas
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    //Vector columnNames = new Vector(columns);

  5. #5
    Membre très actif
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Par défaut
    oui, je ne peux pas tester ce code.

    essaye de simplifier ton exemple. Met des debug en place. Vérifie à chaque étapes que tes objects soient correctement valorisés. Bref un peu de méthode et de rigueur pour comprendre ce que l'on fait.

    bon courage.

  6. #6
    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
    J'ai beau essayé, je n'arrive pas a voir le probleme!!!

  7. #7
    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 : 41
    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
    J'ai testé le code en remplaçant les accès à la base de données par des remplissages manuels et ca fonctionne ...

    Le problème c'est donc tes accès à la base de données.
    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

  8. #8
    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
    salut Nemek,
    envois moi le code que tu as testé sur ta station, sans l'acces a la BD.
    merci,

  9. #9
    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 : 41
    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
    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
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
     
    import java.util.Vector;
     
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
     
     
    public class Test extends JFrame {
     
    	private JPanel jContentPane = null;
    	private JComboBox jComboBox = null;
     
    	Vector dataCombo = new Vector();
    	private final Vector columnNames = new Vector();
    	Vector columnData = new Vector();
     
     
    	private JScrollPane jScrollPane = null;
    	private JTable jTable = null;
     
    	public static void main(final String[] args) {
    		final Test cn = new Test();
     
    	}
    	/**
             * This is the default constructor
             */
    	public Test() {
    		super();
    		initialize();
    		getDataListCombo();
    		getDataList();
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		this.setSize(432, 200);
    		this.setContentPane(getJContentPane());
    	}
    	/**
             * 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(getJScrollPane(), null);
    		}
    		return jContentPane;
    	}
     
    	public void getDataListCombo() {
    		//item = (String) jComboBox.getSelectedItem();
     
    		final int countRows = 0;
    		// System.out.println(countRows + "----" + item);
    //		final String data = "jdbc:odbc:WorldEnergy";
    //		try {
    //			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //			final Connection conn = DriverManager.getConnection(data, "", "");
    //			final Statement st = conn.createStatement();
    //			final ResultSet rec =
    //				st.executeQuery("SELECT DISTINCT Country FROM Coal  ORDER BY Country");
    //
    //			while (rec.next()) {
    //				countRows++;
    //				dataCombo.addElement(rec.getObject(1));
    //			}
    //			st.close();
    //
    //		} catch (final SQLException s) {
    //			System.out.println("SQL Error: "+ s.toString()+ " "	+ s.getErrorCode()
    //					+ " "
    //					+ s.getSQLState());
    //		} catch (final Exception e) {
    //			System.out.println("Error: " + e.toString() + e.getMessage());
    //		}
    		dataCombo.addElement("France");
    		dataCombo.addElement("Germany");
     
    		getJComboBox();
     
    	}
     
    	public void getDataList() {
    			//item = (String) jComboBox.getSelectedItem();
     
    			final int countRows = 0;
    			//System.out.println(countRows + "----" + item);
    //			final String data = "jdbc:odbc:WorldEnergy";
    //			try {
    //				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    //				final Connection conn = DriverManager.getConnection(data, "", "");
    //				final Statement st = conn.createStatement();
    //				final ResultSet rec =
    //					st.executeQuery("SELECT FIPS, Country, Year FROM Coal  " +
    //					//"WHERE " + 	"(Country='" + item + "') " +
    //					"ORDER BY Country");
    //
    //				final ResultSetMetaData md = rec.getMetaData();
    //				final int columns = md.getColumnCount();
    //				final Vector columnNames = new Vector(columns);
    //				for (int i = 1; i <= columns; i++) {
    //					columnNames.addElement(md.getColumnName(i).toString());
    //				}
     
    //				while (rec.next()) {
    //					countRows++;
    //					final Vector row = new Vector(columns);
    //					for (int i = 1; i <= columns; i++) {
    //						row.addElement(rec.getObject((i)));
    //					}
    //					columnData.addElement(row);
    //
    //				}
    //				st.close();
    //
    //			} catch (final SQLException s) {
    //				System.out.println("SQL Error: "+ s.toString()+ " "	+ s.getErrorCode()
    //						+ " "
    //						+ s.getSQLState());
    //			} catch (final Exception e) {
    //				System.out.println("Error: " + e.toString() + e.getMessage());
    //			}
          columnNames.addElement("FIPS");
          columnNames.addElement("Country");
          columnNames.addElement("Year");
     
          Vector row = new Vector(3);
          row.add("0");
          row.add("France");
          row.add("2009");
          columnData.addElement(row);
          row = new Vector(3);
          row.add("1");
          row.add("France");
          row.add("2010");
          columnData.addElement(row);
          row = new Vector(3);
          row.add("2");
          row.add("France");
          row.add("2011");
          columnData.addElement(row);
          row = new Vector(3);
          row.add("3");
          row.add("Germany");
          row.add("2009");
          columnData.addElement(row);
          row = new Vector(3);
          row.add("4");
          row.add("Germany");
          row.add("2010");
          columnData.addElement(row);
          row = new Vector(3);
          row.add("5");
          row.add("Germany");
          row.add("2011");
          columnData.addElement(row);
    			getJTable();
    		}
     
     
    	/**
             * This method initializes jComboBox
             *
             * @return javax.swing.JComboBox
             */
    	private JComboBox getJComboBox() {
    		if(jComboBox == null) {
    			jComboBox = new JComboBox();
    			final DefaultComboBoxModel model = new DefaultComboBoxModel(dataCombo);
    			jComboBox = new JComboBox(model);
    			jComboBox.setBounds(185, 17, 223, 25);
    			jComboBox.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(final java.awt.event.ActionEvent e) {
    					if (jComboBox.getSelectedItem().equals("USA")){
    						//getDataList();
    						System.out.println(columnData.get(2));
    					}
    				}
    			});
    		}
    		return jComboBox;
    	}
    	/**
             * This method initializes jTable
             *
             * @return javax.swing.JTable
             */
    	private JTable getJTable() {
     
    		//DefaultTableModel td = new DefaultTableModel(columnData);
     
    		final Vector cl = new Vector();
    		cl.addElement("Column AFC");
    		cl.addElement("Column Country");
    		cl.addElement("Column Year");
     
    		if(jTable == null) {
    			jTable = new JTable();
    			//jTable = new JTable(columnData,columnNames);
    			jTable = new JTable(columnData,cl);
    		}
    		return jTable;
    	}
    	/**
             * This method initializes jScrollPane
             *
             * @return javax.swing.JScrollPane
             */
    	private JScrollPane getJScrollPane() {
    		//getDataList();
    		if(jScrollPane == null) {
    			jScrollPane = new JScrollPane();
    			jScrollPane.setBounds(15, 61, 394, 99);
    			jScrollPane.setViewportView(getJTable());
    			repaint();
     
    		}
    		//jScrollPane.setViewportView(getJTable());
    		return jScrollPane;
    	}
     
     
    } //  @jve:visual-info  decl-index=0 visual-constraint="12,4"
    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

  10. #10
    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
    Nemek,
    Mon probleme reste tjs posé, j'ai peut etre mal posé ma question, ce que je veux, c'est que qaund je selectionne "France" du combo box, la table affiche les 3 lignes de France, de meme que pour Germany.
    est ce que c'est indisponsable de passer par un JTableModel??? pour faire ce genre de rafraichisement de table.
    merci,

    Regarde le code, j'en suis certain que tu vas comprendre ce que je veux avoir.
    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
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    /*
     * Created on Apr 5, 2011
     *
     * To change the template for this generated file go to
     * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
     */
    package day1;
     
    import java.util.Vector;
     
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
     
    public class Test extends JFrame {
     
    	private JPanel jContentPane = null;
    	private JComboBox jComboBox = null;
     
    	Vector dataCombo = new Vector();
    	private final Vector columnNames = new Vector();
    	Vector columnData = new Vector();
     
    	private JScrollPane jScrollPane = null;
    	private JTable jTable = null;
     
    	public static void main(final String[] args) {
    		final Test cn = new Test();
     
    	}
    	/**
             * This is the default constructor
             */
    	public Test() {
    		super();
    		initialize();
    		getDataListCombo();
    		getDataList();
     
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    	/**
             * This method initializes this
             *
             * @return void
             */
    	private void initialize() {
    		this.setSize(432, 200);
    		this.setContentPane(getJContentPane());
    	}
    	/**
             * 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(getJScrollPane(), null);
    		}
    		return jContentPane;
    	}
     
    	public void getDataListCombo() {
    		//item = (String) jComboBox.getSelectedItem();
     
    		final int countRows = 0;
    		// System.out.println(countRows + "----" + item);
    		//		final String data = "jdbc:odbc:WorldEnergy";
    		//		try {
    		//			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		//			final Connection conn = DriverManager.getConnection(data, "", "");
    		//			final Statement st = conn.createStatement();
    		//			final ResultSet rec =
    		//				st.executeQuery("SELECT DISTINCT Country FROM Coal  ORDER BY Country");
    		//
    		//			while (rec.next()) {
    		//				countRows++;
    		//				dataCombo.addElement(rec.getObject(1));
    		//			}
    		//			st.close();
    		//
    		//		} catch (final SQLException s) {
    		//			System.out.println("SQL Error: "+ s.toString()+ " "	+ s.getErrorCode()
    		//					+ " "
    		//					+ s.getSQLState());
    		//		} catch (final Exception e) {
    		//			System.out.println("Error: " + e.toString() + e.getMessage());
    		//		}
    		dataCombo.addElement("France");
    		dataCombo.addElement("Germany");
     
    		getJComboBox();
     
    	}
     
    	public void getDataList() {
    		//item = (String) jComboBox.getSelectedItem();
     
    		final int countRows = 0;
    		//System.out.println(countRows + "----" + item);
    		//			final String data = "jdbc:odbc:WorldEnergy";
    		//			try {
    		//				Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		//				final Connection conn = DriverManager.getConnection(data, "", "");
    		//				final Statement st = conn.createStatement();
    		//				final ResultSet rec =
    		//					st.executeQuery("SELECT FIPS, Country, Year FROM Coal  " +
    		//					//"WHERE " + 	"(Country='" + item + "') " +
    		//					"ORDER BY Country");
    		//
    		//				final ResultSetMetaData md = rec.getMetaData();
    		//				final int columns = md.getColumnCount();
    		//				final Vector columnNames = new Vector(columns);
    		//				for (int i = 1; i <= columns; i++) {
    		//					columnNames.addElement(md.getColumnName(i).toString());
    		//				}
     
    		//				while (rec.next()) {
    		//					countRows++;
    		//					final Vector row = new Vector(columns);
    		//					for (int i = 1; i <= columns; i++) {
    		//						row.addElement(rec.getObject((i)));
    		//					}
    		//					columnData.addElement(row);
    		//
    		//				}
    		//				st.close();
    		//
    		//			} catch (final SQLException s) {
    		//				System.out.println("SQL Error: "+ s.toString()+ " "	+ s.getErrorCode()
    		//						+ " "
    		//						+ s.getSQLState());
    		//			} catch (final Exception e) {
    		//				System.out.println("Error: " + e.toString() + e.getMessage());
    		//			}
    		columnNames.addElement("FIPS");
    		columnNames.addElement("Country");
    		columnNames.addElement("Year");
     
    		if (jComboBox.getSelectedIndex() == -1) { // au premier passage, afficher tous
    			Vector row = new Vector(3);
    			row = new Vector(3);
    			row.add("0");
    			row.add("France");
    			row.add("2009");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("1");
    			row.add("France");
    			row.add("2010");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("2");
    			row.add("France");
    			row.add("2011");
    			columnData.addElement(row);
     
    			row = new Vector(3);
    			row.add("3");
    			row.add("Germany");
    			row.add("2009");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("4");
    			row.add("Germany");
    			row.add("2010");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("5");
    			row.add("Germany");
    			row.add("2011");
    			columnData.addElement(row);
    		}
     
    		if (jComboBox.getSelectedIndex() == 0) { // Afficher uniquement 'France'
    			Vector row = new Vector(3);
    			row = new Vector(3);
    			row.add("0");
    			row.add("France");
    			row.add("2009");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("1");
    			row.add("France");
    			row.add("2010");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("2");
    			row.add("France");
    			row.add("2011");
    			columnData.addElement(row);
    		}
     
    		if (jComboBox.getSelectedIndex() == 1) { //Afficher uniquement 'Germany'
    			Vector row = new Vector(3);
    			row = new Vector(3);
    			row.add("3");
    			row.add("Germany");
    			row.add("2009");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("4");
    			row.add("Germany");
    			row.add("2010");
    			columnData.addElement(row);
    			row = new Vector(3);
    			row.add("5");
    			row.add("Germany");
    			row.add("2011");
    			columnData.addElement(row);
    		}
    		getJTable();
    	}
     
    	/**
             * This method initializes jComboBox
             *
             * @return javax.swing.JComboBox
             */
    	private JComboBox getJComboBox() {
    		if (jComboBox == null) {
    			jComboBox = new JComboBox();
    			final DefaultComboBoxModel model =
    				new DefaultComboBoxModel(dataCombo);
    			jComboBox = new JComboBox(model);
    			jComboBox.setBounds(185, 17, 223, 25);
    			jComboBox.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(
    					final java.awt.event.ActionEvent e) {
    					if (jComboBox.getSelectedItem().equals("USA")) {
    						//getDataList();
    						System.out.println(columnData.get(2));
    					}
     
    					getDataList();
    				}
    			});
    		}
    		return jComboBox;
    	}
    	/**
             * This method initializes jTable
             *
             * @return javax.swing.JTable
             */
    	private JTable getJTable() {
     
    		//DefaultTableModel td = new DefaultTableModel(columnData);
     
    		final Vector cl = new Vector();
    		cl.addElement("Column AFC");
    		cl.addElement("Column Country");
    		cl.addElement("Column Year");
     
    		if (jTable == null) {
    			jTable = new JTable();
    			//jTable = new JTable(columnData,columnNames);
    			jTable = new JTable(columnData, cl);
    		}
    		return jTable;
    	}
    	/**
             * This method initializes jScrollPane
             *
             * @return javax.swing.JScrollPane
             */
    	private JScrollPane getJScrollPane() {
    		//getDataList();
    		if (jScrollPane == null) {
    			jScrollPane = new JScrollPane();
    			jScrollPane.setBounds(15, 61, 394, 99);
    			jScrollPane.setViewportView(getJTable());
    			repaint();
     
    		}
    		//jScrollPane.setViewportView(getJTable());
    		return jScrollPane;
    	}
     
    }

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

Discussions similaires

  1. Scrollbar et JTable n'affiche pas tout
    Par oneagaindoguys dans le forum Composants
    Réponses: 1
    Dernier message: 01/02/2011, 12h18
  2. [JTable] n'affiche pas plus de 28 lignes.
    Par petrone dans le forum Composants
    Réponses: 7
    Dernier message: 18/07/2008, 16h59
  3. [JTable] les noms des colonnes de s'affichent pas
    Par macben dans le forum Composants
    Réponses: 6
    Dernier message: 25/04/2008, 11h03
  4. JTABLE n'affiche pas les titres
    Par arnaud036 dans le forum Composants
    Réponses: 2
    Dernier message: 04/09/2007, 09h31
  5. [JTable] Comment ne pas afficher les titres ?
    Par FabienBxl dans le forum Composants
    Réponses: 3
    Dernier message: 08/10/2003, 15h01

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