Arc
Arc

Reputation: 449

Java JFrame setting custom opacity

How do I change the opacity of a jframe from user input, lets say from a spinner?

int opacity = 7;
double dOpacity = opacity/10;
String sOpacity = Double.toString(dOpacity)+"f";
this.setOpacity(sOpacity); //???

The above code produces a bunch of errors. Is there anyway to get a opacity number, lets say 6, and then convert it to a value that 'this.setOpacity(sOpacity);' will accept?

errors :

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: GuiClock.Main.setOpacity
    at GuiClock.Main.cinitComponents(Main.java:97)
    at GuiClock.Main.<init>(Main.java:16)
    at GuiClock.Main$6.run(Main.java:168)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Upvotes: 2

Views: 181

Answers (3)

Zied R.
Zied R.

Reputation: 4964

in your code : dOpacity= 0.0

    int opacity = 7;
    float dOpacity = (float)opacity/10; //casting to float , here dOpacity=0.7
    String sOpacity = Float.toString(dOpacity)+"f"; // sOpacity="0.7f"
    this.setOpacity(Float.parseFloat(sOpacity)); //setOpacity(0.7f);

Upvotes: 2

Hugo Sousa
Hugo Sousa

Reputation: 1926

The setOpacity method uses a float as parameter. You're passing a String.

Try this.setOpacity(Float.parseFloat(sOpacity));

Upvotes: 1

Taerus
Taerus

Reputation: 475

You are inserting a String as parameter to the setOpacity method, which only takes doubles.

Upvotes: 0

Related Questions