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

AWT/Swing Java Discussion :

dimensionnement colonnes jdbTable


Sujet :

AWT/Swing Java

  1. #1
    Nouveau candidat au Club
    Inscrit en
    Mars 2007
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Mars 2007
    Messages : 1
    Par défaut dimensionnement colonnes jdbTable
    bonjour à tous,
    je me demande Comment faire pour dimensionner les colonnes de la JdbTable à la taille du contenu.


    Est-on obliger de faire ca à la main? Si oui comment faut faire?



    Merci de votre aide

  2. #2
    Membre Expert
    Avatar de natha
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    2 346
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Janvier 2006
    Messages : 2 346
    Par défaut
    Une petite méthode utilitaire que j'ai trouvée sur le net et un peu retouchée qui redimensionne les colonnes selon leur contenu.

    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
    	/**
             * Size automaticaly all cells by taking the max width and the max height
             * found in the content of the JTable.
             * 
             * @param table The table to set cell sizes too
             * @see #autoSizeCells(JTable, boolean, boolean)
             */
    	public static void autoSizeCells(JTable table) {
    		autoSizeCells(table, true, true);
    	}
     
    	/**
             * Size automaticaly all cells by taking the max width and/or the max height
             * found in the content of the JTable.
             * 
             * @param table The table to set cell sizes too
             * @param rwidth Resize the width of the columns (yes if true)
             * @param rheight Resize the height of the columns (yes if true)
             */
    	public static void autoSizeCells(JTable table, boolean rwidth, boolean rheight) {
    		if (!rwidth && !rheight) {
    			return; // Should resize at least one of these properties
    		}
    		JTableHeader header = table.getTableHeader();
    		TableCellRenderer defaultHeaderRenderer = null;
     
    		if (header != null) {
    			defaultHeaderRenderer = header.getDefaultRenderer();
    		}
     
    		TableColumnModel columns = table.getColumnModel();
    		TableModel data = table.getModel();
     
    		int marginWidth = columns.getColumnMargin(); // only JDK1.3
    		int marginHeight = table.getRowMargin();
     
    		int rowCount = data.getRowCount();
     
    		int totalWidth = 0;
     
    		for (int i = columns.getColumnCount() - 1; i >= 0; --i) {
    			TableColumn column = columns.getColumn(i);
     
    			int columnIndex = column.getModelIndex();
     
    			int width = -1;
    			int height = -1;
     
    			TableCellRenderer h = column.getHeaderRenderer();
     
    			if (h == null) {
    				h = defaultHeaderRenderer;
    			}
    			if (h != null) { // Not explicitly impossible
    				Component c = h.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, i);
    				width = c.getPreferredSize().width;
    				height = c.getPreferredSize().height;
    			}
     
    			for (int row = rowCount - 1; row >= 0; --row) {
    				TableCellRenderer r = table.getCellRenderer(row, i);
    				Component c = r.getTableCellRendererComponent(table, data.getValueAt(row, columnIndex), false, false, row, i);
    				if (c != null) {
    					width = Math.max(width, c.getPreferredSize().width);
    					height = Math.max(height, c.getPreferredSize().height);
    				}
    			}
     
    			if (rwidth && width >= 0) {
    				column.setPreferredWidth(width + marginWidth * 2);
    			}
    			if (rheight && height >= 0) {
    				table.setRowHeight(height + marginHeight * 2);
    			}
     
    			totalWidth += column.getPreferredWidth();
    		}
    	}

Discussions similaires

  1. [Débutant] [uitable] Dimensionnement colonnes
    Par nilsmatlab dans le forum Interfaces Graphiques
    Réponses: 2
    Dernier message: 08/07/2011, 15h29
  2. [Swing]JTable- Dimensionner les colonnes
    Par loutfi dans le forum Composants
    Réponses: 12
    Dernier message: 01/07/2011, 08h50
  3. [FPDF] Dimensionner colonnes PDF
    Par beegees dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 29/03/2009, 10h25
  4. Personnaliser une colonne d'un jdbTable
    Par baya1 dans le forum JBuilder
    Réponses: 1
    Dernier message: 03/04/2007, 19h57
  5. [HTML] Dimensionnement des colonnes d'un tableau
    Par cchampion2fr dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 09/11/2006, 16h04

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