Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I gave this a try (hope that's what you want).</p> <p>With all that links and tutorials already provided, you should have been able to do that (IMO).</p> <p>That's what it looks like:</p> <p><a href="http://img97.imageshack.us/img97/9557/socombobox.png" rel="nofollow noreferrer">Screenshot http://img97.imageshack.us/img97/9557/socombobox.png</a></p> <p>It does <strong>not</strong> do proper exception handling, does <strong>not</strong> round the results and is <strong>not</strong> really object oriented (just uses hardcoded indexes, be careful when changing).</p> <hr> <p>Add the components (called <code>txtInput</code>, <code>cmbChoose</code>, <code>btnDo</code> and <code>txtResult</code> in my case.</p> <p>Edit the <code>model</code> property of your <code>JComboBox</code>, using <code>Combo Box Model Editor</code> and set it to</p> <pre><code>x5 plus10 plus5perc </code></pre> <p>This will generate the following source:</p> <pre><code>cmbChoose.setModel(new javax.swing.DefaultComboBoxModel( new String[] { "x5", "plus10", "plus5perc" })); </code></pre> <p>Put the following into your <code>JButton</code>s <code>ActionPerformed</code> method.</p> <pre><code>try { float input = Float.valueOf(txtInput.getText()); float output = 0; switch (cmbChoose.getSelectedIndex()) { case 0: output = input * 5; break; case 1: output = input + 10; break; case 2: output = input * 1.05f; } txtResult.setText(String.valueOf(output)); } catch (Exception e) { txtResult.setText("[Error]"); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload