3
loadingLab=new JLabel("The name is being saved..");
loadPanel.add(loadingLab);
submitBttn=new JButton("Submit");
submitBttn.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {  
        System.out.println("Submit Button Clicked!!");
        try {
            //something is wrong in here as it throws an exception
            //what is wrong?
            frame.setUndecorated(false);
            frame.setOpacity(0.55f);

            //when above both lines are commented, the code works fine
            //but doesnt have transparency  
            frame.add(loadPanel,BorderLayout.SOUTH);
            frame.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
});

I am trying to display transparent JFrame when "submit" button is clicked which displays panel with a JLabel... I have tried using setOpacity(0.55f), but it throws exception.. what am i doing wrong?

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82

2 Answers2

2

Unfortunately I think there's no way to keep the system window decoration, you will probably have to go with the default one. Since I'm not 100% sure if you want to toggle the opacity of the whole frame or just the frame's background, I've included both functions in my example. (mKorbels answer help you more if you don't want to have a decoration)

Code:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

public class TransparentExample extends JFrame {

    public TransparentExample() {

        super("TransparentExample");
        Color defaultBackground = getBackground();
        float defaultOpacity = getOpacity();

        JToggleButton button1 = new JToggleButton("Toggle background transparency");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (button1.isSelected()) {
                    setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(),
                            defaultBackground.getBlue(), 150));
                } else {
                    setBackground(defaultBackground);
                }
            }
        });

        JToggleButton button2 = new JToggleButton("Toggle opacity of whole frame");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                if (button2.isSelected()) {
                    setOpacity(0.55f);
                } else {
                    setOpacity(defaultOpacity);
                }
                setVisible(true);
            }
        });

        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button1);
        getContentPane().add(button2);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame.setDefaultLookAndFeelDecorated(true);
                TransparentExample frame = new TransparentExample();
                frame.setVisible(true);
            }
        });
    }

}

Picture of frame with no togglebutton selected: enter image description here

Picture of frame with the first togglebutton selected: enter image description here

Picture of frame with the second togglebutton selected: enter image description here

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • thank you very much... i was definately looking for second togglebutton (setting the opacity)..my another question is, how do u make it's panel opaque with background semi-transparent – Programmer007 Nov 06 '15 at 15:11
  • @Programmer007 Well, if you don't mind that the decoration isn't semi-transparent, you could stick with the first button and replace `setBackground(new Color(0, 0, 0, 0));` with `setBackground(new Color(defaultBackground.getRed(), defaultBackground.getGreen(), defaultBackground.getBlue(), 150));`. That would make the background semi-transparent, but every component (the buttons) fully visible (including an added panel, in your case. As long as you didn't call `setOpaque(false)` on it, it should work). – Lukas Rotter Nov 06 '15 at 15:18
  • the semi-transparent worked... will work on opaque panel now – Programmer007 Nov 06 '15 at 15:34
  • @Programmer007 So is this now the end result you wanted? I don't understand what you mean with *"will work on opaque panel now"*... Was that a question? If yes, I guess you're referring to the `loadPanel` in your code. Well... Yes, you can set the background to `new Color(0, 0, 0, 0)` for that, the background color will stay the same. (If that's what you were asking) – Lukas Rotter Nov 06 '15 at 16:05
  • i was asking, only when "submit " button is clicked , the frame turns semi-transparent and it displays panel (loadPanel-which is actually opaque) – Programmer007 Nov 07 '15 at 14:27
  • can somebody show me an example of how to set panel opaque with semi transparent frame in background.... – Programmer007 Nov 07 '15 at 14:45
  • @Programmer007 Combined with the function of my first button you can just add the panel, it will be opaque (frame background will still be semi-transparent. – Lukas Rotter Nov 07 '15 at 17:06
1

@Programmer007 wrote - the exception is " java.awt.IllegalComponentStateException: The frame is displayable."

  • please where I can't see any, for more info about the possible exceptions to read,

  • as mentioned no idea, everything is about your effort, transformed to the SSCCE / MCVE, short, runnable, compilable

.

import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GenericForm extends JDialog {

    private static final long serialVersionUID = 1L;
    private Timer timer;
    private JDialog dialog = new JDialog();
    private int count = 0;

    public GenericForm() {
        dialog.setSize(400, 300);
        dialog.setUndecorated(true);
        dialog.setOpacity(0.5f);
        dialog.setName("Toggling with opacity");
        dialog.getContentPane().setBackground(Color.RED);
        dialog.setLocation(150, 150);
        dialog.setVisible(true);
        timer = new javax.swing.Timer(1500, updateCol());
        timer.setRepeats(true);
        timer.start();
    }

    private Action updateCol() {
        return new AbstractAction("Hello World") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bol = dialog.getOpacity() < 0.55f;
                count += 1;
                if (count < 10) {
                    if (bol) {
                        dialog.setOpacity(1.0f);
                        dialog.getContentPane().setBackground(Color.WHITE);
                    } else {
                        dialog.setOpacity(0.5f);
                        dialog.getContentPane().setBackground(Color.RED);
                    }
                } else {
                    System.exit(0);
                }
            }
        };
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GenericForm();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319