Reputation: 143
I can not seem to get this to work. My assignment will only let us use JTextAreas. The issue with my code is that I can not read it text in the TextArea. The goal is to run the logic after the user types in ENTER after they type in their input.
When I run the code I can only type in one character.. and the the GUI presents the character after a zero for reasons I can not figure out. Ex: [0b ] will be in the TextArea. Please help I can't figure this out.
public class ArabicToRomanGUI extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextArea enterRomanNumber = new JTextArea();
JLabel label = new JLabel();
JPanel panel = new JPanel();
JFrame frame = new JFrame();
//TestArea contructor adds jtextArea to jframe
public ArabicToRomanGUI()
{
super("Convert a Roman Numeral");
setLayout(new FlowLayout());
//Text field to enter a roman numeral
enterRomanNumber = new JTextArea(1,25);
enterRomanNumber.setText("Delete this text and Enter a Roman Numerial Here!");
//enterRomanNumber.setAlignmentX(0);
//enterRomanNumber.setAlignmentY(0);
add(enterRomanNumber);
HandlerForTextArea handler = new HandlerForTextArea();
enterRomanNumber.addKeyListener(handler);
}
private class HandlerForTextArea implements KeyListener
{
//used to process text field events
@Override
public void keyTyped(KeyEvent e)
{
String userInput = "";
userInput = enterRomanNumber.getText();
userInput = userInput.toUpperCase();
ConversionLogic.ConvertFromRomanToArabic(userInput); //converts user string of Roman numerals to an int in arabic
String arabicNumberAsString = ConversionLogic.getConvertedRomanNumeral();
enterRomanNumber.setText(arabicNumberAsString);
//user pressed enter in JTextField enterNumberField
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
//enterRomanNumber.setText(arabicNumberAsString);
if (ConversionLogic.getCheckFail() == true)
{
JOptionPane.showMessageDialog(frame, "The Roman Numeral entered is Invalid", "Error", JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(frame, "The arabic equivalent is " + arabicNumberAsString + "." , "Conversion Successful", JOptionPane.PLAIN_MESSAGE);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
//not used
}
@Override
public void keyReleased(KeyEvent e) {
//not used
}
}//end inner class TextFieldHandler
}//end class ArabicToRomainGUI
Upvotes: 2
Views: 135
Reputation: 285430
As you'll read time and time again on this site -- don't use a KeyListener
with a text component such as a JTextArea
as this can mess up the functioning of the text component. Instead use a DocumentListener
for when you wish to detect changes to the state of the JTextArea
after it happens, or a DocumentFilter
if you wish to detect (and possibly change) changes to the text component before it is posted to the text component.
I see that you're using a JTextArea(1, 25), or a single-line JTextArea
, which makes me ask: why not use a JTextField
? If you do this and want to trap the ENTER key press, then you can simply add an ActionListener
to the JTextField
.
Upvotes: 4