I want to create a semi-transparent JPanel. I've done it by simply using RGBA value of color constructor but problem is when i m using event handling is not woking properly. My requirement is a semi transparent Jpanel when mouse enters it border of this panel became visible and if mouse exit the border shoud not visible. I have done this by following code but problem is its not working properly for transparent backgroud (RGBA) but it working fine for RGB color.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class MDCW extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MDCW frame = new MDCW();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MDCW() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1013, 551);
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 139, 139));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
final JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 0,50));
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
panel.setBorder(new LineBorder(new Color(255, 255, 255), 5));
}
@Override
public void mouseExited(MouseEvent e) {
panel.setBorder(null);
}
});
panel.setBounds(360, 155, 215, 215);
contentPane.add(panel);
final JPanel panel_1 = new JPanel();
panel_1.setBackground(new Color(0, 0, 0));
panel_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
panel_1.setBorder(new LineBorder(new Color(255, 255, 255), 5));
}
@Override
public void mouseExited(MouseEvent e) {
panel_1.setBorder(null);
}
});
panel_1.setBounds(84, 155, 215, 215);
contentPane.add(panel_1);
}
}
You can simply create your jPanel using drag and drop, as you always do and then for changing the panel's color and making it transparent or semi-transparent you can use this code: panel. setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));
JPanel when transparent PNG image is loaded, and set JPanel. setOpaque(false); it will use the image transparent method, else it will show not transparent picture.
The transparency can be set using the setBackground() method. Yes, you might have missed the java. awt. Color class' constructor Color(int r,int g,int b,int a); the last parameter here, does it all.
When using a JLabel, I can get it to be transparent by doing: label. setOpaque(true); label.
JPanel does not support semi-transparent backgrounds. There are two steps needed to take care of this problem:
First, to have any correctly-functioning transparency at all, you must setOpaque(false)
on the panel; otherwise you will have glitches, because an opaque panel is assumed to completely cover what is underneath its bounds.
However, when opaque
is false
, the panel also does not draw its background at all (!) so you will have to draw a background in paintComponent.
Here is a drop-in replacement class which will take care of both of these steps.
private class TransparentPanel extends JPanel {
{
setOpaque(false);
}
public void paintComponent(Graphics g) {
g.setColor(getBackground());
Rectangle r = g.getClipBounds();
g.fillRect(r.x, r.y, r.width, r.height);
super.paintComponent(g);
}
}
I’ve checked that it works in your program if I change the first panel creation to:
final JPanel panel = new TransparentPanel();
It's easy to do it like so:
// initialise JPanel
JPanel somePanel = new JPanel(new GridBagLayout());
somePanel.setBackground(new Color(0,0,0,x);
x in this case is the level of transparency you're looking for 0 being invisible, 100 being solid.
e.g:
somePanel.setBackground(new Color(0,0,0,55)
See: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With