Turn on thread page Beta
    Offline

    0
    ReputationRep:
    You need to put braces around the 2 instructions, and you are still doing the instructions in the wrong order.

    It should be

    Code:
    if((parent.Age.getText().equals(""))) {
      JOptionPane.showConfirmDialog(null, "You are not elegible for a young persons railcard", "Message", JOptionPane.PLAIN_MESSAGE);
      return;
    }
    
    String Age1 = parent.Age.getText();
    int Age2 = Integer.parseInt(Age1);
    parent.Age.setText(String.valueOf(Age2));
    Remember, you need to check the data before using it, not after, and you only need to return in the failure case, your code would return there under all circumstances, even in the success case (well, technically it wouldn't be executed if there was an exception...)
    • Thread Starter
    Offline

    0
    ReputationRep:
    (Original post by slugonamission)
    You need to put braces around the 2 instructions, and you are still doing the instructions in the wrong order.

    It should be

    Code:
    if((parent.Age.getText().equals(""))) {
      JOptionPane.showConfirmDialog(null, "You are not elegible for a young persons railcard", "Message", JOptionPane.PLAIN_MESSAGE);
      return;
    }
    
    String Age1 = parent.Age.getText();
    int Age2 = Integer.parseInt(Age1);
    parent.Age.setText(String.valueOf(Age2));
    Remember, you need to check the data before using it, not after, and you only need to return in the failure case, your code would return there under all circumstances, even in the success case (well, technically it wouldn't be executed if there was an exception...)
    That works thank god!!!!! (I never realised Java was that procedural)

    One more question, in my GUI I have a checkbox as well as a textfield (the component we were working with just now), how can you use two interfaces Actionlistener and ItemListener in the class?

    so lets say:

    if ((parent.student.getSelected == true) && (Age2>=18)){
    System.out.println("blah");
    }
    Offline

    0
    ReputationRep:
    Sorry, don't know anything about Swing...

    Anyway, yeah, instructions follow. You have to tell Java to do things in the order you want them doing, as with pretty much any programming language, you can't just give it some instructions in a random order and hope it assembles them into the right order for you...
    Offline

    1
    ReputationRep:
    (Original post by DOJO)
    That works thank god!!!!! (I never realised Java was that procedural)
    If you don't like return statements mid method, you can do something like this:

    Code:
    String Age1 = parent.Age.getText().trim();
    
    if((Age1.equals(""))) 
    {
          JOptionPane.showConfirmDialog(null, "You are not elegible for a young persons railcard", "Message", JOptionPane.PLAIN_MESSAGE);
    
    }
    else
    {
          int Age2 = Integer.parseInt(Age1);
          parent.Age.setText(String.valueOf(Age2));
    }

    (Original post by DOJO)
    One more question, in my GUI I have a checkbox as well as a textfield (the component we were working with just now), how can you use two interfaces Actionlistener and ItemListener in the class?

    so lets say:

    if ((parent.student.getSelected == true) && (Age2>=18)){
    System.out.println("blah");
    }
    In Java you can implement as many interfaces as you wish. I assume you currently want to do something like this

    Code:
    public class MyFrame extends JFrame implements Actionlistener, ItemListener
    {
          public MyFrame()
          {
                 componentOne.addActionListener(this);
                 componentTwo.addItemListener(this);
          }
    
          public void itemStateChanged(ItemEvent e) 
          {
                  //Do whatever
          }
    
          public void actionPerformed(ActionEvent aE) 
          {
                  //Do whatever
          }
    
    }
    For EventListeners, you dont have to explictly implement any though. You could have them as inner classes. Something like this:

    Code:
    public class MyFrame extends JFrame
    {
    
          public MyFrame()
          {
                 componentOne.addActionListener(new MyActionListener());
                 componentTwo.addItemListener(new MyItemListener());
          }
    
          private class MyItemListener implements ItemListener
          {
                public void itemStateChanged(ItemEvent e) 
                {
                        //Do whatever
                }
          }
    
          private class MyActionListener implements ActionListener
          {
                public void actionPerformed(ActionEvent aE)
                {
                        //Do whatever
                }
          }
    }
    The first option is probably neater, however the second option lets you have more than one different EventListener of the same type inside the class.
    Offline

    14
    ReputationRep:
    Or, as a third option, you can use anonymous inner classes:

    Code:
    public class MyFrame extends JFrame
    {
    
    	public MyFrame()
    	{
    		componentOne.addActionListener(new ActionListener()
    		{
    			public void actionPerformed(ActionEvent aE)
    			{
    				// Do whatever
    			}
    		});
    			
    		componentTwo.addItemListener(new ItemListener()
    		{
    			public void itemStateChanged(ItemEvent e)
    			{
    				// Do whatever
    			}
    		});
    	}
    }
 
 
 
Poll
Black Friday: Yay or Nay?
Useful resources

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.