Modifier le texte d'un JButton dans une JTable
Bonjour,
C'est la suite du post : Redimensionnement des colonnes par l'utilisateur
Je me suis inspirée du lien donné dans le post.
J'ai modifié le mouseDragged de la classe TableColumnResizer ainsi :
Code:
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
| public void mouseDragged(MouseEvent e){
int mouseX = e.getX();
//Colonne redimentionnée
TableColumn resizingColumn = table.getTableHeader().getResizingColumn();
boolean headerLeftToRight = table.getTableHeader().getComponentOrientation().isLeftToRight();
if(resizingColumn != null){
//int oldWidth = resizingColumn.getWidth();
int oldWidth = resizingColumn.getPreferredWidth();
int newWidth;
int diff;
if(headerLeftToRight){
newWidth = mouseX - mouseXOffset;
diff = newWidth - oldWidth;
} else{
newWidth = mouseXOffset - mouseX;
diff = oldWidth - newWidth ;
}
if (diff != 0)
{
resizingColumn.setPreferredWidth(newWidth);
resizingColumn.setWidth(newWidth);
JButton buttonClick = (JButton) resizingColumn.getCellEditor().getCellEditorValue();
int nrbreDepart = Integer.parseInt(buttonClick.getText());
System.out.println("Nbre d : " + nrbreDepart);
int nrbreArrive = (int)(nrbreDepart*newWidth/oldWidth);
JButton buttonArrive = (JButton) resizingColumn.getCellRenderer();
buttonArrive.setText(String.valueOf(nrbreArrive));
System.out.println("Nbre a : " + buttonArrive.getText());
}
}
} |
Et voici mes CellEditor et Redender :
Code:
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
| private class TableCellRendererCouleur extends JButtonCouleur implements TableCellRenderer {
/**
* La JTable de gestion de temps
*/
private static final long serialVersionUID = 1L;
private CompoEffet compoEffet;
public TableCellRendererCouleur() {
super (Color.red);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
compoEffet = (CompoEffet)value;
setColor(new Color(Integer.parseInt(compoEffet.getRouge()),Integer.parseInt(compoEffet.getVert()),Integer.parseInt(compoEffet.getBleu()),255));
setText(compoEffet.getDuree());
return this;
}
}
private class DefaultCellEditorCouleur extends DefaultCellEditor {
/**
* Representation de la JTable de gestion des temps dans la JTable
*/
private static final long serialVersionUID = 1L;
protected JButtonCouleur button;
private boolean isPushed;
private CompoEffet compoEffet;
public DefaultCellEditorCouleur(JCheckBox checkBox) {
super(checkBox);
button = new JButtonCouleur(Color.red);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
compoEffet = (CompoEffet)value;
button.setColor(new Color(Integer.parseInt(compoEffet.getRouge()),Integer.parseInt(compoEffet.getVert()),Integer.parseInt(compoEffet.getBleu()),255));
button.setText(compoEffet.getDuree());
isPushed = true;
return button;
}
public Object getCellEditorValue() {
isPushed = false;
return button;
}
} |
Je recupere bien le text de mon CellEditor mais je n'arrive pas à visualiser la modification du text de mon CellRenderer alors qu'aparemment il est bien modifié.