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] Lancer un traitement au moment de la sélection d'une ligne


Sujet :

Composants Java

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut [JTable] Lancer un traitement au moment de la sélection d'une ligne
    Bonjour,

    J'ai un JTable qui contient 4 colonnes, je veux lors d'une sélection d'une ligne lancer une fonction.

    Après quelques recherches j'ai trouvé la fonction table.getRowSelectionAllowed() mais cette fonction renvoie un boolean.

    Moi je veux récupérer tout l'objet de la ligne sélectionnée et y faire un traitement.

    Merci de m'indiquer comment je dois faire.

    Merci d'avance.

  2. #2
    Membre éclairé
    Avatar de bpy1401
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mars 2003
    Messages
    471
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Eure (Haute Normandie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Industrie

    Informations forums :
    Inscription : Mars 2003
    Messages : 471
    Points : 831
    Points
    831
    Par défaut
    Bonjour amine1980

    J'ai trouvé ceci sur le net

    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
     
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
     
    public class SelectTableExample
    		extends 	JFrame
    		implements	ListSelectionListener
     {
    	/**
       * 
       */
      private static final long serialVersionUID = 1L;
      // Instance attributes used in this example
    	private	JPanel		topPanel;
    	private	JTable		table;
    	private	JScrollPane scrollPane;
     
    	// Constructor of main frame
    	public SelectTableExample()
    	{
    		// Set the frame characteristics
    		setTitle( "Simple Table Application" );
    		setSize( 300, 200 );
    		setBackground( Color.gray );
     
    		// Create a panel to hold all other components
    		topPanel = new JPanel();
    		topPanel.setLayout( new BorderLayout() );
    		getContentPane().add( topPanel );
     
    		// Create columns names
    		String columnNames[] = { "Column 1", "Column 2", "Column 3" };
     
    		// Create some data
    		String dataValues[][] =
    		{
    			{ "12", "234", "67" },
    			{ "-123", "43", "853" },
    			{ "93", "89.2", "109" },
    			{ "279", "9033", "3092" }
    		};
     
    		// Create a new table instance
    		table = new JTable( dataValues, columnNames );
     
    		// Handle the listener
    		ListSelectionModel selectionModel = table.getSelectionModel();
    		selectionModel.addListSelectionListener( this );
     
    		// Add the table to a scrolling pane
    		scrollPane = new JScrollPane( table );
    		topPanel.add( scrollPane, BorderLayout.CENTER );
    	}
     
    	// Handler for list selection changes
     	public void valueChanged( ListSelectionEvent event )
     	{
     		// See if this is a valid table selection
    		if( event.getSource() == table.getSelectionModel()
    						&& event.getFirstIndex() >= 0 )
    		{
    			// Get the data model for this table
    			TableModel model = (TableModel)table.getModel();
     
    			// Determine the selected item
    			String string = (String)model.getValueAt(
    									table.getSelectedRow(),
    									table.getSelectedColumn() );
     
    			// Display the selected item
    			System.out.println( "Value selected = " + string );
    		}
     	}
     
    	// Main entry point for this example
    	public static void main( String args[] )
    	{
    		// Create an instance of the test application
    		SelectTableExample mainFrame	= new SelectTableExample();
    		mainFrame.setVisible( true );
    	}
    }
    Page sur Developpez : http://pbriand.developpez.com

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    350
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 350
    Points : 130
    Points
    130
    Par défaut
    Merci infiniment.

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

Discussions similaires

  1. Ecouter la sélection d'une ligne dans une JTable
    Par cisnake dans le forum Composants
    Réponses: 4
    Dernier message: 17/09/2012, 15h27
  2. Réponses: 2
    Dernier message: 01/06/2011, 16h10
  3. [JTable] Sélection d'une Ligne
    Par lanfeustdetroll dans le forum Composants
    Réponses: 4
    Dernier message: 18/08/2005, 17h59
  4. [JTable] rendre la sélection d'une ligne impossible
    Par norkius dans le forum Composants
    Réponses: 6
    Dernier message: 14/12/2004, 09h50
  5. Sélection d'une ligne dans un JTable
    Par dam21 dans le forum Composants
    Réponses: 3
    Dernier message: 28/07/2004, 12h57

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