-2

can you tell me in details how do i know if a jcheckbox is checked or not? method isSelected didn't work with me it gives me an exception while running

{
 Sandwich = new JButton("Tall");
         contentPane.add(Tall);
         Sandwitch.setBounds(350, 110, 90,40);   //in main
         Sandwitch.addActionListener(this);

}
.....

public void actionPerformed(ActionEvent event) {
JButton clickedButton = (JButton) event.getSource();

        String  buttonText = clickedButton.getText();
..........
if(clickedButton.getText()=="Sandwitch"){
        if(Ketchup.getState()&&!Garlic.getState()){//

       itm=new Item(""+m+clickedButton.getText(),3.0);
        xyz.addItem(itm);
       textArea.append(" "+clickedButton.getText()+",");
        textArea.append(" "+itm.getPrice()+"\n");}

          else if(!Ketchup.isSelected()&&Garlic.isSelected()){//

....................
}

it gives a very long exception while running

can you please help me with this problem?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • 1
    what is the Exception?? – subash Nov 29 '13 at 13:01
  • what does the exception say....null pointer ??? – sanket Nov 29 '13 at 13:01
  • What do u meant by **very long exception** ? – Rakesh KR Nov 29 '13 at 13:04
  • Nobody can help you without knowing the exception you are getting. – Prasad Nov 29 '13 at 13:12
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Andrew Thompson Nov 29 '13 at 14:17
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). As to the 'very long' exception. Trim the bulk of it that references JSE classes out. Add the rest as an edit. – Andrew Thompson Nov 29 '13 at 15:10

1 Answers1

2

Don't use == to compare Strings!

if (clickedButton.getText()=="Sandwitch"){}

Use equals or equalsIgnoreCase()

if ("Sandwich".equalsIgnoreCase(clickedButton.getText()){
    // do something
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • -1, this won't remove OP's exception. – Prasad Nov 29 '13 at 13:26
  • This didn't help me getting rid of the exception. – user3049389 Nov 29 '13 at 15:23
  • 1
    @user3049389 you never posted your exception. How are we supposed to know what the exception is? Consider posting an [SCCEE](http://sccee.org) for more help. – Paul Samsotha Nov 29 '13 at 15:29
  • You have posted the same question at http://stackoverflow.com/questions/20289002/how-do-i-know-if-a-jcheckbox-is-checkedwith-gui The null pointer could be due to some object (may be the checkbox itself) has not been initiated... – sanket Nov 29 '13 at 15:44