I have a question about inner class access. I am not experienced in Java, so please bear with me.
Below is the code i wrote:
public class MainFrame extends JFrame {
...
private String selectedNodeString = NULL; //outer class variable
private JPanel createControlPanel() {
...
parseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
tree.addTreeSelectionListener(new MyTreeSelectionListener());
}
});
class MyTreeSelectionListener implements TreeSelectionListener {
public void valueChanged(TreeSelectionEvent e) {
selectedNodeString = //compile error, can not resolve type
}
}
}
Below is an example from "thinking in java" explaining inner class access, where inner class can access outer class variable.
interface Selector {
...
}
public class Sequence {
private Object[] items;
private class SequenceSelector implements Selector {
...
private int i = 0;
public boolean end() { return i == items.length; }
public Object current() { return items[i]; }
public void next() { if(i < items.length) i++; }
}
}
So what is wrong with my code, is it more than 1 layer of inner class, and how to fix it?
Thanks
You are missing a curly brace after the method createControlpanel.
private JPanel createControlPanel() {
...
parseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
tree.addTreeSelectionListener(new MyTreeSelectionListener());
}
});
} // missing this one.
Related
I'm learning Java, I met an example of Closure:
public class Callbacks {
public static void main(String[] args) {
Callee1 c1 = new Callee1();
Callee2 c2 = new Callee2();
MyIncrement.f(c2);
Caller caller1 = new Caller(c1);
Caller caller2 = new Caller(c2.getcallbackReference());
caller1.go();
caller1.go();
caller2.go();
caller2.go();
}
}
interface Incrementable {
void increment();
}
class Callee1 implements Incrementable {
private int i = 0;
#Override
public void increment() {
i++;
print(i);
}
}
class MyIncrement {
public void increment() {
System.out.println("another operation");
}
public static void f(MyIncrement m) {
m.increment();
}
}
class Callee2 extends MyIncrement {
private int i = 0;
public void increment() {
super.increment();
i++;
print(i);
}
private class Closure implements Incrementable {
#Override
public void increment() {
Callee2.this.increment();
}
}
Incrementable getcallbackReference() {
return new Closure();
}
}
class Caller {
Incrementable callbackRegerence;
Caller(Incrementable cbh) {
callbackRegerence = cbh;
}
void go() {
callbackRegerence.increment();
}
}
Comment from the author of the example :
When Mylncrement is inherited into Callee2, increment( ) can’t be overridden for use by Incrementable, so you’re forced to provide a separate implementation using an inner class.
My question is: What? Why can't we? We can override it in the Callee2 class or did I misunderstand the author?
Please explain what he wanted to say with this comment.
You need to have a type of Incrementable as a Caller argument.
You can change this to have the same.
old
class Callee2 extends MyIncrement {
private int i = 0;
public void increment() {
super.increment();
i++;
print(i);
}
private class Closure implements Incrementable {
#Override
public void increment() {
Callee2.this.increment();
}
}
Incrementable getcallbackReference() {
return new Closure();
}
}
New:
class Callee2 extends MyIncrement implements Incrementable {
private int i = 0;
public void increment() {
super.increment();
i++;
System.out.println(i);
}
}
I would like to create an enum containing one attribut, a list of objects extending the same interface or the same abstract class.
The objective is to have a loop on each list of my enum to call methods dynamically.
public interface Regles {
void verifier();
}
public class Regle01 implements Regles {
#Override
public void verifier() {
}
}
public class Regle02 implements Regles {
#Override
public void verifier() {
}
}
public enum ListRegles {
ENUM1(Arrays.asList(new Regle01(), new Regle02())),
ENUM2(Arrays.asList(new Regle01()))
private List<Regles> regles = new ArrayList<Regles>();
ListRegles(List<Regles> r) {
regles = r;
}
}
how can i do this please ?
enum:
public enum ListRegles {
ENUM1(new Regle01(),new Regle02()),
ENUM2(new Regle01());
private List<Regles> regles ;
ListRegles(Regles... regles) {
this.regles = new ArrayList<>(Arrays.asList(regles));
}
public void verify() {
for (Regles regle : regles) {
regle.verifier();
}
}
}
Will call verifier for Regle01 and Regle02
ListRegles.ENUM1.verify();
I have an abstract class Action with children like SendMessageAction.
I would like to run these actions in a service but how could I force implementation of each child ?
For example I would like to implement an abstract method : void run(Action action)
and methods "run" for each possible Action with an error if some methods are missing.
Any idea ?
Something like below should help you to get started. Happy coding!
Action.java
public abstract class Action {
protected abstract void runAction();
}
MessageSenderAction.java
public class MessageSenderAction extends Action {
public void runAction() {
//send message
}
}
SomeOtherAction.java
public class SomeOtherAction extends Action {
public void runAction() {
//do something else
}
}
ActionHandler.java
public class ActionHandler {
private final static ActionHandler INSTANCE = new ActionHandler();
private ActionHandler() {}
public static ActionHandler getInstance() {
return INSTANCE;
}
private List<Action> allActions = new ArrayList<Action>();
public void addAction(Action action) {
allActions.add(action);
}
public void runAllActions() {
for(Action action: allActions) {
//just to handle exception if there is any. Not to hamper other actions in case of any failures
try {
action.runAction();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
ActionDemo.java
public class ActionDemo {
public static void main(String... args) {
ActionHandler actionHandler = ActionHandler.getInstance();
Action msgSenderAction = new MessageSenderAction();
Action someOtherAction = new SomeOtherAction();
actionHandler.addAction(msgSenderAction);
actionHandler.addAction(someOtherAction);
actionHandler.runAllActions();
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
Im trying to get jTextField value in another class but always get error null exception. Here is my code :
Class Main :
public class FormTambahDoc extends javax.swing.JFrame {
Utility utility;
public FormTambahDoc() {
initComponents();
utility = new Utility();
setButton();
}
public String gettextIdentitasPengguna() {
return textIdentitasPengguna.getText();
}
private void setButton() {
btnSimpan.addActionListener(new ActionListener() {#Override
public void actionPerformed(ActionEvent e) { utility.cek();} });
}
}
Class another:
public class Utility {
FormTambahDoc formTambahDoc;
//FileJpaController controller;
public void cek()
{
String inputText = formTambahDoc.gettextIdentitasPengguna();
System.out.println(inputText);
//return `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException`
}
}
What's wrong in this code ?
You should create instance of FormTambahDoc before you ca use it:
FormTambahDoc formTambahDoc = new FormTambahDoc(); or get the instance from somewhere. Otherwise formTambahDoc will always be null. Check again your AWT tutorial.
Try following
public class FormTambahDoc extends javax.swing.JFrame {
Utility utility;
public FormTambahDoc() {
initComponents();
utility = new Utility(this);
setButton();
}
public String gettextIdentitasPengguna() {
return textIdentitasPengguna.getText();
}
private void setButton() {
btnSimpan.addActionListener(new ActionListener() {#Override
public void actionPerformed(ActionEvent e) { utility.cek();} });
}
}
public class Utility {
FormTambahDoc formTambahDoc;
//FileJpaController controller;
public Utility(FormTambahDoc aForm) {
formTambahDoc = aForm;
}
public void cek()
{
String inputText = formTambahDoc.gettextIdentitasPengguna();
System.out.println(inputText);
//return `Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException`
}
}
I have a class SaveFile implementing ActionListener. The method in it takes a string argument compleName. How do I make completeName be accessable in actionPerformed method in that class.
Thanks
class SaveFile implements ActionListener {
public void uploadToDatabase(String completeName){
}
public void actionPerformed(ActionEvent e) {
// I want to access completeName here
}
}
}
just use it as a variable inside your class
class SaveFile implements ActionListener {
private string completeName;
public void uploadToDatabase(String compName){
//code...
this.completeName = compName;
}
public void actionPerformed(ActionEvent e) {
System.out.println(completeName);
}
}
}
Simply ensure you store completeName as an instance variable.
class SaveFile implements ActionListener {
private String completeName;
public void uploadToDatabase(String completeName){
// do other things
this.completeName = completeName;
}
public void actionPerformed(ActionEvent e) {
// use this.completeName to get that value
}
}
}