Twinuts
Messages postés
5375
Date d'inscription
dimanche 4 mai 2003
Statut
Modérateur
Dernière intervention
14 juin 2023
111
29 oct. 2006 à 11:57
Salut,
il te suffit simplement de redef le model graphique de ton bouton affin de preserver les couleurs et l'etat graphiqe(rollover, armed, etc...)
exemple
public class ButtonUI extends BasicButtonUI {
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
String text = layout(b, SwingUtilities2.getFontMetrics(b, g), b
.getWidth(), b.getHeight());
g.setColor(Color.BLACK);
g.fillRect(0, 0, b.getSize().width, b.getSize().height);
clearTextShiftOffset();
if (model.isArmed() && model.isPressed()) {
paintButtonPressed(g, b);
} else if(!model.isArmed() && !model.isPressed()){
//le bouton n'est pas pressé et n'a pas la souris dessus
}else if(!model.isArmed() && model.isPressed()){
paintButtonPressedNotArmed(g, b);
}
// Paint the Icon
if (b.getIcon() != null)
paintIcon(g, c, iconRect);
//paint the text
if (text != null && !text.equals("")) {
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null)
v.paint(g, textRect);
else
paintText(g, b, textRect, text);
}
if (b.isFocusPainted() && b.hasFocus()) {
// paint UI specific focus
paintFocus(g, b, viewRect, textRect, iconRect);
}
}
protected void paintButtonPressedNotArmed(Graphics g, AbstractButton b) {
//le bouton est pressé mais la souris n'est pas dessus
}
protected void paintButtonPressed(Graphics g, AbstractButton b) {
//le bouton est pressé a toi de voir comment tu veux le dessiner
}
protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) {
super.paintIcon(g, c, iconRect);
}
protected void paintText(Graphics g, JComponent c, Rectangle textRect,
String text) {
Graphics2D g2 = (Graphics2D) g;
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(c, g2);
int mnemonicIndex = b.getDisplayedMnemonicIndex();
/* Draw the Text */
if (model.isEnabled()) {
/*** paint the text normally */
g2.setColor(b.getForeground());
SwingUtilities2.drawStringUnderlineCharAt(c, g2, text,
mnemonicIndex, textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
} else {
/*** paint the text disabled ***/
g2.setColor(b.getBackground().brighter());
SwingUtilities2.drawStringUnderlineCharAt(c, g2, text,
mnemonicIndex, textRect.x, textRect.y + fm.getAscent());
g2.setColor(b.getBackground().darker());
SwingUtilities2.drawStringUnderlineCharAt(c, g2, text,
mnemonicIndex, textRect.x - 1, textRect.y + fm.getAscent()
- 1);
}
}
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect,
String text) {
paintText(g, (JComponent) b, textRect, text);
}
private String layout(AbstractButton b, FontMetrics fm, int width, int height) {
Insets i = b.getInsets();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = width - (i.right + viewRect.x);
viewRect.height = height - (i.bottom + viewRect.y);
textRect.x textRect.y textRect.width = textRect.height = 0; iconRect.x iconRect.y iconRect.width = iconRect.height = 0;
// layout the text and icon
return SwingUtilities.layoutCompoundLabel(b, fm, b.getText(), b
.getIcon(), b.getVerticalAlignment(), b
.getHorizontalAlignment(), b.getVerticalTextPosition(), b
.getHorizontalTextPosition(), viewRect, iconRect, textRect, b
.getText() == null ? 0 : b.getIconTextGap());
}
}
bref il te faut bidouiller à ta sauce mais tu devrais pouvoir t'en sortir avec.... point de vu utilisation c'est simple
JButton button = new JButton();
button.setUI(new ButtonUI());
------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."
WORA