Solved

ToggleButton switch boolean

Posted on 2011-02-16
23
1,363 Views
Last Modified: 2012-05-11
Please advice, I'm already little bit crazy with this ToggleButton, I study plenty sites and docs and everybody write different advises.
I create class BusinessLogicControl in JDeveloper and try to establish two things.
1. boolean flag0 unselected is false, after - true;
2. color of button, initial red and after clicking green;
Hundreds samples, but all of them useless.
No one has exact solution which can advice proper solution way for my case.
All samples don't connected to any real situation, just create class ToggleButton and play with it.
JDeveloper create JFrame class which has no relation to the published samples, maybe because all this samples already antique.
 
public class BusinessLogicControl extends JFrame {
    private JLabel jLabel1 = new JLabel();
    private JToggleButton jToggleButton1 = new JToggleButton();
    private JButton jButton1 = new JButton();
    transient IGateway mFxcmGateway;
    private boolean flag0 = false;

    public BusinessLogicControl() {
        try {
            jbInit();
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jbInit() throws Exception {

        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(554, 397));
        this.setTitle("BusinessLogic Control Panel");
        this.setBackground(new Color(214, 255, 239));
        jLabel1.setText("EUR/USD");
        jLabel1.setBounds(new Rectangle(70, 50, 70, 15));
        jLabel1.setPreferredSize(new Dimension(70, 15));
        jLabel1.setOpaque(true);
        jToggleButton1.setText("");
        jToggleButton1.setBounds(new Rectangle(185, 40, 70, 40));
        jToggleButton1.setSize(new Dimension(70, 40));
        jToggleButton1.setMaximumSize(new Dimension(70, 40));
        jToggleButton1.setMinimumSize(new Dimension(70, 40));
        jToggleButton1.setBackground(Color.red);
        jToggleButton1.setFont(new Font("Lucida Grande", 1, 14));
        jToggleButton1.setBorderPainted(false);
        jToggleButton1.setOpaque(true);
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jToggleButton1_mouseClicked(e);
                }
            });
        jButton1.setText("DISCONNECT");
        jButton1.setBounds(new Rectangle(120, 300, 85, 29));
        jButton1.setPreferredSize(new Dimension(200, 40));
        jButton1.setMinimumSize(new Dimension(200, 40));
        jButton1.setMaximumSize(new Dimension(200, 40));
        jButton1.setOpaque(true);
        jButton1.setSize(new Dimension(200, 40));
        jButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jButton1_mouseClicked(e);
                }
            });
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jToggleButton1, null);
        this.getContentPane().add(jLabel1, null);
        jToggleButton1.setModel(new JToggleButton.ToggleButtonModel());
    }

    private void jButton1_mouseClicked(MouseEvent e) {
        mFxcmGateway.logout();
        System.exit(0);
    }

    private void jToggleButton1_mouseClicked(MouseEvent e) {
    }
}

Open in new window

JBotton1 I've arranged and it works perfect, but for toggle button I spend whole day without any positive result.
Please advice.
0
Comment
Question by:gbcbr
  • 13
  • 6
  • 3
  • +1
23 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
What I would suggest - just don't use this JToggleButton - use normal JButton
and change background or font color of it when you need to show the user  and change
the flag value - all that in the event handler (say in mouseClicked) - you'll
be in fuill control of it and don't have to deal with this
strange way they implemented this JToggleButton in swing
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
Is it for the exrcise of JToggleButton or this is some project for the real life?
if this is the exercise - then it's another story of course
0
 
LVL 7

Expert Comment

by:rodness
Comment Utility
The problem is that you don't really want a JToggleButton for this.

A JToggleButton needs to be in a group with other JToggleButtons, and when clicked, will stay clicked until you click another button in the group.

If you just use a JButton, you can click it to turn it on and off.


I changed your JToggleButton to a JButton and added the logic to change the color, and it seems to work just fine.


public class BusinessLogicControl extends JFrame {

    private JLabel jLabel1 = new JLabel();
    private JButton jToggleButton1 = new JButton();  // NOTE CHANGED TO JBUTTON
    private JButton jButton1 = new JButton();
    transient IGateway mFxcmGateway;
    private boolean flag0 = false;

