Results 1 to 5 of 5

Thread: Help with this

  1. #1
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default Help with this

    Hi, I am writing application and I thought this would be the easiest part, but it is driving me crazy. Basically, I have a JTextField and a JButton. When I click on the JButton, it checks to see if the letter A is entered in the JTextField. I am using the getText() method but for some reason, the else condition always executes! Please note that i have used the JButton to perform other tasks and it works fine.

    Help is much appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Don't use == to compare Strings

    i.e.
    Java Code:
    // bad
    if (str1 == str2) {
    ...
    // good
    if (str1.equals(str2)) {
    ...

  3. #3
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

    Default

    I have tried this but still no luck. My code is posted below
    //--------------------------------------------------------
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class QuestionsAnswers extends JFrame
    {


    public QuestionsAnswers()
    {



    JPanel content = (JPanel)getContentPane();

    /* Set JFrame Properties */
    setSize(1000, 700);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.red); // Sets frame color to grey
    setVisible(true);
    setTitle("Java123 - Questions and Answers");

    JPanel panel1 = new JPanel( );
    JPanel panel2 = new JPanel( );
    JPanel panel3 = new JPanel( );





    /* Question Label */
    JLabel question = new JLabel( );
    question.setText("Java is ..... ");
    question.setSize(700,400);
    panel1.add(question);

    /* Option Labels */
    JLabel optionA = new JLabel( );
    optionA.setText("A. an object-oriented programming language");
    optionA.setSize(300,25);
    panel2.add(optionA);


    JLabel optionB = new JLabel( );
    optionB.setText("B. a structured programming language");
    optionB.setSize(300,25);
    panel2.add(optionB);


    JLabel optionC = new JLabel( );
    optionC.setText("C. a procedural programming language");
    optionC.setSize(300,25);
    panel2.add(optionC);



    /* Answer TextField */
    final JTextField answer = new JTextField( );
    answer.setColumns(5);
    panel3.add(answer);


    final String checkAnswer = "A";
    final String checkAnswer2;

    checkAnswer2 = answer.getText();


    /* Submit Answer Button */
    JButton submit = new JButton("Submit Answer");
    submit.setSize(80,30);
    panel3.add(submit);



    /* Add to content pane */
    content.add(panel1, BorderLayout.NORTH);
    content.add(panel2, BorderLayout.CENTER);
    content.add(panel3, BorderLayout.SOUTH);

    /* Menu Events caused by user clicks */
    // --------------------------------------------------------------------------------

    answer.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {



    if (checkAnswer2.equalsIgnoreCase(checkAnswer))
    {

    System.out.println("Yes");

    }

    else
    {

    System.out.println("No");

    }




    }

    });
    // --------------------------------------------------------------------------------


    }


    public static void main(String[] args)
    {
    QuestionsAnswers start = new QuestionsAnswers();
    start.setVisible(true);
    }
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Rewrite your actionListener to use getText from the textfield rather than a final local variable.

  5. #5
    Doctor Cactus is offline Member
    Join Date
    Oct 2008
    Posts
    63
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •