0

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 :)...

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
Arc
  • 441
  • 1
  • 9
  • 26
  • 2
    I don't know about others, but I can't see how we can help you with snippets of code like this. I believe that you're going to have to create and post a [minimal, compilable, runnable example program](http://stackoverflow.com/help/mcve), if we're going to have a chance to understand your problem and help. – Hovercraft Full Of Eels Feb 22 '14 at 14:52

1 Answers1

2

First of all take a look to this topic: The Use of Multiple JFrames, Good/Bad Practice?. You should have only one JFrame and use non-modal JDialog instead. Have a look to How to Use Modality in Dialogs article.

About your ActionListener implementation you can highly simplify your code by doing this:

@Override
public void actionPerformed(ActionEvent e) {
    JComboBox comboBox = (JComboBox)e.getSource();
    Integer selectedValue = (Integer)comboBox.getSelectedValue();
    Float opacity = Float.valueOf((float)(selectedValue / 100)); // if 10 then 0.1, 20 then 0.2, and so on
    // set the opacity to the frame and dialogs    
}

Finally, by doing this:

public void opacityComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
    ...
    GuiClock guic = new GuiClock(); // being the class with the other GUI JFrame
    guic.setOpacity();
}

You don't change the opacity of a displayed window but you create a new one instead. You need to keep reference to the displayed GuiClock object and change the opacity of this one.

The same happens here:

public void setOpacity() {
    Main m = new Main();
    ...
}

For a better help, post a Minimal, Complete, Tested and Readable example.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    1+ re creation of new objects, but I'm still confused as to why his code would cause his little GUI's to lock. That's the part that would require an mcve, I think. – Hovercraft Full Of Eels Feb 22 '14 at 15:15
  • 1
    Totally agree. I've linked the MCTRE page to encourage OP post an example. Also have voted to close because as is we cannot provide nothing but advices. @HovercraftFullOfEels – dic19 Feb 22 '14 at 15:19