I want to make transparency for jPanel do it with

jPanel1.setBackground(new Color(255, 255, 255, 30));

Everything is good, the panel is semi transparent, the inscriptions on the panel are also transparent, but when I start to make setText on jLabel, bugs come out, the inscription on the inscription is superimposed and the transparency of the inscription begins to turn white.

On the Internet I read about jPanel1.setOpaque(false);

I did, everything works as it should, but the panel has become completely transparent. How to fix it?

JDK1.7-1.8 NetBeans javax.swing

    2 answers 2

    If you override the paintComponent(Graphics g) method of JPanel , set a breakpoint and go deep into steps, you will see that the panel simply fills its entire space with the color getBackground() if isOpaque() returns true . If the panel is transparent, then its paintComponent does nothing. And without setting transparency, Swing will not redraw the components that lie below the panel, which will cause various interesting effects.

    You need to redefine the paintComponent in a translucent panel (create a full-fledged class-descendant, or anonymous class) and make the fill with the desired transparency.

     public static void main(String[] args) { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); // Полосатая фоновая панель final JPanel contentPane = new JPanel( new BorderLayout() ) { @Override public void paintComponent(Graphics g) { super.paintComponent( g ); g.setColor( Color.RED ); for ( int i = 0; i < getWidth(); i += 20 ) { g.fillRect( i, 0, 10, getHeight() ); } } }; contentPane.setBackground( Color.GREEN ); frame.setContentPane( contentPane ); contentPane.add( new JLabel( "NORTH"), BorderLayout.NORTH ); contentPane.add( new JLabel( "SOUTH"), BorderLayout.SOUTH ); contentPane.add( new JLabel( "EAST"), BorderLayout.EAST ); contentPane.add( new JLabel( "WEST"), BorderLayout.WEST ); //Полупрозрачная панель JPanel otherPanel = new JPanel( new BorderLayout() ) { @Override public void paintComponent( Graphics g ) { super.paintComponent( g ); // Apply our own painting effect Graphics2D g2d = (Graphics2D) g.create(); // 50% transparent Alpha g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); g2d.setColor(getBackground()); g2d.fillRect( 0, 0, getWidth(), getHeight() ); g2d.dispose(); } }; otherPanel.setOpaque( false ); contentPane.add( otherPanel, BorderLayout.CENTER ); // обратите внимание, что альфа-канал цвета - 255 (непрозрачный) // т.к. используется композит в paintComponent // можно сделать полупрозрачный цвет и убрать композит otherPanel.setBackground( new Color( 0, 0, 250, 255 ) ); final JLabel label = new JLabel( "LABEL" ); otherPanel.add( label ); label.setForeground( new Color( 200, 200, 0, 200 ) ); frame.pack(); frame.setVisible( true ); // изменяем метку Timer t = new Timer(); t.scheduleAtFixedRate( new TimerTask() { @Override public void run() { SwingUtilities.invokeLater( () -> { label.setText( label.getText() + " + "); }); } }, 1000, 1000 ); } 

    The redraw code is picked up in this answer .

    • And it can be shorter, eyes run up among jLabels and others - Denis Kotlyarov
    • Thank you class! It turned out to be even simpler, there are things in netbeans, Tipo code instead of creating, put yours in there, everything was changed, added a couple of actions and the panel was semi transparent and buggy, thanks a lot - Denis Kotlyarov

    Well, actually setOpaque(false) makes the component completely transparent.

    UPD. Try playing by changing the text color directly to jLabel or with the setForeground() method instead of the jPanel color.

    • So how to fix this "bugs come out, the inscription on the inscription is superimposed and the transparency of the inscription begins to turn white" when I do on the inscription setText - Denis Kotlyarov
    • When the 2 translucent components overlap, they change color, it is inevitable. - anber
    • @DenisKotlyarov updated answer - anber
    • It’s not the text color that changes, but the background is jLabel, its boundaries are Denis Kotlyarov
    • jLabel setForeground doesn't help either, but somehow strange setOpaque works perfectly well, but that doesn't want to happen - Denis Kotlyarov