I want to make JButton with a transparent background icon, which can response to three kind of mode and change its corresponding icon:
- rollover
- pressed
- when become normal mode
I have already set these icons, but when I rollover become black and black by every time, and also when I press the button, the background become very ugly (draw some text or other buttons background etc).
If you know about this problem please tell me what is wrong here. For more details I uploaded some pictures:
when pressed or rollover (printed JRadioButton in background)
when pressed or rollover (print another JButton's in background)
I just want that the background icon be normal when no rollover or pressed, and become light blue when rollover and become full blue when pressed, I have already set these icons, I am using Intellij IDE GUI form designer.
EDITED Added Code:
public class TransparentTest extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
TransparentTest frame = new TransparentTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TransparentTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//----------------------------------------------------------
JLayeredPane layeredPane = new JLayeredPane();
contentPane.add(layeredPane, BorderLayout.CENTER);
//----------------------------------------------------------
JPanel mailJPanel = new JPanel();
mailJPanel.setBackground(Color.RED);
mailJPanel.setBounds(0, 0, 422, 243);
layeredPane.add(mailJPanel);
//----------------------------------------------------------
JPanel bottomJPanel = new JPanel();
//panel_1.setOpaque(false);
bottomJPanel.setBounds(0, 195, 422, 48);
layeredPane.setLayer(bottomJPanel, 1);
layeredPane.add(bottomJPanel);
// set background
bottomJPanel.setBackground(Color.BLUE);
Color color = bottomJPanel.getBackground();
// set transparency
bottomJPanel.setBackground(new Color(color.getRed(), color.getGreen(), color.getBlue(), 64));
// set layout
FlowLayout fl_bottomJPanel = new FlowLayout();
fl_bottomJPanel.setHgap(0);
fl_bottomJPanel.setVgap(0);
bottomJPanel.setLayout(fl_bottomJPanel);
//----------------------------------------------------------
JButton button = new JButton("");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setPressedIcon(new ImageIcon(TransparentTest.class.getResource("/start-3.png")));
button.setRolloverIcon(new ImageIcon(TransparentTest.class.getResource("/start-2.png")));
button.setHorizontalTextPosition(SwingConstants.CENTER);
//button.setOpaque(false);
button.setContentAreaFilled(false);
button.setBorderPainted(false);
button.setFocusPainted(false);
button.setIcon(new ImageIcon(TransparentTest.class.getResource("/start-1.png")));
// add to bottom panel
bottomJPanel.add(button);
}
}