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 fusionner cellule


Sujet :

Composants Java

  1. #1
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 66
    Points : 50
    Points
    50
    Par défaut Jtable fusionner cellule
    Bonjour,

    Je souhaiterai dépoussiérer un vieux sujet concernant la fusion des cellules dans un Jtable.

    J'ai vu qu'il y a eu des discussions mais cela date un peu et les liens proposait ne marche plus.

    Est ce que quelqu'un connait la méthode pour fusionner des cellules dans un Jtable?

    Merci d'avance!

  2. #2
    Modérateur
    Avatar de joel.drigo
    Homme Profil pro
    Ingénieur R&D - Développeur Java
    Inscrit en
    Septembre 2009
    Messages
    12 430
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur R&D - Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2009
    Messages : 12 430
    Points : 29 131
    Points
    29 131
    Billets dans le blog
    2
    Par défaut
    Salut,

    Nativement, une JTable ne permet pas de fusionner de cellules. Il faut le réaliser soit-même et on trouve effectivement de nombreux exemples sur Internet, et ils restent valables. On peut trouver des composants qui implémentent cette notion dans certaines bibliothèques également.
    L'expression "ça marche pas" ne veut rien dire. Indiquez l'erreur, et/ou les comportements attendus et obtenus, et donnez un Exemple Complet Minimal qui permet de reproduire le problème.
    La plupart des réponses à vos questions sont déjà dans les FAQs ou les Tutoriels, ou peut-être dans une autre discussion : utilisez la recherche interne.
    Des questions sur Java : consultez le Forum Java. Des questions sur l'EDI Eclipse ou la plateforme Eclipse RCP : consultez le Forum Eclipse.
    Une question correctement posée et rédigée et vous aurez plus de chances de réponses adaptées et rapides.
    N'oubliez pas de mettre vos extraits de code entre balises CODE (Voir Mode d'emploi de l'éditeur de messages).
    Nouveau sur le forum ? Consultez Les Règles du Club.

  3. #3
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 66
    Points : 50
    Points
    50
    Par défaut
    Merci d'avoir répondu si rapidement!

  4. #4
    Membre du Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    66
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2011
    Messages : 66
    Points : 50
    Points
    50
    Par défaut
    Voici une solution que j'ai trouvé :

    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
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    591
    592
    593
    594
    595
    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    684
    685
    686
    687
    688
    689
    690
    691
    692
    693
    694
    695
    696
    697
    698
    699
    700
    701
    702
    703
    704
    705
    706
    707
    708
    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Enumeration;
    import java.util.Vector;
     
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSeparator;
    import javax.swing.JTable;
    import javax.swing.ListSelectionModel;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.TableModelEvent;
    import javax.swing.plaf.basic.BasicTableUI;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import javax.swing.table.TableModel;
     
     
    /**
    * @version 1.0 11/26/98
    */
    public class MultiSpanCellTableExample extends JFrame {
     
    MultiSpanCellTableExample() {
     super( "Multi-Span Cell Example" );
     
     AttributiveCellTableModel ml = new AttributiveCellTableModel(10,6);
     
     final CellSpan cellAtt =(CellSpan)ml.getCellAttribute();
     final MultiSpanCellTable table = new MultiSpanCellTable( ml );
     JScrollPane scroll = new JScrollPane( table );
     
     JButton b_one   = new JButton("Combine");
     b_one.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
    int[] columns = table.getSelectedColumns();
    int[] rows    = table.getSelectedRows();
    cellAtt.combine(rows,columns);
    table.clearSelection();
    table.revalidate();
    table.repaint();
       }
     });
     JButton b_split = new JButton("Split");
     b_split.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
    int column = table.getSelectedColumn();
    int row    = table.getSelectedRow();
    cellAtt.split(row,column);
    table.clearSelection();
    table.revalidate();
    table.repaint();
       }
     });
     JPanel p_buttons = new JPanel();
     p_buttons.setLayout(new GridLayout(2,1));
     p_buttons.add(b_one);
     p_buttons.add(b_split);
     
     Box box = new Box(BoxLayout.X_AXIS);
     box.add(scroll);
     box.add(new JSeparator(SwingConstants.HORIZONTAL));
     box.add(p_buttons);
     getContentPane().add( box );
     setSize( 400, 200 );
     setVisible(true);
    }
     
    public static void main(String[] args) {
     MultiSpanCellTableExample frame = new MultiSpanCellTableExample();
     frame.addWindowListener( new WindowAdapter() {
       public void windowClosing( WindowEvent e ) {
         System.exit(0);
       }
     });
    }
    }
     
    class AttributiveCellTableModel extends DefaultTableModel {
     
    protected CellAttribute cellAtt;
     
    public AttributiveCellTableModel() {
     this((Vector)null, 0);
    }
    public AttributiveCellTableModel(int numRows, int numColumns) {
     Vector names = new Vector(numColumns);
     names.setSize(numColumns);
     setColumnIdentifiers(names);
     dataVector = new Vector();
     setNumRows(numRows);
     cellAtt = new DefaultCellAttribute(numRows,numColumns);
    }
    public AttributiveCellTableModel(Vector columnNames, int numRows) {
    	  columnIdentifiers = columnNames;
     dataVector = new Vector();
     setNumRows(numRows);
     cellAtt = new DefaultCellAttribute(numRows,columnNames.size());
    }
    public AttributiveCellTableModel(Object[] columnNames, int numRows) {
     this(convertToVector(columnNames), numRows);
    }  
    public AttributiveCellTableModel(Vector data, Vector columnNames) {
     setDataVector(data, columnNames);
    }
    public AttributiveCellTableModel(Object[][] data, Object[] columnNames) {
     setDataVector(data, columnNames);
    }
     
     
    public void setDataVector(Vector newData, Vector columnNames) {
     if (newData == null)
       throw new IllegalArgumentException("setDataVector() - Null parameter");
     dataVector = new Vector(0);
     columnIdentifiers = columnNames;
     dataVector = newData;
     
     //
     cellAtt = new DefaultCellAttribute(dataVector.size(),
                                        columnIdentifiers.size());
     
     newRowsAdded(new TableModelEvent(this, 0, getRowCount()-1,
      TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
    }
     
    public void addColumn(Object columnName, Vector columnData) {
     if (columnName == null)
       throw new IllegalArgumentException("addColumn() - null parameter");
     columnIdentifiers.addElement(columnName);
     int index = 0;
     Enumeration eeration = dataVector.elements();
     while (eeration.hasMoreElements()) {
       Object value;
       if ((columnData != null) && (index < columnData.size()))
     value = columnData.elementAt(index);
       else
    value = null;
       ((Vector)eeration.nextElement()).addElement(value);
       index++;
     }
     
     //
     cellAtt.addColumn();
     
     fireTableStructureChanged();
    }
     
    public void addRow(Vector rowData) {
     Vector newData = null;
     if (rowData == null) {
       newData = new Vector(getColumnCount());
     }
     else {
       rowData.setSize(getColumnCount());
     }
     dataVector.addElement(newData);
     
     //
     cellAtt.addRow();
     
     newRowsAdded(new TableModelEvent(this, getRowCount()-1, getRowCount()-1,
        TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
    }
     
    public void insertRow(int row, Vector rowData) {
     if (rowData == null) {
       rowData = new Vector(getColumnCount());
     }
     else {
       rowData.setSize(getColumnCount());
     }
     
     dataVector.insertElementAt(rowData, row);
     
     //
     cellAtt.insertRow(row);
     
     newRowsAdded(new TableModelEvent(this, row, row,
        TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
    }
     
    public CellAttribute getCellAttribute() {
     return cellAtt;
    }
     
    public void setCellAttribute(CellAttribute newCellAtt) {
     int numColumns = getColumnCount();
     int numRows    = getRowCount();
     if ((newCellAtt.getSize().width  != numColumns) ||
         (newCellAtt.getSize().height != numRows)) {
       newCellAtt.setSize(new Dimension(numRows, numColumns));
     }
     cellAtt = newCellAtt;
     fireTableDataChanged();
    }
     
     
     
    }
     
     
    /**
    * @version 1.0 11/22/98
    */
     
    class DefaultCellAttribute 
    // implements CellAttribute ,CellSpan  {
       implements CellAttribute ,CellSpan ,ColoredCell ,CellFont {
     
    //
    // !!!! CAUTION !!!!!
    // these values must be synchronized to Table data
    //
    protected int rowSize;
    protected int columnSize;
    protected int[][][] span;                   // CellSpan
    protected Color[][] foreground;             // ColoredCell
    protected Color[][] background;             //
    protected Font[][]  font;                   // CellFont
     
    public DefaultCellAttribute() {
     this(1,1);
    }
     
    public DefaultCellAttribute(int numRows, int numColumns) {
     setSize(new Dimension(numColumns, numRows));
    }
     
    protected void initValue() {
     for(int i=0; i<span.length;i++) {
       for(int j=0; j<span[i].length; j++) {
    span[i][j][CellSpan.COLUMN] = 1;
    span[i][j][CellSpan.ROW]    = 1;
       }
     }
    }
     
     
    //
    // CellSpan
    //
    public int[] getSpan(int row, int column) {
     if (isOutOfBounds(row, column)) {
       int[] ret_code = {1,1};
       return ret_code;
     }
     return span[row][column];
    }
     
    public void setSpan(int[] span, int row, int column) {
     if (isOutOfBounds(row, column)) return;
     this.span[row][column] = span;
    }
     
    public boolean isVisible(int row, int column) {
     if (isOutOfBounds(row, column)) return false;
     if ((span[row][column][CellSpan.COLUMN] < 1)
       ||(span[row][column][CellSpan.ROW]    < 1)) return false;
     return true;
    }
     
    public void combine(int[] rows, int[] columns) {
     if (isOutOfBounds(rows, columns)) return;
     int    rowSpan  = rows.length;
     int columnSpan  = columns.length;
     int startRow    = rows[0];
     int startColumn = columns[0];
     for (int i=0;i<rowSpan;i++) {
       for (int j=0;j<columnSpan;j++) {
    if ((span[startRow +i][startColumn +j][CellSpan.COLUMN] != 1)
     ||(span[startRow +i][startColumn +j][CellSpan.ROW]    != 1)) {
     //System.out.println("can't combine");
     return ;
    }
       }
     }
     for (int i=0,ii=0;i<rowSpan;i++,ii--) {
       for (int j=0,jj=0;j<columnSpan;j++,jj--) {
    span[startRow +i][startColumn +j][CellSpan.COLUMN] = jj;
    span[startRow +i][startColumn +j][CellSpan.ROW]    = ii;
    //System.out.println("r " +ii +"  c " +jj);
       }
     }
     span[startRow][startColumn][CellSpan.COLUMN] = columnSpan;
     span[startRow][startColumn][CellSpan.ROW]    =    rowSpan;
     
    }
     
    public void split(int row, int column) {
     if (isOutOfBounds(row, column)) return;
     int columnSpan = span[row][column][CellSpan.COLUMN];
     int    rowSpan = span[row][column][CellSpan.ROW];
     for (int i=0;i<rowSpan;i++) {
       for (int j=0;j<columnSpan;j++) {
    span[row +i][column +j][CellSpan.COLUMN] = 1;
    span[row +i][column +j][CellSpan.ROW]    = 1;
       }
     }
    }
     
     
    //
    // ColoredCell
    //
    public Color getForeground(int row, int column) {
     if (isOutOfBounds(row, column)) return null;
     return foreground[row][column];
    }
    public void setForeground(Color color, int row, int column) {
     if (isOutOfBounds(row, column)) return;
     foreground[row][column] = color;
    }
    public void setForeground(Color color, int[] rows, int[] columns) {
     if (isOutOfBounds(rows, columns)) return;
     setValues(foreground, color, rows, columns);
    }
    public Color getBackground(int row, int column) {
     if (isOutOfBounds(row, column)) return null;
     return background[row][column];
    }
    public void setBackground(Color color, int row, int column) {
     if (isOutOfBounds(row, column)) return;
     background[row][column] = color;
    }
    public void setBackground(Color color, int[] rows, int[] columns) {
     if (isOutOfBounds(rows, columns)) return;
     setValues(background, color, rows, columns);
    }
    //
     
     
    //
    // CellFont
    //
    public Font getFont(int row, int column) {
     if (isOutOfBounds(row, column)) return null;
     return font[row][column];
    }
    public void setFont(Font font, int row, int column) {
     if (isOutOfBounds(row, column)) return;
     this.font[row][column] = font;
    }
    public void setFont(Font font, int[] rows, int[] columns) {
     if (isOutOfBounds(rows, columns)) return;
     setValues(this.font, font, rows, columns);
    }
    //
     
     
    //
    // CellAttribute
    //
    public void addColumn() {
     int[][][] oldSpan = span;
     int numRows    = oldSpan.length;
     int numColumns = oldSpan[0].length;
     span = new int[numRows][numColumns + 1][2];
     System.arraycopy(oldSpan,0,span,0,numRows);
     for (int i=0;i<numRows;i++) {
       span[i][numColumns][CellSpan.COLUMN] = 1;
       span[i][numColumns][CellSpan.ROW]    = 1;
     }
    }
     
    public void addRow() {
     int[][][] oldSpan = span;
     int numRows    = oldSpan.length;
     int numColumns = oldSpan[0].length;
     span = new int[numRows + 1][numColumns][2];
     System.arraycopy(oldSpan,0,span,0,numRows);
     for (int i=0;i<numColumns;i++) {
       span[numRows][i][CellSpan.COLUMN] = 1;
       span[numRows][i][CellSpan.ROW]    = 1;
     }
    }
     
    public void insertRow(int row) {
     int[][][] oldSpan = span;
     int numRows    = oldSpan.length;
     int numColumns = oldSpan[0].length;
     span = new int[numRows + 1][numColumns][2];
     if (0 < row) {
       System.arraycopy(oldSpan,0,span,0,row-1);
     }
     System.arraycopy(oldSpan,0,span,row,numRows - row);
     for (int i=0;i<numColumns;i++) {
       span[row][i][CellSpan.COLUMN] = 1;
       span[row][i][CellSpan.ROW]    = 1;
     }
    }
     
    public Dimension getSize() {
     return new Dimension(rowSize, columnSize);
    }
     
    public void setSize(Dimension size) {
     columnSize = size.width;
     rowSize    = size.height;
     span = new int[rowSize][columnSize][2];   // 2: COLUMN,ROW
     foreground = new Color[rowSize][columnSize];
     background = new Color[rowSize][columnSize];
     font = new Font[rowSize][columnSize];
     initValue();
    }
     
    /*
    public void changeAttribute(int row, int column, Object command) {
    }
     
    public void changeAttribute(int[] rows, int[] columns, Object command) {
    }
    */
     
     
     
     
    protected boolean isOutOfBounds(int row, int column) {
     if ((row    < 0)||(rowSize    <= row)
       ||(column < 0)||(columnSize <= column)) {
       return true;
     }
     return false;
    }
     
    protected boolean isOutOfBounds(int[] rows, int[] columns) {
     for (int i=0;i<rows.length;i++) {
       if ((rows[i] < 0)||(rowSize <= rows[i])) return true;
     }
     for (int i=0;i<columns.length;i++) {
       if ((columns[i] < 0)||(columnSize <= columns[i])) return true;
     }
     return false;
    }
     
    protected void setValues(Object[][] target, Object value,
                            int[] rows, int[] columns) {
     for (int i=0;i<rows.length;i++) {
       int row = rows[i];
       for (int j=0;j<columns.length;j++) {
    int column = columns[j];
    target[row][column] = value;
       }
     }
    }
    }
    /*
    * (swing1.1beta3)
    * 
    */
     
    /**
    * @version 1.0 11/22/98
    */
     
    interface CellAttribute {
     
    public void addColumn();
     
    public void addRow();
     
    public void insertRow(int row);
     
    public Dimension getSize();
     
    public void setSize(Dimension size);
     
     
    }
    /*
    * (swing1.1beta3)
    * 
    */
     
    /**
    * @version 1.0 11/22/98
    */
     
    interface CellSpan {
    public final int ROW    = 0;
    public final int COLUMN = 1;
     
    public int[] getSpan(int row, int column);
    public void setSpan(int[] span, int row, int column);
     
    public boolean isVisible(int row, int column);
     
    public void combine(int[] rows, int[] columns);
    public void split(int row, int column);
     
    }
    /*
    * (swing1.1beta3)
    * 
    */
     
    /**
    * @version 1.0 11/26/98
    */
     
    class MultiSpanCellTable extends JTable {
     
    public MultiSpanCellTable(TableModel model) {
     super(model);
     setUI(new MultiSpanCellTableUI());
     getTableHeader().setReorderingAllowed(false);
     setCellSelectionEnabled(true);
     setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
    }
     
     
    public Rectangle getCellRect(int row, int column, boolean includeSpacing) {
     Rectangle sRect = super.getCellRect(row,column,includeSpacing);
     if ((row <0) || (column<0) ||
         (getRowCount() <= row) || (getColumnCount() <= column)) {
         return sRect;
     }
     CellSpan cellAtt = (CellSpan)((AttributiveCellTableModel)getModel()).getCellAttribute();
     if (! cellAtt.isVisible(row,column)) {
       int temp_row    = row;
       int temp_column = column;
       row    += cellAtt.getSpan(temp_row,temp_column)[CellSpan.ROW];
       column += cellAtt.getSpan(temp_row,temp_column)[CellSpan.COLUMN];      
     }
     int[] n = cellAtt.getSpan(row,column);
     
     int index = 0;
     int columnMargin = getColumnModel().getColumnMargin();
     Rectangle cellFrame = new Rectangle();
     int aCellHeight = rowHeight + rowMargin;
     cellFrame.y = row * aCellHeight;
     cellFrame.height = n[CellSpan.ROW] * aCellHeight;
     
     Enumeration eeration = getColumnModel().getColumns();
     while (eeration.hasMoreElements()) {
       TableColumn aColumn = (TableColumn)eeration.nextElement();
       cellFrame.width = aColumn.getWidth() + columnMargin;
       if (index == column) break;
       cellFrame.x += cellFrame.width;
       index++;
     }
     for (int i=0;i< n[CellSpan.COLUMN]-1;i++) {
       TableColumn aColumn = (TableColumn)eeration.nextElement();
       cellFrame.width += aColumn.getWidth() + columnMargin;
     }
     
     
     
     if (!includeSpacing) {
       Dimension spacing = getIntercellSpacing();
       cellFrame.setBounds(cellFrame.x +      spacing.width/2,
         cellFrame.y +      spacing.height/2,
         cellFrame.width -  spacing.width,
         cellFrame.height - spacing.height);
     }
     return cellFrame;
    }
     
     
    private int[] rowColumnAtPoint(Point point) {
     int[] retValue = {-1,-1};
     int row = point.y / (rowHeight + rowMargin);
     if ((row <0)||(getRowCount() <= row)) return retValue;
     int column = getColumnModel().getColumnIndexAtX(point.x);
     
     CellSpan cellAtt = (CellSpan)((AttributiveCellTableModel)getModel()).getCellAttribute();
     
     if (cellAtt.isVisible(row,column)) {
       retValue[CellSpan.COLUMN] = column;
       retValue[CellSpan.ROW   ] = row;
       return retValue;
     }
     retValue[CellSpan.COLUMN] = column + cellAtt.getSpan(row,column)[CellSpan.COLUMN];
     retValue[CellSpan.ROW   ] = row + cellAtt.getSpan(row,column)[CellSpan.ROW];
     return retValue;
    }
     
     
    public int rowAtPoint(Point point) {
     return rowColumnAtPoint(point)[CellSpan.ROW];
    }
    public int columnAtPoint(Point point) {
     return rowColumnAtPoint(point)[CellSpan.COLUMN];
    }
     
     
     
    public void columnSelectionChanged(ListSelectionEvent e) {
     repaint();
    }
     
    public void valueChanged(ListSelectionEvent e) {
     int firstIndex = e.getFirstIndex();
     int  lastIndex = e.getLastIndex();
     if (firstIndex == -1 && lastIndex == -1) { // Selection cleared.
       repaint();
     }
     Rectangle dirtyRegion = getCellRect(firstIndex, 0, false);
     int numCoumns = getColumnCount();
     int index = firstIndex;
     for (int i=0;i<numCoumns;i++) {
       dirtyRegion.add(getCellRect(index, i, false));
     }
     index = lastIndex;
     for (int i=0;i<numCoumns;i++) {
       dirtyRegion.add(getCellRect(index, i, false));
     }
     repaint(dirtyRegion.x, dirtyRegion.y, dirtyRegion.width, dirtyRegion.height);
    }
     
    }
     
     
    /**
    * @version 1.0 11/26/98
    */
     
    class MultiSpanCellTableUI extends BasicTableUI {
     
    public void paint(Graphics g, JComponent c) {
     Rectangle oldClipBounds = g.getClipBounds();
     Rectangle clipBounds    = new Rectangle(oldClipBounds);
     int tableWidth   = table.getColumnModel().getTotalColumnWidth();
     clipBounds.width = Math.min(clipBounds.width, tableWidth);
     g.setClip(clipBounds);
     
     int firstIndex = table.rowAtPoint(new Point(0, clipBounds.y));
     int  lastIndex = table.getRowCount()-1;
     
     Rectangle rowRect = new Rectangle(0,0,
       tableWidth, table.getRowHeight() + table.getRowMargin());
     rowRect.y = firstIndex*rowRect.height;
     
     for (int index = firstIndex; index <= lastIndex; index++) {
       if (rowRect.intersects(clipBounds)) {
         //System.out.println();                  // debug
         //System.out.print("" + index +": ");    // row
    paintRow(g, index);
       }
       rowRect.y += rowRect.height;
     }
     g.setClip(oldClipBounds);
    }
     
    private void paintRow(Graphics g, int row) {
     Rectangle rect = g.getClipBounds();
     boolean drawn  = false;
     
     AttributiveCellTableModel tableModel = (AttributiveCellTableModel)table.getModel();
     CellSpan cellAtt = (CellSpan)tableModel.getCellAttribute();
     int numColumns = table.getColumnCount();
     
     for (int column = 0; column < numColumns; column++) {
       Rectangle cellRect = table.getCellRect(row,column,true);
       int cellRow,cellColumn;
       if (cellAtt.isVisible(row,column)) {
    cellRow    = row;
    cellColumn = column;
     //  System.out.print("   "+column+" ");  // debug
       } else {
    cellRow    = row + cellAtt.getSpan(row,column)[CellSpan.ROW];
    cellColumn = column + cellAtt.getSpan(row,column)[CellSpan.COLUMN];
     //  System.out.print("  ("+column+")");  // debug
       }
       if (cellRect.intersects(rect)) {
    drawn = true;
    paintCell(g, cellRect, cellRow, cellColumn);
       } else {
    if (drawn) break;
       } 
     }
     
    }
     
    private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
     int spacingHeight = table.getRowMargin();
     int spacingWidth  = table.getColumnModel().getColumnMargin();
     
     Color c = g.getColor();
     g.setColor(table.getGridColor());
     g.drawRect(cellRect.x,cellRect.y,cellRect.width-1,cellRect.height-1);
     g.setColor(c);
     
     cellRect.setBounds(cellRect.x + spacingWidth/2, cellRect.y + spacingHeight/2,
            cellRect.width - spacingWidth, cellRect.height - spacingHeight);
     
     if (table.isEditing() && table.getEditingRow()==row &&
    table.getEditingColumn()==column) {
       Component component = table.getEditorComponent();
       component.setBounds(cellRect);
       component.validate();
     }
     else {
       TableCellRenderer renderer = table.getCellRenderer(row, column);
       Component component = table.prepareRenderer(renderer, row, column);
     
       if (component.getParent() == null) {
    rendererPane.add(component);
       }
       rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
           cellRect.width, cellRect.height, true);
     }
    }    
    }
     
     
    interface CellFont {
     
     public Font getFont(int row, int column);
     public void setFont(Font font, int row, int column);
     public void setFont(Font font, int[] rows, int[] columns);
     
     
    }
     
     
    interface ColoredCell {
     
     public Color getForeground(int row, int column);
     public void setForeground(Color color, int row, int column);
     public void setForeground(Color color, int[] rows, int[] columns);
     
     public Color getBackground(int row, int column);
     public void setBackground(Color color, int row, int column);
     public void setBackground(Color color, int[] rows, int[] columns);
     
    }

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

Discussions similaires

  1. Fractionner/Fusionner cellule d' en-tête JTable
    Par ddb_am dans le forum Composants
    Réponses: 1
    Dernier message: 21/03/2014, 23h20
  2. Fusionner cellule JTable
    Par timtima dans le forum Composants
    Réponses: 0
    Dernier message: 21/11/2008, 18h44
  3. [JTable]Fusionner des cellules
    Par bassim dans le forum Composants
    Réponses: 2
    Dernier message: 11/07/2007, 19h24
  4. [JTable] Fusionner des cellules
    Par Rimk78 dans le forum Composants
    Réponses: 2
    Dernier message: 13/05/2005, 15h53
  5. [Debutant] [JTable] Fusionner deux cellules
    Par devil26 dans le forum Composants
    Réponses: 4
    Dernier message: 03/05/2005, 13h39

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