    public BusinessLogicControl() {
        try {
            jbInit();
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jbInit() throws Exception {

        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(554, 397));
        this.setTitle("BusinessLogic Control Panel");
        this.setBackground(new Color(214, 255, 239));
        jLabel1.setText("EUR/USD");
        jLabel1.setBounds(new Rectangle(70, 50, 70, 15));
        jLabel1.setPreferredSize(new Dimension(70, 15));
        jLabel1.setOpaque(true);
        jToggleButton1.setText("");
        jToggleButton1.setBounds(new Rectangle(185, 40, 70, 40));
        jToggleButton1.setSize(new Dimension(70, 40));
        jToggleButton1.setMaximumSize(new Dimension(70, 40));
        jToggleButton1.setMinimumSize(new Dimension(70, 40));
        jToggleButton1.setBackground(Color.red);
        jToggleButton1.setFont(new Font("Lucida Grande", 1, 14));
        jToggleButton1.setBorderPainted(false);
        jToggleButton1.setOpaque(true);
        jToggleButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jToggleButton1_mouseClicked(e);
                }
            });
        
        jButton1.setText("DISCONNECT");
        jButton1.setBounds(new Rectangle(120, 300, 85, 29));
        jButton1.setPreferredSize(new Dimension(200, 40));
        jButton1.setMinimumSize(new Dimension(200, 40));
        jButton1.setMaximumSize(new Dimension(200, 40));
        jButton1.setOpaque(true);
        jButton1.setSize(new Dimension(200, 40));
        jButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jButton1_mouseClicked(e);
                }
            });
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jToggleButton1, null);
        this.getContentPane().add(jLabel1, null);
//        jToggleButton1.setModel(new JToggleButton.ToggleButtonModel());   // UNNECESSARY
    }

    private void jButton1_mouseClicked(MouseEvent e) {
        mFxcmGateway.logout();
        System.exit(0);
    }

    private void jToggleButton1_mouseClicked(MouseEvent e) {
    	// Toggle the flag, then set color
    	flag0 = !flag0;
    	if ( flag0 ) {
    		jToggleButton1.setBackground( Color.green );
    	} else {
    		jToggleButton1.setBackground( Color.red );
    	}
    }

    public static void main(String[] args) {
    	BusinessLogicControl blc = new BusinessLogicControl();
    	blc.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	blc.setVisible(true);
    }
}

Open in new window

0
 

Author Comment

by:gbcbr
Comment Utility
This is real project, I need to control deals direction.
I have to allow to open SELL or BUY position alone, both of them or prohibit all deals.
This I see it's very easy with this JToggleButtons.
For each command from BusinessLogic I will connect one boolean flag0s and flag0b, initial position will be false, so no deals allowed from the beginning.
If I decide to open deals in any direction, I'll press SELL or BUY button to change value to the true and allow deals in selected direction.
If I'll use JButton for this actions I will need 2 buttons for each side, one for open and another one for closing deals.
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
You still can have one button and you can change what is written
on the button - once they press it you can change from the eventhandler and toggle it your self from BUY
to SELL if that is what you want using Button.setText() and change color using Button.setBackground(Color.RED)
and it is all strightforward, why to dela with these ainful toggle buttons?
0
 

Author Comment

by:gbcbr
Comment Utility
>> rodness
I make your sample like this:
public class BusinessLogicControl extends JFrame {
    private JLabel jLabel1 = new JLabel();
    private JButton jButton2 = new JButton();
    private JButton jButton1 = new JButton();
    transient IGateway mFxcmGateway;
    transient BusinessLogic_EURUSD bLogic_EURUSD;
    private boolean flag0s = false;
    private boolean flag0b = false;
    double[] outY0 = new double[6];
    private boolean clicked;

