Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #4877
| From | markspace <-@.> |
|---|---|
| Newsgroups | comp.lang.java.gui |
| Subject | Re: Scope of a JLabel? Also "unchecked operation" |
| Date | 2012-01-03 11:18 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <jdvkag$nr7$1@dont-email.me> (permalink) |
| References | <4f0350b3$0$2549$da0feed9@news.zen.co.uk> |
On 1/3/2012 11:02 AM, A B wrote:
> 2) The itemStateChanged method can't recognise a perfectly good
> JTextArea that I want it to output to, just keeps saying that there is
> no variable called "display".
Right, now that I can read the darn thing, I can see that "display" is a
local variable, and not accessible outside of the constructor (or before
it's declared, in fact).
public Vectorine() {
...
JTextArea display = new JTextArea("Type here.", 4, 18);
...
}
public void itemStateChanged(ItemEvent event) {
...
display.setText("Option chosen: " + choice);
...
}
So the first instance of "display" can't bust out of those braces there
("{}") because it has local scope. You need to declare display with a
broader scope to allow it to be recognized in both methods (probably
instance scope). For example:
class Vectorine extends JFrame implements ItemListener {
private JTextArea display;
public Vectorine() {
...
display = new JTextArea("Type here.", 4, 18);
...
}
public void itemStateChanged(ItemEvent event) {
...
display.setText("Option chosen: " + choice);
...
}
}
<http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html>
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-03 19:02 +0000
Re: Scope of a JLabel? Also "unchecked operation" markspace <-@.> - 2012-01-03 11:05 -0800
Re: Scope of a JLabel? Also "unchecked operation" markspace <-@.> - 2012-01-03 11:18 -0800
Re: Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-03 21:22 +0000
Re: Scope of a JLabel? Also "unchecked operation" markspace <-@.> - 2012-01-03 14:10 -0800
Re: Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-04 21:05 +0000
Re: Scope of a JLabel? Also "unchecked operation" markspace <-@.> - 2012-01-04 13:10 -0800
Re: Scope of a JLabel? Also "unchecked operation" "Gavino" <invalid@invalid.invalid> - 2012-01-05 19:54 +0100
Re: Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-12 18:51 +0000
Re: Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-04 20:58 +0000
Re: Scope of a JLabel? Also "unchecked operation" "A B" <@bleBaker.uk> - 2012-01-12 18:53 +0000
csiph-web