I'm making sorta of a system information project as a beginner. The project has a GUI clock and another GUI which reports: CPU, memory usage, ect. As a feature, I want to the user to be able to change the opacity of all of these JFrames at once.
The program is organized where when you open the program, a main menu pops up, then the user can open the clock or CPU monitor. On the main menu there is a combo box which reads : 100%, 90%, ect down to 10%. I added an action event to the combo box which then sets the opacity of the main menu to the corresponding output of the combo box.
Heres where the problem is. The above paragraph only works if the clock GUI is not open. If I try to change the opacity of the main menu and GUI clock, the clock freezes and the opacity does not change on any of the GUIs.
public void opacityComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
int x = opacityComboBox.getSelectedIndex();
switch(x) {
case 0:
break;
case 1:
opacity=10; // in the combo box the value shows 100%
break;
case 2:
opacity=9;
break;
case 3:
opacity=8;
break;
case 4:
opacity=7;
break;
case 5:
opacity=6;
break;
case 6:
opacity=5;
break;
case 7:
opacity=4;
break;
case 8:
opacity=3;
break;
case 9:
opacity=2;
break;
case 10:
opacity=1; // in the combo box the value shows 10%
break;
default:
opacity=10;
break;
}
setOpacity();
GuiClock guic = new GuiClock(); // being the class with the other GUI JFrame
guic.setOpacity();
}
public void setOpacity() {
Main m = new Main();
float fOpacity = (float)m.opacity/10;
String sOpacity = Float.toString(fOpacity)+"f";
this.setOpacity(Float.parseFloat(sOpacity));
}
The GUI clock updates on a thread, so does the void above interfere with the thread? If so should I just make another thread that constantly checks for opacity changes, or would this just chew through extra CPU.
What can I do, so that the opacity changes on all of the GUIs given input from the combo box?
note : if there is a more effective way to optimize my code, or change it entirely please tell me. I'm just trying to learn more :)...