    public BusinessLogicControl() {
        try {
            jbInit();
            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jbInit() throws Exception {

        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(554, 397));
        this.setTitle("BusinessLogic Control Panel");
        this.setBackground(new Color(214, 255, 239));
        jLabel1.setText("EUR/USD");
        jLabel1.setBounds(new Rectangle(70, 50, 70, 15));
        jLabel1.setPreferredSize(new Dimension(70, 15));
        jLabel1.setOpaque(true);
        jButton2.setText("");
        jButton2.setBounds(new Rectangle(185, 40, 70, 40));
        jButton2.setSize(new Dimension(70, 40));
        jButton2.setMaximumSize(new Dimension(70, 40));
        jButton2.setMinimumSize(new Dimension(70, 40));
        jButton2.setBackground(Color.red);
        jButton2.setFont(new Font("Lucida Grande", 1, 14));
        jButton2.setBorderPainted(false);
        jButton2.setOpaque(true);
        jButton2.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e){
                    try {
                        jButton2_mouseClicked(e);
                    } catch (Exception f) {
                    }
                }
            });
        jButton1.setText("DISCONNECT");
        jButton1.setBounds(new Rectangle(120, 300, 85, 29));
        jButton1.setPreferredSize(new Dimension(200, 40));
        jButton1.setMinimumSize(new Dimension(200, 40));
        jButton1.setMaximumSize(new Dimension(200, 40));
        jButton1.setOpaque(true);
        jButton1.setSize(new Dimension(200, 40));
        jButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jButton1_mouseClicked(e);
                }
            });
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jLabel1, null);
        jButton2.setModel(new JToggleButton.ToggleButtonModel());
    }

    private void jButton1_mouseClicked(MouseEvent e) {
        mFxcmGateway.logout();
        System.exit(0);
    }


    private void jButton2_mouseClicked(MouseEvent e) throws Exception {
        flag0s = !flag0s;
        if (flag0s) {

            jButton2.setBackground(Color.green);
          bLogic_EURUSD = new BusinessLogic_EURUSD();
            bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0s, flag0b);
            
        } else {

            jButton2.setBackground(Color.red);
            bLogic_EURUSD = new BusinessLogic_EURUSD();
            bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0s, flag0b);
        }
    }
}

Open in new window

