JButton Problem
Here is an SSCCE which shows it:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass SSCCEextends JComponentimplements ActionListener{
public SSCCE(){
setLayout(null);
JButton j =new JButton("A Button");
j.setSize(300,50);
j.setLocation(30,50);
j.setBackground(new Color(0,0,0,0));
j.setForeground(Color.yellow);
//j.setBorderPainted(false);
//j.setFocusPainted(false);
//j.setRolloverEnabled(false);
add(j);
new Timer(30,this);
}
publicvoid actionPerformed(ActionEvent e){
repaint();
}
publicvoid paintComponent(Graphics g){
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getHeight());
}
publicstaticvoid main(String[] args){
JFrame frame =new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new SSCCE());
frame.pack();
frame.setSize(500,500);
frame.setVisible(true);
}
}
The button stays highlighted after you have pressed on it no matter what. Also, how can I change the color of the highlight for when the mouse is pressed down on the button?
[2409 byte] By [
Nethera] at [2007-11-26 17:09:40]

# 3
import java.awt.*;
import java.awt.image.*;
import javax.swing.border.*;
public class CentredBackgroundBorder implements Border {
private final BufferedImage image;
public CentredBackgroundBorder(BufferedImage image) {
this.image = image;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
int x0 = x + (width-image.getWidth())/2;
int y0 = y + (height-image.getHeight())/2;
g. drawImage(image, x0, y0, null);
}
public Insets getBorderInsets(Component c) {
return new Insets(0,0,0,0);
}
public boolean isBorderOpaque() {
return true;
}
}
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
public class Example {
public static void main(String[] args) throws IOException {
Color clear = new Color(0,0,0,0);
UIManager.put("Button.select", clear);
int r = 20, c = 12;
JPanel contentPane = new JPanel(new GridLayout(r,c));
contentPane.setBackground(Color.WHITE);
for(int i=0; i<r*c; ++i) {
JButton btn = new JButton(String.valueOf(i));
btn.setBackground(clear);
btn.setForeground(Color.BLUE);
btn.setContentAreaFilled(false);
contentPane.add(btn);
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(contentPane);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
URL url = new URL("https://duke.dev.java.net/images/godzilla/FlamingGodzillaSmall.png");
contentPane.setBorder(new CentredBackgroundBorder(ImageIO.read(url)));
}
}
>
# 6
> Note the magic incantation to get rid of the selection coloring of JButton:
Not sure what that does. I don't think you need that code or the setBackground(...). Using setContentAreaFilled(false) seems to be all I need (on JDK1.4.2 anyway).
This is from the API on AbstractButton:
If you wish to have a transparent button, such as an icon only button, for example, then you should set this to false. Do not call setOpaque(false).
# 9
> I don't think you need that code or the setBackground(...). Using
> setContentAreaFilled(false) seems to be all I need (on JDK1.4.2 anyway).
>
> This is from the API on AbstractButton: ...
You absolutely right. I has been using btn.setOpaque(false), which required me to setBackground and do that UIManager hack. With setContentAreaFilled(false), it looks like I don't need any of that other stuff. Thanks for pointing out the API comment.