JTable en java

Fermé
sicard_51 - 16 juil. 2002 à 10:53
 sicard_51 - 17 juil. 2002 à 13:47
Bonjour,

bon voila mon probleme, je manipule une JTable en java et je voudrais, que lorsque je rajoute une ligne à ma table, cette ligne puisse etre grisée ou non, cela dépendrait d'une condition. Je pense qu'il faut utiliser setBackground() mais comment l'appliquer à une ligne spécifique de la table et non à la table entière ??

Merci d'avance.
A voir également:

3 réponses

Tout d'bord, désolé pour ma remarque, sicard_51. Je pensais que c'était une autre personne que toi qui avait écrit "Re, bon allez encore...".

Chose promise, chose dûe, voilà le code:

1. Classe "main" qui permet de lancer l'application:
-----------------------------------------------------------

package CCM.tableTester;

/**
*
* @author HackTrack
* @version 1.0
*/
public class TableTesterLauncher {

public TableTesterLauncher() {
}

public static void main (String args[]) {
MyFrame view = new MyFrame();
view.pack();
view.setVisible(true);
}
}

2. Classe contenant la vue:
--------------------------------
package CCM.tableTester;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

/**
*
* @author HackTrack
* @version 1.0
*/
public class MyFrame extends JFrame{
private Container c;
private InsertTable table;


/** Creates new MyFrame */
public MyFrame() {
super("Test frame");
initialize();
}

private void initialize(){
c = getContentPane();
c.setSize(320,200);
c.setLayout(new BorderLayout());
String[][] data = {
{"10","Donnée test 1"},
{"23","Valeur 2"}
};
String[] columnNames = {"N°","Data"};
DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
TableColumn col_1 = new TableColumn();
col_1.setHeaderValue(columnNames[0]);
columnModel.addColumn(col_1);
TableColumn col_2 = new TableColumn();
col_2.setHeaderValue(columnNames[1]);
columnModel.addColumn(col_2);
DefaultTableModel tableModel = new DefaultTableModel(2,2);

table = new InsertTable(tableModel, columnModel, Color.red);
table.setRowSelectionAllowed(true);
c.add(new JScrollPane(table), BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
JButton addRowWithHighlight = new JButton("Ajouter une ligne avec surlignage");
addRowWithHighlight.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
table.addRow(InsertTable.HIGHLIGHT_TRUE);
c.validate();
}
});
bottomPanel.add(addRowWithHighlight);
JButton addRowWithoutHighlight = new JButton("Ajouter une ligne sans surlignage");
addRowWithoutHighlight.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
table.addRow(InsertTable.HIGHLIGHT_FALSE);
c.validate();
}
});
bottomPanel.add(addRowWithoutHighlight);
c.add(bottomPanel, BorderLayout.SOUTH);
c.validate();
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

3. Et enfin, le code tellement attendu:
--------------------------------------------
J'ai écrit une classe InsertTable qui éténd JTable et j'y ai ajouté une méthode "addRow(boolean)" qui prend un boolean en argument. Ce boolean permet de préciser si l'on veut que la nouvelle ligne créée soit surligné (InsertTable.HIGHLIGHT_TRUE) ou non ((InsertTable.HIGHLIGHT_FALSE).

package CCM.tableTester;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
*
* @author Hacktrack
* @version 1.0
*/
public class InsertTable extends JTable{
public static final boolean HIGHLIGHT_TRUE = true;
public static final boolean HIGHLIGHT_FALSE = false;
private TableModel tableModel;
private TableColumnModel columnModel;
private static final Color DEFAULT_HIGHLIGHT_COLOR = Color.white;
private Color userHighlightColor;
private Color highlightColor;

public InsertTable(TableModel tableModel, TableColumnModel columnModel, Color userHighlightColor) {
super(tableModel, columnModel);
this.userHighlightColor = userHighlightColor;
}


public void addRow(boolean highlight){
int newRowIndex = getModel().getRowCount() + 1;
this.setModel(new DefaultTableModel(newRowIndex,2));

if(highlight==HIGHLIGHT_TRUE){
highlightColor = userHighlightColor;
}else{
highlightColor = DEFAULT_HIGHLIGHT_COLOR;
}
setSelectionBackground(highlightColor);

selectRow(newRowIndex);

validate();
}


private void selectRow(int rowIndex){
ListSelectionModel lsm = getSelectionModel();
lsm.setSelectionInterval(rowIndex-1,rowIndex-1);
setSelectionModel(lsm);
}


;-)
HackTrack
3
Re,

bon allez un ch'tit effort !! :-)
Si c pas possible pour une ligne, doit bien y avoir un moyen pour changer la couleur du fond d'une cellule au moins dans une JTable ??
0
Pour donner une réponse comme cela, autant ne rien dire...
J'envoie la réponse demain

;-)
HackTrack
0
Excellent Hack,

je te tiens au courant pour savoir si j'arrice à l'appliquer à mon cas

merci encore
0