and put println into BusinessLogic class:
public BusinessLogic_EURUSD() throws Exception {
    }

    public void businessLogic_EURUSD(double[] outY0, boolean flag0s, boolean flag0b) throws Exception {

        System.out.println("  flag0s   " + flag0s);

        if (outY0 != null) {
            try {

Open in new window

and I have this output
flag0s   false
  flag0s   false
  flag0s   false
  flag0s   true
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   true
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   false
  flag0s   true
  flag0s   false
  flag0s   false

Open in new window

So, as soon I click on the button it starts to be green and value change to true, but in the next second value changed back to false, but button remains green, I click again to change a color to red and after next click again changed value for 1 second.
I suppose that is the problem that this button doesn't keep value permanently.
Another class which call BusinessLogic is the AlgoCalc but it call it from constructor and, theoretically, has not create new instances all the time.
public class AlgoCalcY_EURUSD {

    BusinessLogic_EURUSD bLogic_EURUSD;
    double[] outY0 = new double[6];
    boolean flag0s;
    boolean flag0b;

    public AlgoCalcY_EURUSD() throws Exception {
        bLogic_EURUSD = new BusinessLogic_EURUSD();
    }

    public void outY(double[][] Y) throws Exception {


        if (Y[0][0] != 0) {

            outY0[0] = (Y[0][0] - Y[0][3]) * 1000000;
            outY0[1] = (Y[0][3] - Y[0][6]) * 1000000;
            outY0[2] = (Y[0][6] - Y[0][9]) * 1000000;
            outY0[3] = (Y[0][9] - Y[0][12]) * 1000000;
            outY0[4] = (Y[0][12] - Y[0][15]) * 1000000;
            outY0[5] = (Y[0][18] - Y[0][21]) * 1000000;

            
            bLogic_EURUSD.businessLogic_EURUSD(outY0,flag0s,flag0b);

        } else
            System.out.println(new java.util.Date() +
                               "     N0 data from ArrayConverter");
    }
}

Open in new window

Please advice
0
 
LVL 7

Expert Comment

by:rodness
Comment Utility
Remove this line:

        jButton2.setModel(new JToggleButton.ToggleButtonModel());

Open in new window


jButton2 is not a JToggleButton, so making it use a ToggleButtonModel is causing issues.
0
 

Author Comment

by:gbcbr
Comment Utility
No, this doesn't solve the issue, output the same
0
 

Author Comment

by:gbcbr
Comment Utility
I suppose that JButton can change value only once, after clicking. And after value returns to it's original value == false.
0
 
LVL 7

Expert Comment

by:rodness
Comment Utility
No, the JButton can constantly switch values back and forth.

This simplified version of your code gives the following output (I commented out your other classes so it would compile):

true
false
true
false
true
false
true
false

public class BusinessLogicControl extends JFrame {

    private JLabel jLabel1 = new JLabel();
    private JButton jButton2 = new JButton();
    private JButton jButton1 = new JButton();
//    transient IGateway mFxcmGateway;
//    transient BusinessLogic_EURUSD bLogic_EURUSD;
    private boolean flag0s = false;
    private boolean flag0b = false;
    double[] outY0 = new double[6];
    private boolean clicked;

    public BusinessLogicControl() {
        try {
            jbInit();
//            this.mFxcmGateway = DBFXConnect.getGateway();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jbInit() throws Exception {

        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(554, 397));
        this.setTitle("BusinessLogic Control Panel");
        this.setBackground(new Color(214, 255, 239));
        jLabel1.setText("EUR/USD");
        jLabel1.setBounds(new Rectangle(70, 50, 70, 15));
        jLabel1.setPreferredSize(new Dimension(70, 15));
        jLabel1.setOpaque(true);
        jButton2.setText("");
        jButton2.setBounds(new Rectangle(185, 40, 70, 40));
        jButton2.setSize(new Dimension(70, 40));
        jButton2.setMaximumSize(new Dimension(70, 40));
        jButton2.setMinimumSize(new Dimension(70, 40));
        jButton2.setBackground(Color.red);
        jButton2.setFont(new Font("Lucida Grande", 1, 14));
        jButton2.setBorderPainted(false);
        jButton2.setOpaque(true);
        jButton2.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e){
                    try {
                        jButton2_mouseClicked(e);
                    } catch (Exception f) {
                    }
                }
            });
        jButton1.setText("DISCONNECT");
        jButton1.setBounds(new Rectangle(120, 300, 85, 29));
        jButton1.setPreferredSize(new Dimension(200, 40));
        jButton1.setMinimumSize(new Dimension(200, 40));
        jButton1.setMaximumSize(new Dimension(200, 40));
        jButton1.setOpaque(true);
        jButton1.setSize(new Dimension(200, 40));
        jButton1.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    jButton1_mouseClicked(e);
                }
            });
        this.getContentPane().add(jButton1, null);
        this.getContentPane().add(jButton2, null);
        this.getContentPane().add(jLabel1, null);
    }

    private void jButton1_mouseClicked(MouseEvent e) {
//        mFxcmGateway.logout();
        System.exit(0);
    }


    private void jButton2_mouseClicked(MouseEvent e) throws Exception {
        flag0s = !flag0s;
        if (flag0s) {

            jButton2.setBackground(Color.green);
//          bLogic_EURUSD = new BusinessLogic_EURUSD();
//            bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0s, flag0b);
            
        } else {

            jButton2.setBackground(Color.red);
//            bLogic_EURUSD = new BusinessLogic_EURUSD();
//            bLogic_EURUSD.businessLogic_EURUSD(outY0, flag0s, flag0b);
        }
        
        System.out.println( flag0s );
    }

    public static void main(String[] args) {
    	BusinessLogicControl blc = new BusinessLogicControl();
    	blc.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	blc.setVisible(true);
    }

}

Open in new window



Try printing the value of flag0s in your BusinessLogicControl class instead of in BusinessLogic_EURUSD and see what that tells you.  (Take out the println in BusinessLogic_EURUSD).  I bet you've got other things calling BusinessLogic_EURUSD with a false flag, so you've got extra println's there which are confusing.

