if else statement

to read from combo box in if else

Page 1 of 1

4 Replies - 3446 Views - Last Post: 22 August 2010 - 05:14 PM Rate Topic: -----

#1 amalina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-August 10

if else statement

Posted 22 August 2010 - 01:10 PM

hi. i already do some coding in java. i need some help to make some correction in my java coding. my problem is to make if else statement. i want to read the variable from combo box. when the specific item is selected, i want to paste it in 'OK' button.

this is the error part.
okBtn = new javax.swing.JButton();

cBox1 = new javax.swing.JComboBox();
        
cBox2 = new javax.swing.JComboBox();


private void OkPerform(java.awt.event.ActionEvent evt) {                           
        // TODO add your handling code here:
        if (cBox1.selectedItem.label == "Herba" & cBox2.selectedItem.label == "Tonik Bersalin") <--error
        
        {
        new HerbaView().setVisible(true);
    }



Is This A Good Question/Topic? 0
  • +

Replies To: if else statement

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Re: if else statement

Posted 22 August 2010 - 01:20 PM

With JComboBox, you need to use the getSelectedItem() method, as there is no selectedItem variable. Also, with Strings better to compare with the equals() method and not the == operator, as equals() compares the values while == compares the memory addresses.

In addition, not good practice to use the NetBeans GUI Builder as it produces unmaintainable code for humans. Swing is so easy, better to write it by hand. Check the Java tutorials section for a lot of good Swing tutorials.
Was This Post Helpful? 0
  • +
  • -

#3 amalina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-August 10

Re: if else statement

Posted 22 August 2010 - 01:25 PM

thanks for ur advice. i already learn some basic things in java. my problem is.. i left the java for 2 years.. then i want to start again. so i forget some basic things. thanks for once again.
Was This Post Helpful? 0
  • +
  • -

#4 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: if else statement

Posted 22 August 2010 - 04:50 PM

[quote]
if (cBox1.selectedItem.label == "Herba" & cBox2.selectedItem.label == "Tonik Bersalin")
[quote]
You are using a bitwise operator instead of a logical with &.
&& is equal to and

Mac gave you the other reasons you are having trouble.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10486
  • View blog
  • Posts: 38,857
  • Joined: 27-December 08

Re: if else statement

Posted 22 August 2010 - 05:14 PM

The bitwise and conditional AND operators will both produce the same results, but the conditional AND operator is more efficient in that it short-circuits if the first condition evaluates to false.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1