I have a static method in which I would like to return a String from a JTextField. I would like it to act like Scanner's nextLong(), when the method is called, the rest of the program stalls until input. Here's my code, with my attempt on how to do this.
String y;
JTextField a = new JTextField(7);
a.setFont(silkScreen);
a.setForeground(Color.white);
a.setBackground(Color.black);
a.setAlignmentX(JTextField.CENTER_ALIGNMENT);
a.setBorder(javax.swing.BorderFactory.createEmptyBorder());
a.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (a.getText().length() >= 9) { // limit textfield to 9 characters
e.consume();
}
}
});
a.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
y = a.getText();
}
});
panel.add(a);
frame.add(panel);
frame.pack();
frame.setSize(640 / 2, 480 / 2);
frame.setVisible(true);
return y;
But, Java spits out this error:
Local variable y defined in an enclosing scope must be final or effectively final