0
 

Author Comment

by:gbcbr
Comment Utility
I don't need this button without commented lines.
These lines are key points of my app.
I don't interested just to change color and value in this class, I need this value work it BusinessLogic_EURUSD class.
This is main idea, if it can't keep permanent value of flag0s in BusinessLogic_EURUSD, this is useless game, because this class create business logic based on this value.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:rodness
Comment Utility
My point is that the button itself will keep a permanent value just fine.
It's something else in your code that's causing the problem.

I think you've just got BusinessLogic_EURUSD println'ing too often, showing you the value is false even though it wasn't always called from the BusinessLogicControl class.
0
 

Author Comment

by:gbcbr
Comment Utility
Maybe you are right that button can keep value in permanent condition is some other situation.
In this case we have every second incoming data into BusinessLogic_EURUSD class from AlgoCalc class, apart of that instance created into the constructor of AlgoCalc class, incoming data may disturb this value and return it into initial state-false.
In this case we have to check this suspicion.
0
 

Author Comment

by:gbcbr
Comment Utility
Yes, I comment >>bLogic_EURUSD.businessLogic_EURUSD(outY0,flag0s,flag0b);
and block incoming data.
And as a result value changed only by clicking button.
So, the question how to keep value in permanent state undependable from incoming value?
0
 
LVL 7

Expert Comment

by:rodness
Comment Utility
I doubt it's the actual flag0s in the button that's getting changed.
You probably have more things calling bLogic_EURUSD.businessLogic_EURUSD than you realize.

For example, in the method jButton2_mouseClicked, you create a new BusinessLogic_EURUSD every time the button is clicked.  Do you intend to do that?
0
 

Author Comment

by:gbcbr
Comment Utility
I move >>bLogic_EURUSD = new BusinessLogic_EURUSD(); from method into constructor, but nothing changed
0
 
LVL 7

Expert Comment

by:rodness
Comment Utility
Well, I don't know that that's the only problem.  It was just an example of how things can get called more than you expect.

Another example is uninitialized values.  In AlgoCalcY_EURUSD.outY,  you call BusinessLogic_EURUSD with a flag0s value, but you don't appear to have ever initialized that value.  So maybe that's a problem also.
0
 

Author Comment

by:gbcbr
Comment Utility
Maybe I need to create separate class for flag0s and flag0b values and call from this class BusinessLogic_EURUSD.
I'll try to do this tomorrow, today is too late.
I'll let you know about result.
0
 

Author Comment

by:gbcbr
Comment Utility
I create separate class DecisionCenter_EURUSD_S, responsible only for SELL deals, which has to receive and keep flag0s value from BusinessLogicControl class by pressing button SELL, which has to send flag0s = true and get value of bl0 from BusinessLogic class, if the value will satisfy conditions.
 
public class DecisionCenter_EURUSD_S {

    private static DecisionCenter_EURUSD_S dc_EURUSD_S;
    ISide side;
    boolean flag0s;

    public DecisionCenter_EURUSD_S() {
    }

    public static DecisionCenter_EURUSD_S getInstance(boolean flag0s) {

        if (flag0s == true )
        {
            dc_EURUSD_S = new DecisionCenter_EURUSD_S();
        }

        return dc_EURUSD_S;
    }

    public void decisionCenter_EURUSD_S(boolean bl0) throws Exception {

        System.out.println("  bl0   " + bl0);
        System.out.println("  flag0s   " + flag0s);

        try {
            if (bl0 == false && flag0s == true)

                side = SideFactory.SELL;
            System.out.println("    side  " + side);

            AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);

        } catch (Exception ex) {
            Logger.getLogger(BusinessLogic_EURUSD.class.getName()).log(Level.SEVERE,
                                                                       null,
                                                                       ex);
        }
    }
}

Open in new window

public class BusinessLogic_EURUSD {

    boolean bl0;
    boolean flag0s;
    boolean flag0b;

    public BusinessLogic_EURUSD() throws Exception {
    }

