1

I am currently stuck on a bit of program, I'm sure there is a way to do it but just can't think of it, so here it goes. so I am using eclipse window viewer and I made a GUI to how I want and I got a Ccombo with a few options to select from, and what I want to do is when a button is clicked I want the program to be able to see what they selected and then depending on what they selected have a different outcome put into another text field

OptionsOptimizer = new Shell();
    OptionsOptimizer.setSize(450, 340);
    OptionsOptimizer.setText("Options Optimizer");

    Label lblSpread = new Label(OptionsOptimizer, SWT.NONE);
    lblSpread.setAlignment(SWT.CENTER);
    lblSpread.setBounds(10, 10, 213, 15);
    lblSpread.setText("Type Of Spread");

    CCombo combo = new CCombo(OptionsOptimizer, SWT.BORDER);
    combo.setItems(new String[] {"Put Credit Spread", "Short Naked Put", "Put Broken Wing Butterfly", "Custom Naked Put", "Call Debit Spread", "Call Calander", "Call Ratio Backspread", "Put Diagonal", "Short Straddle", "Short Strangle", "Short Iron Condor", "Short Iron Butterfle"});
    combo.setBounds(10, 31, 213, 21);

     Button Enter = new Button(OptionsOptimizer, SWT.NONE);
     Enter.addMouseListener(new MouseAdapter() {
        public void mouseDoubleClick(MouseEvent e) {
            double profit = Double.parseDouble(Profit.getText());
            double margin = Double.parseDouble(Margin.getText());
            double roi = profit/margin;
            double roundroi = Math.round(roi*10000)/100.0;
            ROI.setText("Your return on investment is " + roundroi + "%");
            double percent = Double.parseDouble(ITM.getText());
            double OTM = ((100 - percent)/100);
            int day = Integer.parseInt(Days.getText());
            double roc = (roi/day)*OTM;
            double roundroc = Math.round(roc*10000)/100.0;
            ROC.setText("Your return on capital per day is " + roundroc + "%");
            double annual = roc*365*.4;
            double roundannual = Math.round(annual*10000)/100.0;
            Annual.setText("Your annual return is " + roundannual + "% if you invested 40% of your total equity");

            Point spread = combo.getSelection();
                if(spread.toString() == "Put Credit Spread")
                {
                    Volitility.setText("A fall in implied volitility will help your position");
                }   
    });

    Enter.setBounds(159, 108, 75, 25);
    Enter.setText("Evaluate");

}
Andrew D
  • 11
  • 4

1 Answers1

1

I can see one problem with your code: Your are comparing the text of the Button using ==, but you should be using spread.toString().equals("Put Credit Spread").

See How do I compare strings in Java? for more information.


As a bit of advice: CCombo has a getSelectionIndex function that returns a integer value of the selected item beginning at 0, or -1 if no item is selected. Using this function would have the benefit of beeing able to change the Item text without needing to change the logic aswell.

Community
  • 1
  • 1
NoRelect
  • 598
  • 4
  • 20
  • i tried changing the == to .equals and its is having the same effect (the volitility.settext() is doing nothing – Andrew D Dec 16 '16 at 20:32
  • Does executing `Volitility.setText("asdf")` outside of the button handler work? – NoRelect Dec 17 '16 at 10:54
  • yes ive tried to just do volitility.settext(combo.gettext()). if i do this it will show in the text field whatever i selected – Andrew D Dec 17 '16 at 16:46
  • What version of SWT are you using? the API I looked up only has a `addSelectionListener()`, not a `addMouseListener()` for the Button class. – NoRelect Dec 17 '16 at 19:16
  • I am using eclipse neon and installed from there – Andrew D Dec 18 '16 at 17:55
  • Do you want that you have to click the button twice in a row very fast (`mouseDoubleClick`) or do you want to click it once to process something (`mouseDown`)? just rename the method in the MouseListener accordingly. – NoRelect Dec 18 '16 at 20:43
  • clicking once would be fine, but would that change how the if statement works? – Andrew D Dec 21 '16 at 22:05
  • You code worked fine when I clicked the Button very fast repeatedly... It doesn't change the way the if-statement works, but it changes when to mousedoubleclicklistener gets called – NoRelect Dec 22 '16 at 14:11