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 :

Définir la hauteur des cellules JTable


Sujet :

Composants Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Architecte de système d’information
    Inscrit en
    Janvier 2007
    Messages
    439
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Architecte de système d’information

    Informations forums :
    Inscription : Janvier 2007
    Messages : 439
    Par défaut Définir la hauteur des cellules JTable
    Bonjour , pour le moment j'arrive à imprimer un Jtable avec la fonction print.
    Mon tableau occupe 80% de la largeur de la feuille ce qui est correct.
    Par contre c'est très petit en terme de hauteur de ligne.

    J'aimerais agrandir ceci :


    Après la méthode initComponents généré par Netbeans j'écris :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    lstJrnTablex.getColumn(0).setWidth(500);
    lstJrnTablex.getColumn(1).setWidth(500);
    lstJrnTablex.getColumn(2).setWidth(500);
    lstJrnTablex.setDefaultRenderer(Object.class,  new HeaderCellRenderer());
    
    lstJrnTablex.setRowHeight(100);
    J'ai beau changé la valeur du setRowHeight mais rien ne change à l'impression ni l'affichage .

    Pourtant sur un projet plus simple, ça fonctionne :
    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
     
    import java.awt.BorderLayout;
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.print.PrinterException;
    import java.text.MessageFormat;
     
    import javax.swing.JButton;
    import javax.swing.JTable;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
     
    public class Main{
     
      private static JTable table;
      private static String h;
     
      public static void main(String args[]) {
     
        JFrame frame = new JFrame("Table Printing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
     
     
        int rows = 10;
        int cols = 5;
        table = new JTable(rows, cols);
        table.setRowHeight(30);
     
         table.getColumn(0).setWidth(300);
         table.getColumn(1).setWidth(300);
         table.getColumn(2).setWidth(300);
     
     
     
     
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);
        JButton button = new JButton("Print");
        ActionListener printAction = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              MessageFormat headerFormat = new MessageFormat("Page {0}");
              MessageFormat footerFormat = new MessageFormat("- {0} -");
              table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat);
            } catch (PrinterException pe) {
              System.err.println("Error printing: " + pe.getMessage());
            }
          }
        };
        button.addActionListener(printAction);
        frame.add(button, BorderLayout.SOUTH);
        frame.setSize(300, 150);
        frame.setVisible(true);
      }
     
     
        // Set the height of all rows to 32 pixels high,
        // regardless if any heights were assigned to particular rows
     
        // the height of the 1st row is set to 32 pixels high
     
        // Returns the preferred height of a row.
        // The result is equal to the tallest cell in the row.
        public int getPreferredRowHeight(JTable table, int rowIndex, int margin) {
            // Get the current default height for all rows
            int height = table.getRowHeight();
     
            // Determine highest cell in the row
            for (int c=0; c<table.getColumnCount(); c++) {
                TableCellRenderer renderer = table.getCellRenderer(rowIndex, c);
                Component comp = table.prepareRenderer(renderer, rowIndex, c);
                int h = comp.getPreferredSize().height + 2*margin;
                height = Math.max(height, h);
            }
            return height;
        }
     
        // The height of each row is set to the preferred height of the
        // tallest cell in that row.
        public void packRows(JTable table, int margin) {
            packRows(table, 0, table.getRowCount(), margin);
        }
     
        // For each row >= start and < end, the height of a
        // row is set to the preferred height of the tallest cell
        // in that row.
        public void packRows(JTable table, int start, int end, int margin) {
            for (int r=0; r<table.getRowCount(); r++) {
                // Get the preferred height
                int h = getPreferredRowHeight(table, r, margin);
     
                // Now set the row height using the preferred height
                if (table.getRowHeight(r) != h) {
                    table.setRowHeight(r, h);
                }
            }
        }
     
    }
    Merci de votre aide.

  2. #2
    Membre expérimenté 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
    Par défaut
    Citation Envoyé par cotede2 Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    lstJrnTablex.setRowHeight(100);
    Est-ce que lstJrnTablex est bien la table que tu imprime?

Discussions similaires

  1. Tabulate et hauteur des cellules
    Par Rose1366 dans le forum ODS et reporting
    Réponses: 0
    Dernier message: 23/09/2011, 17h30
  2. Redimensionner la hauteur des cellules d'un gridview
    Par lazins dans le forum ASP.NET
    Réponses: 0
    Dernier message: 04/12/2008, 16h20
  3. [Pilotage d'Excel]Comment définir les formats des cellules
    Par Masmeta dans le forum Windows Forms
    Réponses: 1
    Dernier message: 03/04/2008, 18h34
  4. Réponses: 14
    Dernier message: 01/08/2006, 23h20
  5. [VBA-E]définir l'ensemble des cellules d'une feuille?
    Par yaya54 dans le forum Macros et VBA Excel
    Réponses: 15
    Dernier message: 02/03/2006, 08h46

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