    public void businessLogic_EURUSD(double[] outY0) throws Exception {

        if (outY0 != null) {
            try {

                if (outY0[0] < 0 && outY0[1] < 0 && outY0[2] < 0 &&
                    outY0[3] < 0 && outY0[4] < 0 && outY0[5] < 0) {

                    bl0 = false;

                    DecisionCenter_EURUSD_S.getInstance(flag0s).decisionCenter_EURUSD_S(bl0);


                } else if (outY0[0] > 0 && outY0[1] > 0 && outY0[2] > 0 &&
                           outY0[3] > 0 && outY0[4] > 0 && outY0[5] > 0) {

                    bl0 = true;

                    DecisionCenter_EURUSD_B.getInstance(flag0b).decisionCenter_EURUSD_B(bl0);

                } else {
                }
            } catch (Exception ex) {
                Logger.getLogger(BusinessLogic_EURUSD.class.getName()).log(Level.SEVERE,
                                                                           null,
                                                                           ex);
            }
        }
    }
}

Open in new window

private void jButton2_mouseClicked(MouseEvent e) throws Exception {
        flag0s = !flag0s;
        if (flag0s) {
            jButton2.setBackground(Color.green);
            dc_EURUSD_S.decisionCenter_EURUSD_S(bl0);
            
        } else {
            jButton2.setBackground(Color.red);         
          dc_EURUSD_S.decisionCenter_EURUSD_S(bl0);
        }
    }

Open in new window

but I have absolutely wrong output:
bl0   false
  flag0s   false
    side  null

    EUR/USD  
    Margin use = 0.0%
 Thu Feb 17 13:36:56 EET 2011

  bl0   false
  flag0s   false
    side  null

    EUR/USD  
    Margin use = 0.0%
 Thu Feb 17 13:36:59 EET 2011

Open in new window

Each time when I press button SELL I get value false apart of true and automatically generated original value bl0==false.
My opinion we have to go back to Toggle button or create for for each direction two buttons SELL - STOP, SELL - START.
Only what it's work in this system perfect is color changing:).
0
 

Author Comment

by:gbcbr
Comment Utility
I solve most of the problems. I create JToggleButton instead of JButton and found proper code;
 
private void jToggleButton1_mouseClicked(MouseEvent e) throws Exception {

        AbstractButton abstractButton = (AbstractButton) e.getSource();
        boolean flag0s = abstractButton.getModel().isSelected();
        dc_EURUSD_S = new DecisionCenter_EURUSD_S(flag0s).getInstance(flag0s);

        System.out.println("Action - flaf0s = " + flag0s + "\n");
        
        if (flag0s == true) {
            jToggleButton1.setBackground(Color.green);

        } else {            
            jToggleButton1.setBackground(Color.red);
        }
    }

Open in new window

So, now when press button it change not only color and also boolean value.
The problem which remain is how to set this value permanently into the class DecisionCenter, because even when I call instance of this class created by sending flag0s = true; it again changed back to false after incoming message from BusinessLogic class.  
public class DecisionCenter_EURUSD_S {

    private static DecisionCenter_EURUSD_S dc_EURUSD_S;
    ISide side;
    boolean flag0s;

    public DecisionCenter_EURUSD_S(boolean flag0s) {

    }


    public static DecisionCenter_EURUSD_S getInstance(boolean flag0s) {
        
        if (flag0s == true)
        {
            System.out.println("$$$$$$$$  flag0s   " + flag0s);
            dc_EURUSD_S = new DecisionCenter_EURUSD_S(flag0s);
        }
        return dc_EURUSD_S;
    }

    public void decisionCenter_EURUSD_S(boolean bl0) throws Exception {

        System.out.println("  bl0   " + bl0);
        System.out.println(" &&&&&&&  flag0s   " + flag0s);

        try {

            if (bl0 == false && flag0s == true)

                side = SideFactory.SELL;
            System.out.println("    side  " + side);

            AlgoTradeMarket_EURUSD.getInstance(null).algoTradeMarket(side);


        } catch (Exception ex) {
            Logger.getLogger(BusinessLogic_EURUSD.class.getName()).log(Level.SEVERE,
                                                                       null,
                                                                       ex);
        }
    }
}

