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

Interfaces Graphiques en Java Discussion :

Comment utiliser TableCellRenderer


Sujet :

Interfaces Graphiques en Java

  1. #1
    Membre averti
    Avatar de omar344
    Homme Profil pro
    Développeur Java
    Inscrit en
    Juin 2007
    Messages
    287
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2007
    Messages : 287
    Points : 301
    Points
    301
    Par défaut Comment utiliser TableCellRenderer
    Salut,
    j'ai une JTable avec des cellules où le texte est trop long, pour adapter ce texte à la largeur de la cellule avec d'éventuels retours à la ligne j'ai trouvé qu'il faut utiliser un TableCellRenderer, j'en ai copier un 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
    85
    86
    87
    88
    89
     
    import java.awt.Component;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.font.FontRenderContext;
    import java.awt.font.LineBreakMeasurer;
    import java.awt.font.TextLayout;
    import java.text.AttributedCharacterIterator;
    import java.text.AttributedString;
    import java.text.BreakIterator;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
     
    public class MultilineTableCell    implements TableCellRenderer {
     
        class CellArea extends DefaultTableCellRenderer {
     
            /**
             *
             */
            private static final long serialVersionUID = 1L;
            private String text;
            protected int rowIndex;
            protected int columnIndex;
            protected JTable table;
            protected Font font;
            private int paragraphStart, paragraphEnd;
            private LineBreakMeasurer lineMeasurer;
     
            public CellArea(String s, JTable tab, int row, int column, boolean isSelected) {
                text = s;
                rowIndex = row;
                columnIndex = column;
                table = tab;
                font = table.getFont();
                if (isSelected) {
                    setForeground(table.getSelectionForeground());
                    setBackground(table.getSelectionBackground());
                }
            }
     
            @Override
            public void paintComponent(Graphics gr) {
                super.paintComponent(gr);
                if (text != null && !text.isEmpty()) {
                    Graphics2D g = (Graphics2D) gr;
                    if (lineMeasurer == null) {
                        AttributedCharacterIterator paragraph = new AttributedString(text).getIterator();
                        paragraphStart = paragraph.getBeginIndex();
                        paragraphEnd = paragraph.getEndIndex();
                        FontRenderContext frc = g.getFontRenderContext();
                        lineMeasurer = new LineBreakMeasurer(paragraph, BreakIterator.getWordInstance(), frc);
                    }
                    float breakWidth = (float) table.getColumnModel().getColumn(columnIndex).getWidth();
                    float drawPosY = 0;
                    // Set position to the index of the first character in the paragraph.
                    lineMeasurer.setPosition(paragraphStart);
                    // Get lines until the entire paragraph has been displayed.
                    while (lineMeasurer.getPosition() < paragraphEnd) {
                        // Retrieve next layout. A cleverer program would also cache
                        // these layouts until the component is re-sized.
                        TextLayout layout = lineMeasurer.nextLayout(breakWidth);
                        // Compute pen x position. If the paragraph is right-to-left we
                        // will align the TextLayouts to the right edge of the panel.
                        // Note: this won't occur for the English text in this sample.
                        // Note: drawPosX is always where the LEFT of the text is placed.
                        float drawPosX = layout.isLeftToRight()
                                ? 0 : breakWidth - layout.getAdvance();
                        // Move y-coordinate by the ascent of the layout.
                        drawPosY += layout.getAscent();
                        // Draw the TextLayout at (drawPosX, drawPosY).
                        layout.draw(g, drawPosX, drawPosY);
                        // Move y-coordinate in preparation for next layout.
                        drawPosY += layout.getDescent() + layout.getLeading();
                    }
                    table.setRowHeight(rowIndex, (int) drawPosY);
                }
            }
        }
     
        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            CellArea area = new CellArea(value.toString(), table, row, column, isSelected);
            return area;
        }
    }
    Ma question est comment utiliser ce TableCellRenderer sur ma JTable?

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

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    Dans ce cas, autant suivre un tutoriel sur l'utilisation de la JTable à l'usage des débutants. Ca te permettra d'éviter un paquet d'erreurs.

    http://baptiste-wicht.developpez.com.../swing/jtable/
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

Discussions similaires

  1. Réponses: 4
    Dernier message: 24/02/2009, 12h06
  2. Comment utiliser un cache ?
    Par TOM-Z dans le forum XMLRAD
    Réponses: 4
    Dernier message: 14/03/2003, 09h55
  3. comment utiliser actionscript ?
    Par webs dans le forum Flash
    Réponses: 3
    Dernier message: 09/02/2003, 23h11
  4. Comment utiliser OUT ?
    Par Bouziane Abderraouf dans le forum CORBA
    Réponses: 3
    Dernier message: 20/07/2002, 09h35
  5. Réponses: 5
    Dernier message: 11/06/2002, 15h21

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