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 :

problème d'ajouter une ligne sur jtable


Sujet :

Java

  1. #1
    Provisoirement toléré
    Inscrit en
    Avril 2007
    Messages
    679
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 679
    Points : 76
    Points
    76
    Par défaut problème d'ajouter une ligne sur jtable
    salut tout le monde
    j'ai un problème avec jtable je n'arrive pas à ajouter des nouveaux enregistremnt sur jtable

    pour mieux comprendre
    j'ai une table avec access "client" contient par exemple 5 lignes " je l'affiche sur jtable ça se passe , dans l'interface il y a un bouton ajouter un nouveau client sur jtable sur jtable j'ai 5 ligne normalemnt nouveau client s'affiche sur la 6 lignes de jtable mais c'est le contraire ça affiche sur le reste des ligne de jtable

    je n'ai pas compris pourquoi

    voila le code de cette fenetre

    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
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
     
     
    public class clients extends JFrame {
     
    	public static clients  musta1=null;
    	DefaultTableModel tableModel;
    	static DefaultTableModel tableModel1;
    	static String référence;
             static int some=0;
    	private JPanel jContentPane = null;
    	 int k= 0;
    	 int k1= 0;
    	private Vector data = null;   
     
    	private Vector columnNames = null;  
     
    	private JPanel jPanel = null;
     
    	private JScrollPane jScrollPane = null;
     
    	private JTable jTable = null;
     
    	private Vector data1 = null;   
     
    	private Vector columnNames1 = null;
     
    	private JLabel jLabel4 = null;
     
    	private JToolBar jJToolBarBar = null;
     
    	private JPanel jPanel2 = null;
     
    	private JButton jButton = null;
     
    	private Vector getData() {
    		if (data == null) {
    			data = new Vector();
    		}
    		return data;
    	}
     
     
    	private Vector getColumnNames() {
    		if (columnNames == null) {
    			columnNames = new Vector();
    		}
    		return columnNames;
    	}
     
     
     
    	private JPanel getJPanel() {
    		if (jPanel == null) {
    			jLabel4 = new JLabel();
    			jLabel4.setBounds(new Rectangle(180, 59, 692, 25));
    			jLabel4.setHorizontalAlignment(SwingConstants.CENTER);
    			jLabel4.setFont(new Font("Dialog", Font.BOLD, 14));
    			jLabel4.setBorder(LineBorder.createGrayLineBorder());
    			jPanel = new JPanel();
    			jPanel.setLayout(null);
    			jPanel.setBounds(new Rectangle(-17, 6, 880, 635));
    			jPanel.setBackground(new Color(0, 138, 255));
    			jPanel.add(getJScrollPane(), null);
    			jPanel.add(jLabel4, null);
    			jPanel.add(getJPanel2(), null);
    		}
    		return jPanel;
    	}
     
     
    	private JScrollPane getJScrollPane() {
    		if (jScrollPane == null) {
    			jScrollPane = new JScrollPane();
    			int vpolicy = jScrollPane.getVerticalScrollBarPolicy(); 
    			jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    			jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     
     
     
    			jScrollPane.setBounds(new Rectangle(180, 84, 694, 300));
    			jScrollPane.setViewportView(getJTable());
    		}
    		return jScrollPane;
    	}
     
     
    	private Vector getData1() {
    		if (data1 == null) {
    			data1 = new Vector();
    		}
    		return data1;
    	}
     
     
    	private Vector getColumnNames1() {
    		if (columnNames1 == null) {
    			columnNames1 = new Vector();
    		}
    		return columnNames1;
    	}
     
     
     
    	private JTable getJTable() {
    	if (jTable == null) {
     
    			Vector columnNames = new Vector();
     
    	         Vector data = new Vector();
     
     
     
     
    	   try {
     
    			 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); 
     
     
    			 String url= "jdbc:odbc:test";
    	 	 String user = "user"; 
    	 String pass = "pass";
    	 	Connection connection = DriverManager.getConnection (url, user, pass); 
    		    Statement instruction = connection.createStatement();
     
     
     
    		    String sql = " select  *  from  client " ;    
    		              ResultSet resultat  = instruction.executeQuery(sql);
    		              ResultSetMetaData md = resultat .getMetaData();
    		              int columns = md.getColumnCount();
     
     
    		              //  Get column names
     
    		              for (int id = 2; id < columns+1; id++) {
    		                columnNames.addElement(md.getColumnName(id));
     
    		              }
     
    		              //  Get row data
     
    		              while (resultat .next()) {
    		           Vector row = new Vector(columns);
     
    		                for (int j = 2; j <= columns; j++) {
     
    		                  row.addElement(resultat .getObject(j));
    		                }
     
    		                data.addElement(row);
    		                k++;
    		              }
    		              Vector row = new Vector(columns);
    		              for (int j1 = 2; j1 <= columns; j1++) {
     
    							row.addElement("");
    			                }
     
     
    		              for (int j1 = k; j1 <= 13; j1++) {
    		            	  data.addElement(row);
     
     
    			                }
     
    		              String nombre =String.valueOf(k);
    		              jLabel4.setText("                                      la liste des clients                      "+nombre+" enregistrments trouvés");
    		              tableModel = new DefaultTableModel(data, columnNames);
     
     
     
     
     jTable = new JTable(tableModel)  {
       public boolean isCellEditable(int rowIndex, int vColIndex) {
      return false;
    		                    }
    		                  }; 
     
       jTable.setRowHeight(20);
     
     
     
     
     
    jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    		              jTable.getColumnModel().getColumn(0).setPreferredWidth(70);
    		              jTable.getColumnModel().getColumn(1).setPreferredWidth(70);
    		              jTable.getColumnModel().getColumn(2).setPreferredWidth(170);
    		              jTable.getColumnModel().getColumn(3).setPreferredWidth(170);
    		              jTable.getColumnModel().getColumn(4).setPreferredWidth(170);
    		              jTable.getColumnModel().getColumn(5).setPreferredWidth(170);
    jTable.setBackground(Color.green);
      jTable.setSelectionBackground(Color.yellow);
      jTable.setFont(new Font("Arial", Font.PLAIN, 14));
     
     jTable.setDefaultRenderer(Object.class, new CenterTableCellRenderer());
      JTableHeader header = jTable.getTableHeader();
     header.setBackground(new Color(0, 138, 255));
     
     jTable.addMouseListener(new java.awt.event.MouseAdapter() {
    	public void mouseClicked(java.awt.event.MouseEvent e) {
    		              		System.out.println("mouseClicked()"); // TODO Auto-generated Event stub mouseClicked()
    Point p=e.getPoint();
    							int row=jTable.rowAtPoint(p);
    							String h1=jTable.getValueAt(row, 1).toString();
    							String h2=jTable.getValueAt(row, 2).toString();
    							String h3=jTable.getValueAt(row, 0).toString();
    							String h4=jTable.getValueAt(row, 3).toString();
    							String h5=jTable.getValueAt(row, 4).toString();
     
    		              	}
    		              });
     
     jTable.setVisible(true);        
     
     }catch (Exception ex) {
    				         		System.out.println("jjjjjjjjjjj "+ex.getMessage());
     
    								  ex.printStackTrace() ;
    							}
    }
    return jTable;
    	}
     
     
    private JToolBar getJJToolBarBar() {
    		if (jJToolBarBar == null) {
    			jJToolBarBar = new JToolBar();
    			jJToolBarBar .setFloatable(false);
    			jJToolBarBar.setBounds(new Rectangle(5, 34, 147, 151));
    			jJToolBarBar.setOrientation(JToolBar.VERTICAL);
    			jJToolBarBar.setBackground(new Color(0, 138, 255));
    			jJToolBarBar.add(getJButton());
    		}
    		return jJToolBarBar;
    	}
    	private JPanel getJPanel2() {
    		if (jPanel2 == null) {
    			jPanel2 = new JPanel();
    			jPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
    			jPanel2.setLayout(null);
    			jPanel2.setBounds(new Rectangle(22, 129, 155, 207));
    			jPanel2.setBackground(new Color(0, 138, 255));
    			jPanel2.add(getJJToolBarBar(), null);
    		}
    		return jPanel2;
    	}
     
     
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton(new ImageIcon("src/image/NOUVEAU.png"));
    			jButton.setText("   ajouter      ");
    			jButton.setFont(new Font("Dialog", Font.BOLD, 14));
    			jButton.addActionListener(new java.awt.event.ActionListener() {
    				public void actionPerformed(java.awt.event.ActionEvent e) {
    					ajoutclient  a	 = new ajoutclient();
     
    					  a.setVisible(true);
     
    				}
    			});
    		}
    		return jButton;
    	}
     
     
    	public static void main(String[] args) {
     
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    clients thisClass = new clients();
    				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    thisClass.setVisible(true);
    thisClass.setResizable (false);
    thisClass.setLocationRelativeTo(null);
    			}
    		});
    	}
    public clients() {
    super();
    initialize();
    	}
     
     
    private void initialize() {
    musta1=this;
    this.setSize(879, 679);
     
    this.setContentPane(getJContentPane());
    this.setTitle("JFrame");
    	}
     
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.setBackground(new Color(0, 138, 255));
    jContentPane.add(getJPanel(), null);
    		}
    return jContentPane;
    	}
     
     
    public class CenterTableCellRenderer extends DefaultTableCellRenderer {
      public CenterTableCellRenderer() {
      setHorizontalAlignment(CENTER);
       setVerticalAlignment(CENTER);
    	    }
    	}
    	public static int some1()
    	{
    	return some;
    	}
    	public static  DefaultTableModel  ref (   ){
     
    		return tableModel1;
     
    	}
    	 public void MaMethode1(String cv,String nom,String prenom,String adresse,String telephonne,String CP,String ville,String pays ){  
     
     jTable.setValueAt(cv, k, 0);
     jTable.setValueAt(nom, k, 1);
    jTable.setValueAt(prenom, k, 2);
    jTable.setValueAt(adresse,k,3);	 
    jTable.setValueAt(telephonne, k, 4);
    jTable.setValueAt(CP, k, 5);
    jTable.setValueAt(ville,k, 6);
    jTable.setValueAt(pays,k, 7);
     
    	   }
     
    }

    si je client sur le bouton voila le code qui permet d'ajouter nouvelle ligne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    public void MaMethode1(String cv,String nom,String prenom,String adresse,String telephonne,String CP,String ville,String pays ){    
       jTable.setValueAt(cv, k, 0);  
       jTable.setValueAt(nom, k, 1);  
      jTable.setValueAt(prenom, k, 2);  
      jTable.setValueAt(adresse,k,3);    
      jTable.setValueAt(telephonne, k, 4);  
      jTable.setValueAt(CP, k, 5);  
      jTable.setValueAt(ville,k, 6);  
      jTable.setValueAt(pays,k, 7);  
                }
    merci d'avance

  2. #2
    Membre averti
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    342
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 342
    Points : 419
    Points
    419
    Par défaut
    Si je ne me trompe pas setValueAt vas permetre de modifier une valeur existant et non d'en crée une

    si je me souvient bien il faut redéfinir un TableModel qui prend en compte l'ajoute de ligne

  3. #3
    Provisoirement toléré
    Inscrit en
    Avril 2007
    Messages
    679
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 679
    Points : 76
    Points
    76
    Par défaut
    je n'ai pas encore trouvé la solution quelqu'un pu m'aider

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    299
    Détails du profil
    Informations personnelles :
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Janvier 2009
    Messages : 299
    Points : 422
    Points
    422
    Par défaut
    Même réponse que rolfone.
    Utilise un DefaultTableModel (si tu n'en as pas déjà un à toi) et passe par lui pour ajouter des éléments à ta JTable.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
     
    // ou alors :
    // table.setModel(model);
     
    // tu as ensuite des méthodes d'ajout
    // model.addColumn();
    // model.addRow();
    // model.setValueAt();
    // ...

Discussions similaires

  1. [WD16] problème pour ajouter une ligne dans la table
    Par T1929D dans le forum WinDev
    Réponses: 3
    Dernier message: 02/08/2013, 22h39
  2. [JTable] Ajouter une écoute sur le bord d'une ligne
    Par GlamIS dans le forum Composants
    Réponses: 0
    Dernier message: 03/08/2011, 17h37
  3. [XL-2007] Probléme pour séléctionner une ligne sur trois dans une colonne
    Par ro31hg dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 18/02/2010, 20h48
  4. Ajouter une ligne sur un interaction.plot
    Par habasque dans le forum R
    Réponses: 1
    Dernier message: 25/06/2009, 15h11
  5. [JTable] ajouter une ligne
    Par rvfranck dans le forum Composants
    Réponses: 3
    Dernier message: 30/03/2005, 14h25

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