I'm trying to work out a way of creating a JTextfield dynamically without assigning it to a variable.
panel.add(new JTextField("hello"));
The code above works just fine, but when I try and manipulate like below the compiler says Void not allowed here. I'm a beginner and I know this is probably something simple, please help me out.
panel.add(new JTextField("hello").setForeground(Color.yellow));
You may define your own constructor, so that you can chain those calls together, although I don't know if it's worth the trouble, just to avoid a variable.
private JTextField newColoredTextField(String s, Color c) {
JTextField tf = new JTextField(s);
tf.setForeground(c);
return tf;
}
You may then use it like so:
panel.add(newColoredTextField("hello", Color.yellow));