I'm working on a custom Jbutton who has to show a status next to it. Code is kind of dirty and there's still work on the architecture, right now it's only a protoype using a JPanel with a delegate JButton. I'm experiencing strange paint artefacts when hovering other JFrame components, and i've no clue why.
Here's the code of the component with a test program:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class CustomStateButton extends JPanel {
JButton button;
boolean status;
public CustomStateButton(String text, ImageIcon icon, boolean status) {
super();
this.status = status;
this.button = new JButton(text,icon);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add (this.button);
}
public CustomStateButton(String text, boolean status) {
super();
this.status = status;
this.button = new JButton(text);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add(this.button);
}
public CustomStateButton( ImageIcon icon, boolean status) {
super();
this.status = status;
this.button = new JButton(icon);
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add (Box.createHorizontalStrut(15));
add (this.button);
}
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(Color.WHITE);
if (this.status)
{
g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
getHeight()-10), Color.GREEN.darker()));
}
else{
g2.setPaint(new GradientPaint(new Point(0, (getHeight()-10)/2), Color.WHITE, new Point(0,
getHeight()-10), Color.BLACK));
}
g2.fillOval(2, (getHeight()-10)/2, 10, 10);
g2.setPaint(Color.BLACK);
g2.drawOval(2, (getHeight()-10)/2, 10, 10);
}
/**
* @param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setSize(new Dimension(400,300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
final CustomStateButton button = new CustomStateButton("test", false);
final JRadioButton radioButton = new JRadioButton();
radioButton.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
button.status= radioButton.isSelected();
button.repaint();
}
});
frame.getContentPane().add (button);
frame.getContentPane().add (new JButton("blabla"));
frame.getContentPane().add (radioButton);
frame.setVisible(true);
}
}
Any suggestion is welcome :-)
When overriding the paintComponent
method of a JComponent
, the first call should usually be a call to the super.paintComponent(g)
method:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
}
Otherwise, rendering artafacts may appear, which result from thing remaining on the screen that hat been painted in a previous rendering pass.
By default, the super.paintComponent(g)
method will cause opaque components to be cleared with its background color, by eventually delegating to this method of the ComponentUI
:
public void update(Graphics g, JComponent c) {
if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);
}