0

Is there any good way to change a JFrame opacity real time. right now i need to restart the window to get the opacity

        if (Variables.LoggerOpacity){
        if (AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) {
              AWTUtilities.setWindowOpaque(Frame, true);
              AWTUtilities.setWindowOpacity(Frame, 0.60f);
        }
    }

When i use

AWTUtilities.setWindowOpacity(Frame, 0.60f);

On a button JCheckBox i won't change the opacity. Q: How can i change the opacity realtime?

kleopatra
  • 51,061
  • 28
  • 99
  • 211
jeroen
  • 41
  • 2
  • 4
  • No, That is a good idea. I will try that! – jeroen Nov 11 '11 at 10:21
  • The problem i then get is wih the opacity code: AWTUtilities.setWindowOpacity(Frame, 0.60f); You need to give the frame. the problem is i can't find a way to put the frame there. i made Frame static and used ClassName.Frame, but that doesn't work. Any idea's how to fix this? – jeroen Nov 11 '11 at 10:30
  • please learn java naming conventions and stick to them – kleopatra Nov 11 '11 at 11:25
  • 1
    something is wrong with the code you are not showing, time for a sscce – kleopatra Nov 11 '11 at 11:26

2 Answers2

0

Even if you have set the JFrame to static, you should be able to reference it if your opacity method is within the same class, if it isn't - create a getter method to reference your JFrame and pass that to your function. Here's an example program that executes and the opacity works fine:

public class JFrameOpacityExample extends JFrame {

private static JFrame myFrame;
private static boolean loggerOpacity;
private static JButton button;

public static void main(String[] args) {

    myFrame = new JFrame("Test Frame");
    myFrame.setSize(400, 400);
    myFrame.setVisible(true);

    JPanel panel = new JPanel();

    button = new JButton("Press me");
    button.setBounds(100, 100, 50, 50);
    button.setVisible(true);
    panel.add(button);
    myFrame.add(panel);

    loggerOpacity = true;

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Object src = evt.getSource();
            if (src == button && loggerOpacity) {
                AWTUtilities.setWindowOpacity(myFrame, 0.40f);
            }
        }
    });
  }
}
Deco
  • 3,261
  • 17
  • 25
  • Right now i have this: 'code' Variables.LoggerOpacity = true; repaint(); 'code' But That doesn't work? I have the opacity method in the class where i make the frame. does repaint(); refresh the whole frame? – jeroen Nov 11 '11 at 10:34
0

Add the following command to a frame's constructor. The name of the frame in this example is MyFrame.

    jCheckBox1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            AWTUtilities.setWindowOpacity(MyFrame.this, 0.2f);
        }
    });
wannik
  • 12,212
  • 11
  • 46
  • 58