Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Using an inner class actionListener in another class

You declared:

PrintQueue s = new PrintQueue();

inside the main() method - which means that it's available only from inside main() (method scope). If you want to use it outside the method, you'll have to declare it as a class member and expose it via a "get()" method.

For example:

public class GUI { 

  static PrintQueue s = null;  

  public static void main(String[] args) {        
    s = new PrintQueue();

  public PrintQueue getS() {return s;}

Then you'll be able to use it:

public void actionPerformed
(ActionEvent e) { 
     System.out.println("The length of the queue
is: " + s.length() +"
"); //from the same class

or from outside the class using:

GUI gui = new Gui();
PrintQueue s = gui.getS();

I'm not sure if main() is the right place to instantiate PrintQueue - it might be so, but it might also be that a constructor would be a better idea.

Categories : Java

Related to : Using an inner class actionListener in another class
How to open a Jframe which is in another class as a constructor with actionListener?
Well, when the button is clicked you want actionPerformed() to be called. But the button is in SlotMachine so actionPerformed() needs to be in the SlotMachine class as well. You didn't post SlotMachine but it would look something like this: public class SlotMachine extends JFrame implements ActionListener { public static void main(String[] args) { SlotMachine mainFrame = new SlotMachi

Categories : Java
ActionListener with a custom Button class (Java)
I am not sure if I understand you correctly but think that instead of : downButton.addActionListener(this); you must use: downButton.addActionListener(downButton); since you are trying to add action listener somewhere outside MyButton so 'this' does not point to instance of MyButton and MyButton must implement ActionListener ie this method: @Override public void actionPerformed(ActionEvent

Categories : Java
I don't understand the call to the parent class from within the class in this JAVA class
Your DownloadHandler class has a static void main method, which is the single point of entry when executing command-line programs. That method is not a constructor. What it does is initialize a new instance of DownloadHandler and invoking an instance method on that object by passing the given String argument. Not sure what's the usage there. In order for your initialization to compile, you

Categories : Java
Class Inheritance, using Child Class to set up Parent Class
It's good practice to call the super-init-method, so I would suggest: class Character(): 'Common base class for all characters' def __init__(self, attack, defence, strength): self.attack = attack self.defence = defence self.strength = strength class Enemy(Character): 'Enemy Class' def __init__(self): Character.__init__(self, attack=random.randint(

Categories : Python
How to call a different method(written in second class) run from different class(first class)?
If you want to call an instance method of the second class, you have to create an object of the second class and call the method through it. ( "Second" is the class name) Second mySecond = new Second(); mySecond.myMethod(); If you want to call a static method of the second class, you can call it by mentioning the second class in following way Second.myMethod();

Categories : Java
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 Publishing Limited. All rights reserved.