Open in new window

public class BusinessLogic_EURUSD {

    boolean bl0;
    boolean flag0s;
    boolean flag0b;


    public BusinessLogic_EURUSD() throws Exception {
    }

    public void businessLogic_EURUSD(double[] outY0) throws Exception {

        if (outY0 != null) {
            try {
               if (outY0[0] < 0 && outY0[1] < 0 && outY0[2] < 0 &&
                    outY0[3] < 0 && outY0[4] < 0 && outY0[5] < 0) {

                    bl0 = false;

                    System.out.println("   bl0 = " + bl0 + "\n");

                    DecisionCenter_EURUSD_S.getInstance(flag0s).decisionCenter_EURUSD_S(bl0);

Open in new window

// Initial output after pressing button

$$$$$$$$  flag0s   true  // from DecisionCenter
Action - flaf0s = true   // from BusinessLogicControl

// Output after sending value bl0 = false to DecisionCenter

 bl0 = false //from BusinessLogic

  bl0   false            // from DecisionCenter
 &&&&&&&  flag0s   false //
    side  null           //

Open in new window

Please advice how to set value of boolean for DecisionCenter class and make it changeable only from BusinessLogicControl class.
0
 

Author Comment

by:gbcbr
Comment Utility
OK, this is already another question, I publish my result, so maybe somebody will find this JToggleButton code useful:)
0
 
LVL 2

Accepted Solution

by:
Crazy_Bytes earned 500 total points
Comment Utility
Hi,

maybe a little late, but you could override the color scheme for all JToggleButtons in your app by the following lines:

        UIManager.put("ToggleButton.select", Color.GREEN);
        UIManager.put("ToggleButton.background", Color.RED);

But as said before this scheme will apply for JToggleButtons in you app.

I attached a simple demo class. Maybe it helps :)

Best regards,
CB


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;

@SuppressWarnings("serial")
public class ToggleButtonDemo extends JPanel {
    private JLabel[] lbl;
    private JToggleButton[] jtb;
    private static final Color RED = new Color(225, 0, 0);
    private static final Color GREEN = new Color(0, 225, 0);

    private boolean myBooleanFlag[];

    public ToggleButtonDemo() {
        init();
    }

    private void init() {
        setLayout(new GridBagLayout());

        lbl = new JLabel[5];
        jtb = new JToggleButton[5];
        myBooleanFlag = new boolean[5];

        // Override UI-Settings
        UIManager.put("ToggleButton.select", GREEN);
        UIManager.put("ToggleButton.background", RED);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTHWEST;

        for (int y = 0; y < lbl.length; y++) {
            final int fy = y;
            lbl[y] = new JLabel("Switch " + (y + 1) + ":");
            jtb[y] = new JToggleButton("Off");
            jtb[y].setContentAreaFilled(true);
            jtb[y].setOpaque(true);
            myBooleanFlag[y] = jtb[y].isSelected();

            jtb[y].addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        jtb[fy].setText("On");
                    } else {
                        jtb[fy].setText("Off");
                    }
                    // Apply selection-state to flag
                    myBooleanFlag[fy] = jtb[fy].isSelected();
                }
            });

            gbc.fill = GridBagConstraints.VERTICAL;
            gbc.gridx = 0;
            gbc.gridy = y;
            gbc.insets = new Insets(5, 5, 0, 0);
            add(lbl[y], gbc);

            gbc.anchor = GridBagConstraints.NORTHWEST;
            gbc.fill = GridBagConstraints.NONE;
            gbc.gridx = 1;
            gbc.gridy = y;
            gbc.insets = new Insets(0, 5, 0, 5);
            add(jtb[y], gbc);
        }

    }

    public static void main(String[] args) {
        JFrame f = new JFrame(ToggleButtonDemo.class.getSimpleName());
        f.getContentPane().add(new ToggleButtonDemo());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

Open in new window

0
 

Author Comment

by:gbcbr
Comment Utility
Thank you
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now