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 : ajout ligne


Sujet :

Composants Java

  1. #1
    Nouveau membre du Club
    Inscrit en
    Septembre 2009
    Messages
    51
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 51
    Points : 34
    Points
    34
    Par défaut JTable : ajout ligne
    Bonjour,

    j'ai une jtable dans un jscrollpane avec un model qui comporte une méthode addrow().

    l'ajout de la ligne se fait bien dans la jtable. je "repaint" le jframe pour rafraichir.
    le problème est que la scroll barre du jscrollpane ne se réactualise pas.
    j'ai bien ma nouvelle ligne qui s'ajoute en fin de tableau, mais, je ne peux pas l'afficher en déscendant le scroll vertical.
    ma ligne s'affiche si je réduit ma jframe.

    avez-vous une idée ?

  2. #2
    Membre actif Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Points : 275
    Points
    275
    Par défaut
    C'est probablement le TableModel qui est mal implémenté.

  3. #3
    Nouveau membre du Club
    Inscrit en
    Septembre 2009
    Messages
    51
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 51
    Points : 34
    Points
    34
    Par défaut Mon model
    Ci-dessous mon 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
     
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
     
    import javax.swing.event.TableModelListener;
    import javax.swing.table.TableModel;
     
     
    public class ClientsModelTable implements TableModel{
     
     
    	List<ClientObject> mesClients = new ArrayList<ClientObject>();
    	List<String> columnName = new ArrayList<String>();
    	String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/MaBase.accdb";
    	String user = "";
    	String password = "";
     
    	public ClientsModelTable(){
     
    		columnName.add("CustomerID");
    		columnName.add("CompanyName");
    		columnName.add("ContactName");
    		columnName.add("ContactTitle");
    		columnName.add("Address");
    		columnName.add("City");
    		columnName.add("Region");
    		columnName.add("PostalCode");
    		columnName.add("Country");
    		columnName.add("Phone");
    		columnName.add("Fax");
     
    		Connection connection = null;
    		try {
    			connection = DriverManager.getConnection(url, user, password);
    			Statement stmt = connection.createStatement();
    			String sql = "SELECT * FROM Customers";
    			ResultSet result = stmt.executeQuery(sql);
    			while (result.next()){
    				mesClients.add(new ClientObject(result.getString("CustomerId"), result.getString("CompanyName"),
    						result.getString("ContactName"), result.getString("ContactTitle"), result.getString("Address"),
    						result.getString("City"), result.getString("Region"), result.getString("PostalCode"),
    						result.getString("Country"),result.getString("Phone"), result.getString("Fax")));
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		finally{
    			try {
    				connection.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
     
     
    	@Override
    	public void addTableModelListener(TableModelListener l) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public Class<?> getColumnClass(int columnIndex) {
     
    		return String.class;
    	}
     
    	@Override
    	public int getColumnCount() {
     
    		return columnName.size();
    	}
     
    	@Override
    	public String getColumnName(int columnIndex) {
    		return columnName.get(columnIndex);
    	}
     
    	@Override
    	public int getRowCount() {
    		return mesClients.size();
    	}
     
    	@Override
    	public Object getValueAt(int rowIndex, int columnIndex) {
     
    		switch (columnIndex){
    		case 0 : return mesClients.get(rowIndex).getCustomerId();
    		case 1 : return mesClients.get(rowIndex).getCompanyName();
    		case 2 : return mesClients.get(rowIndex).getContactName();
    		case 3 : return mesClients.get(rowIndex).getContactTitles();
    		case 4 : return mesClients.get(rowIndex).getAddress();
    		case 5 : return mesClients.get(rowIndex).getCity();
    		case 6 : return mesClients.get(rowIndex).getRegion();
    		case 7 : return mesClients.get(rowIndex).getPostalCode();
    		case 8 : return mesClients.get(rowIndex).getCountry();
    		case 9 : return mesClients.get(rowIndex).getPhone();
    		case 10 : return mesClients.get(rowIndex).getFax();
    		}
    		return null;
    	}
     
    	@Override
    	public boolean isCellEditable(int rowIndex, int columnIndex) {
    		return false;
    	}
     
    	@Override
    	public void removeTableModelListener(TableModelListener l) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
     
    		ClientObject newCustomer = mesClients.get(rowIndex);
    		switch (columnIndex){
    		case 0 : newCustomer.setCustomerId((String) aValue);
    		break;
    		case 1 : newCustomer.setCompanyName((String) aValue);
    		break;
    		case 2 : newCustomer.setContactName((String) aValue);
    		break;
    		case 3 : newCustomer.setContactTitles((String) aValue);
    		break;
    		case 4 : newCustomer.setAddress((String) aValue);
    		break;
    		case 5 : newCustomer.setCity((String) aValue);
    		break;
    		case 6 : newCustomer.setRegion((String) aValue);
    		break;
    		case 7 : newCustomer.setPostalCode((String) aValue);
    		break;
    		case 8 : newCustomer.setCountry((String) aValue);
    		break;
    		case 9 : newCustomer.setPhone((String) aValue);
    		break;
    		case 10 : newCustomer.setFax((String) aValue);
    		break;
    		}
     
    	}
     
    	public void addRow (){
     
    		mesClients.add(new ClientObject("rien", "rien", "rien", 
    					"rien", "rien", "rien", "rien",
    				"rien", "rien", "rien", "rien"));
     
    	}
     
     
    	public void removeRow(int rowIndex){
     
    		mesClients.remove(rowIndex);
    	}
    }

  4. #4
    Membre actif Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Points : 275
    Points
    275
    Par défaut
    Essaie ceci:
    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
    import java.sql.*;
    import java.util.*;
    import javax.swing.table.*;
     
    public class ClientsModelTable extends AbstractTableModel {
     
        private List<ClientObject> mesClients = new ArrayList<ClientObject>();
        private List<String> columnName = new ArrayList<String>();
        private String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/MaBase.mdb";
        private String user = "";
        private String password = "";
     
        public ClientsModelTable() {
            columnName.add("CustomerID");
            columnName.add("CompanyName");
            columnName.add("ContactName");
            columnName.add("ContactTitle");
            columnName.add("Address");
            columnName.add("City");
            columnName.add("Region");
            columnName.add("PostalCode");
            columnName.add("Country");
            columnName.add("Phone");
            columnName.add("Fax");
            Connection connection = null;
            try {
                connection = DriverManager.getConnection(url, user, password);
                Statement stmt = connection.createStatement();
                String sql = "SELECT * FROM Customers";
                ResultSet result = stmt.executeQuery(sql);
                while (result.next()) {
                    mesClients.add(new ClientObject(result.getString("CustomerId"), result.getString("CompanyName"),
                            result.getString("ContactName"), result.getString("ContactTitle"), result.getString("Address"),
                            result.getString("City"), result.getString("Region"), result.getString("PostalCode"),
                            result.getString("Country"), result.getString("Phone"), result.getString("Fax")));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
     
        @Override
        public Class<?> getColumnClass(final int columnIndex) {
            return String.class;
        }
     
        @Override
        public int getColumnCount() {
            return columnName.size();
        }
     
        @Override
        public String getColumnName(final int columnIndex) {
            return columnName.get(columnIndex);
        }
     
        @Override
        public int getRowCount() {
            return mesClients.size();
        }
     
        @Override
        public Object getValueAt(final int rowIndex, final int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return mesClients.get(rowIndex).getCustomerId();
                case 1:
                    return mesClients.get(rowIndex).getCompanyName();
                case 2:
                    return mesClients.get(rowIndex).getContactName();
                case 3:
                    return mesClients.get(rowIndex).getContactTitles();
                case 4:
                    return mesClients.get(rowIndex).getAddress();
                case 5:
                    return mesClients.get(rowIndex).getCity();
                case 6:
                    return mesClients.get(rowIndex).getRegion();
                case 7:
                    return mesClients.get(rowIndex).getPostalCode();
                case 8:
                    return mesClients.get(rowIndex).getCountry();
                case 9:
                    return mesClients.get(rowIndex).getPhone();
                case 10:
                    return mesClients.get(rowIndex).getFax();
            }
            return null;
        }
     
        @Override
        public boolean isCellEditable(final int rowIndex, final int columnIndex) {
            return false;
        }
     
        @Override
        public void setValueAt(final Object aValue, final int rowIndex, final int columnIndex) {
            ClientObject newCustomer = mesClients.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    newCustomer.setCustomerId((String) aValue);
                    break;
                case 1:
                    newCustomer.setCompanyName((String) aValue);
                    break;
                case 2:
                    newCustomer.setContactName((String) aValue);
                    break;
                case 3:
                    newCustomer.setContactTitles((String) aValue);
                    break;
                case 4:
                    newCustomer.setAddress((String) aValue);
                    break;
                case 5:
                    newCustomer.setCity((String) aValue);
                    break;
                case 6:
                    newCustomer.setRegion((String) aValue);
                    break;
                case 7:
                    newCustomer.setPostalCode((String) aValue);
                    break;
                case 8:
                    newCustomer.setCountry((String) aValue);
                    break;
                case 9:
                    newCustomer.setPhone((String) aValue);
                    break;
                case 10:
                    newCustomer.setFax((String) aValue);
                    break;
            }
        }
     
        public void addRow() {
            int rowIndex = mesClients.size();
            mesClients.add(new ClientObject("rien" + rowIndex, "rien", "rien",
                    "rien", "rien", "rien", "rien",
                    "rien", "rien", "rien", "rien"));
            fireTableRowsInserted(rowIndex, rowIndex);
        }
     
        public void removeRow(final int rowIndex) {
            mesClients.remove(rowIndex);
            fireTableRowsDeleted(rowIndex, rowIndex);
        }
    }

  5. #5
    Nouveau membre du Club
    Inscrit en
    Septembre 2009
    Messages
    51
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 51
    Points : 34
    Points
    34
    Par défaut Mon model
    la méthode n'est pas connue dans l'implémentation de TableModel

    La méthode fireTableRowsDeleted(int, int) est indéfinie pour le type ClientsModelSelection

    en fait j'ai trouvé une solution en actualisant le JScrollPane par :

    ClientsApplication.scroll.setViewportView(table);

    mais cela ne me semble pas très propre.

  6. #6
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Le fireTableXXX est une méthode héritée de la classe AbstractTableModel et non de l'interface TableModel.

    De fait je te conseille très fortement de remplacer ton implements TableModel par un extends AbstractTableModel pour profiter de tous les goodies déjà fournis. De plus cette classe est amplement utilisée par la JTable.

  7. #7
    Nouveau membre du Club
    Inscrit en
    Septembre 2009
    Messages
    51
    Détails du profil
    Informations forums :
    Inscription : Septembre 2009
    Messages : 51
    Points : 34
    Points
    34
    Par défaut Jtable
    Ok Merci

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

Discussions similaires

  1. [JTable] Ajouter ligne avec moins de colonnes
    Par encours dans le forum Composants
    Réponses: 1
    Dernier message: 27/12/2011, 09h57
  2. [JTable] Ajout ligne
    Par rems033 dans le forum Composants
    Réponses: 1
    Dernier message: 09/08/2007, 10h41
  3. Réponses: 15
    Dernier message: 09/06/2006, 12h13
  4. [debutant] [JTable] ajout d'une ligne
    Par lanfeustdetroll dans le forum Composants
    Réponses: 1
    Dernier message: 29/06/2005, 18h19
  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