-
Bug
-
Resolution: Duplicate
-
P2
-
None
-
1.3.0
-
x86
-
windows_nt
Name: krT82822 Date: 02/19/2000
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)
There is a problem with JToggleButton, or more precisely with its
BasicToggleButtonUI class.
While a selected JToggleButton is painted properly, adhering to the setOpaque()
and setContentAreaFilled() settings, an *unselected* JToggleButton ignores
them! (it comes out opaque)
This is a new problem in JDK 1.3, this works fine in JDK 1.2.2.
Here is quick sample code to reproduce the problem, works as both Applet and
Application:
// ------ START OF SOURCE CODE ------------------
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class Bug extends java.applet.Applet {
public Bug() {
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(Color.cyan);
JToggleButton button1 = new JToggleButton("Selected");
button1.setSelected(true);
button1.setOpaque(false);
button1.setContentAreaFilled(false);
JToggleButton button2 = new JToggleButton("Not Selected");
button2.setSelected(false);
button2.setOpaque(false);
button2.setContentAreaFilled(false);
panel.add(button1);
panel.add(button2);
add(panel);
}
public static void main(String[] args) {
Frame frame = new Frame();
frame.add(new Bug());
frame.setSize(200, 200);
frame.setVisible(true);
}
}
// -------- END OF SOURCE CODE --------------
Upon inspecting the difference in source for BasicToggleButtonUI between JDK
1.2.2 and JDK 1.3, I discovered that a completely new else-branch has been
inserted at line 86, that always does a fillRect() on the entire JToggleButton
if its not selected, ignoring any opaque setting:
// ------ START OF PROBLEM CODE (BasicToggleButtonUI.java, line 84) ---------
if (model.isArmed() && model.isPressed() || model.isSelected()) {
paintButtonPressed(g,b);
} else { // ---- New in JDK 1.3 !!!
Insets insets = b.getInsets();
Insets margin = b.getMargin();
g.fillRect(insets.left - margin.left,
insets.top - margin.top,
size.width - (insets.left-margin.left) - (insets.right -
margin.right),
size.height - (insets.top-margin.top) - (insets.bottom -
margin.bottom));
}
// ------ END OF PROBLEM CODE-------------
This is not good. There should either be a new paintButtonUnPressed() method
(or named something similar), that can easily be over-ridden by a UI subclass,
or there is should be a conditional statement that checks isOpaque() and
isContentAreaFilled() before filling the area.
(Review ID: 101104)
======================================================================
- duplicates
-
JDK-4313754 JToggleButton's setOpaque() fails to show the background behind it
-
- Closed
-