Results 1 to 5 of 5
Thread: Help with this
- 11-11-2008, 11:43 AM #1
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
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.
- 11-11-2008, 12:12 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Don't use == to compare Strings
i.e.
Java Code:// bad if (str1 == str2) { ... // good if (str1.equals(str2)) { ...
- 11-11-2008, 12:32 PM #3
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0
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);
}
}
- 11-11-2008, 12:49 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Rewrite your actionListener to use getText from the textfield rather than a final local variable.
- 11-11-2008, 12:58 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 63
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks