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] ajouter une ligne


Sujet :

Composants Java

  1. #1
    Membre confirmé Avatar de rvfranck
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2004
    Messages
    746
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2004
    Messages : 746
    Points : 534
    Points
    534
    Par défaut [JTable] ajouter une ligne
    Salut,
    Je voudrais ajouter une ligne à ma JTable, lorsque l'utilisateur clique sur le bouton cmd1:
    Voici le code associé au bouton cmd1où table est ma JTable)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
     
    public void actionPerformed(ActionEvent e)
    	{
    		DefaultTableModel table_defaut = (DefaultTableModel)table.getModel();
    		Object [] nouv_ligne = null;
    		table_defaut.addRow(nouv_ligne);
    	}
    quand je clique sur le bouton cmd1 j'ai ça à l'écran:


    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: jtable.Tableau$1
    at jtable.Tableau.actionPerformed(Tableau.java:54)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    Merci de m'aider, au cas où se serait necessaire, voilà tout 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
     
     
     
    package jtable;
     
    import javax.swing.table.*;
    import javax.swing.*;
     
    import java.awt.event.*;
    import java.awt.*;
     
    public class Tableau extends JPanel implements ActionListener
    {
    	JTable table; // ma JTable
     
    	public Tableau()
    	{
    		setLayout(new FlowLayout());
     
    		// Le model de données
     
    		TableModel datamodel = new AbstractTableModel()
    		{
    			public int getRowCount(){return 3;}
    			public int getColumnCount(){return 4;}
    			public Object getValueAt(int ligne, int col) { return null;}
    			public boolean isCellEditable(int ligne, int col){return true;}
    			public String getColumnName(int col)
    			{
    				String val = null;
     
    				switch(col){
    					case 0: val = "Nom";break; 
    					case 1: val = "Prénom";break;
    					case 2: val = "Age";break;
    					case 3: val = "Genre";break;
    				}
     
    				return val;			
    			}
     
    		};
     
     
    		table = new JTable(datamodel);
     
    		JScrollPane bardefil = new JScrollPane(table);
     
    		// le bouton sur lequel il faut cliquer pour ajouter la ligne
    		JButton cmd1 = new JButton("Ajouter une Ligne");
    		cmd1.addActionListener(this);
     
    		add(bardefil);
    		add(cmd1);
     
    	}
     
    	public void actionPerformed(ActionEvent e)
    	{
    		DefaultTableModel table_defaut = (DefaultTableModel)table.getModel();
    		Object [] nouv_ligne = null;
    		table_defaut.addRow(nouv_ligne);
    	}
     
    	public static void main(String [] arg)
    	{
    		JFrame f = new JFrame("Les JTables");
    		Container c = f.getContentPane();
    		c.setLayout(new FlowLayout());
    		Tableau panel = new Tableau();
     
    		f.addWindowListener(new WindowAdapter()
    				{
    					public void windowClosing(WindowEvent e)
    					{
    						System.exit(0);
    					}
    				});
     
    		c.add(panel);
    		f.pack();
    		f.setVisible(true);
    	}
    }

  2. #2
    Membre expérimenté
    Avatar de Adjanakis
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    739
    Détails du profil
    Informations personnelles :
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations forums :
    Inscription : Avril 2004
    Messages : 739
    Points : 1 351
    Points
    1 351
    Par défaut
    Salut...

    A mon avis c'est une erreur de logique dans votre programme. Quand vous déclarez votre TableModel en instanciant un AbstractTableModel enrichit, Java n'assimile pas cet objet à un DefaultTableModel

    De ce fait, le cast est impossible. La meilleure solution serait de définir une nouvelle classe qui hériterait de DefaultTableMode sur laquelle un cast fonctionnerait. Cependant, il y a d'autre solution.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Avril 2002
    Messages
    77
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Avril 2002
    Messages : 77
    Points : 73
    Points
    73
    Par défaut
    salut !

    essaye ceci et si ça marche tu l'ajustera à tes besoins
    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
     
    import javax.swing.table.*; 
    import javax.swing.*; 
     
    import java.awt.event.*; 
    import java.awt.*; 
     
    public class essai extends JPanel implements ActionListener 
    { 
    	Object[] data = new Object[5];
        DefaultTableModel table_defaut = new DefaultTableModel();
        JTable  table = new JTable(table_defaut); 
     
       public essai() 
       { 
          setLayout(new FlowLayout()); 
     
     
          for (int h = 0 ; h < 5 ; h++)
       		{
       			table_defaut.addColumn("colonne "+ h);
       		}
     
     
          for (int row = 0 ; row < 5 ; row++)
       		{
       			for (int colonne = 0 ; colonne < 5 ; colonne++)
       			{
       				data[colonne] = "";
       			}
       		table_defaut.addRow(data);
       		}
     
          JScrollPane bardefil = new JScrollPane(table); 
     
          // le bouton sur lequel il faut cliquer pour ajouter la ligne 
          JButton cmd1 = new JButton("Ajouter une Ligne"); 
          cmd1.addActionListener(this); 
     
          add(bardefil); 
          add(cmd1); 
     
       } 
     
       public void actionPerformed(ActionEvent e) 
       { 
     
     		int i = table_defaut.getRowCount();
       		int j = table_defaut.getColumnCount();
       		Object[] data = new Object[j];
     
       		for (int h = 0 ; h < j ; h++)
       		{
       			data[h] = "";
       		}
       		table_defaut.addRow(data);
     
       } 
     
       public static void main(String [] arg) 
       { 
          JFrame f = new JFrame("Les JTables"); 
          Container c = f.getContentPane(); 
          c.setLayout(new FlowLayout()); 
          essai panel = new essai(); 
     
          f.addWindowListener(new WindowAdapter() 
                { 
                   public void windowClosing(WindowEvent e) 
                   { 
                      System.exit(0); 
                   } 
                }); 
     
          c.add(panel); 
          f.pack(); 
          f.setVisible(true); 
       } 
    }
    Bonne chance

  4. #4
    Membre confirmé Avatar de rvfranck
    Profil pro
    Étudiant
    Inscrit en
    Novembre 2004
    Messages
    746
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2004
    Messages : 746
    Points : 534
    Points
    534
    Par défaut
    Merci ça a marché, j'ai ajuster ton code à ce que je voulais.

    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
     
     
     
    package jtable;
     
    import javax.swing.table.*; 
    import javax.swing.*; 
     
    import java.awt.event.*; 
    import java.awt.*;
     
    public class Tableau extends JPanel implements ActionListener 
    { 
       Object[] data = new Object[4]; 
        DefaultTableModel table_defaut = new DefaultTableModel()
    	{
        	public int getColumnCount(){return 4;}
        	public String getColumnName(int col)
    		{
    			String val = null;
     
    			switch(col){
    				case 0: val = "Nom";break; 
    				case 1: val = "Prénom";break;
    				case 2: val = "Age";break;
    				case 3: val = "Genre";break;
    			}
     
    			return val;			
    		} 
    	};
     
    	JTable  table = new JTable(table_defaut); 
     
       public Tableau() 
       { 
          setLayout(new FlowLayout()); 
     
          JScrollPane bardefil = new JScrollPane(table); 
     
          // le bouton sur lequel il faut cliquer pour ajouter la ligne 
          JButton cmd1 = new JButton("Ajouter une Ligne"); 
          cmd1.addActionListener(this); 
     
          add(bardefil); 
          add(cmd1); 
     
       } 
     
       public void actionPerformed(ActionEvent e) 
       {   
             Object[] data = null;
             table_defaut.addRow(data);    
       } 
     
       public static void main(String [] arg) 
       { 
          JFrame f = new JFrame("Les JTables"); 
          Container c = f.getContentPane(); 
          c.setLayout(new FlowLayout()); 
          Tableau panel = new Tableau(); 
     
          f.addWindowListener(new WindowAdapter() 
                { 
                   public void windowClosing(WindowEvent e) 
                   { 
                      System.exit(0); 
                   } 
                }); 
     
          c.add(panel); 
          f.pack(); 
          f.setVisible(true); 
       } 
    }
    Merci.

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

Discussions similaires

  1. [JTable] Ajouter une ligne
    Par aitbouhou dans le forum Composants
    Réponses: 1
    Dernier message: 22/07/2008, 16h32
  2. [JTable] Ajouter une ligne dans un table triée
    Par AliJava dans le forum Composants
    Réponses: 12
    Dernier message: 22/12/2007, 15h17
  3. Ajouter une ligne à JTable
    Par ShredLord dans le forum Composants
    Réponses: 1
    Dernier message: 13/11/2007, 22h15
  4. [JTABLE] ajouter une ligne
    Par Dokho1000 dans le forum Composants
    Réponses: 12
    Dernier message: 13/09/2006, 13h52
  5. [JTable] ajouter une ligne
    Par maminova77 dans le forum Composants
    Réponses: 6
    Dernier message: 26/03/2006, 19h28

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