I've found the similar question, but it's still be unclear for me.
So, I have a main class ProcessorCalculations(), from which I call MainFrame() class. In MainFrame class user should choose the folder. How I can transmit the JFileChooser() object from MainFrame() to ProcessorCalculations()?
I've tried to implement the hint from the link above:
ProcessorCalculation processor = new ProcessorCalculation();
MainFrame mainFrame = new MainFrame(processor);
But I don't know how to call processor methods from mainFrame without creating new objects.
Even I dont't know the correct question I should ask Google.
Help please.
If you're using the code written above, then you're passing the current processor instance into your MainFrame constructor. What are you doing with the reference from within this constructor? Are you settinga a ProcessorCalculation instance to this reference? Please show us your constructor.
Your MainFrame class should look something like...
public class MainFrame extends JFrame {
// your ProcessorCalculation field
private ProcessorCalculation processor;
public MainFrame(ProcessorCalculation processor) {
// set the field with ref passed in parameter
this.processor = processor;
// of course other code goes here
}
public void someMainFrameMethod() {
// use the reference
processor.someProcessorMethod();
}
}
Create an attribute say for example files in the mainframe by which the contents of JFileChooser() are referenced ( you may say contents are stored in this attribute ). If this attribute is private put getter setter methods in the Mainframe for this attribute ( to make it accessible from other classes) now coming back to your ProcessorCalculation class when you write mainFrame.getFiles() ( you have already created object mainFrame object there) it returns the data you wanted in this class.
In case you still face a problem please ask for a coded solution I will do.
Related
Giving the following class of my android Project :
Preview extends SurfaceView implements SurfaceHolder.Callback
and
A extends Doc
I don't really know how to ask and I know that this is not really good but I want that from Preview, I call an abstract method of Doc. In this Doc's method, I have to call a method of the previous Object of Preview.
This is an example :
From Preview.java :
Doc _doc = new A();
private void myMethod() {
this._doc.process(this)
}
From A.java :
#Override
public void process(Preview p) {
p.processA();
}
The problem is that I got an error :
The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})
However, I can't change this judging by the fact that I want to call the method from Preview. I tried many thing such as cast etc. None of them works.
Thanks for your help !
PS : I am on Eclipse under Windows.
Assuming you are calling A.process(this) from an anonymous inner class (of type Camera.PreviewCallback I presume, hence the error message), you have to write A.process(Preview.this), since a standalone this refers to the inner class and not to the Preview instance.
The method process(Preview) in the type Doc is not applicable for the arguments (new Camera.PreviewCallback(){})
It simply means you are passing the object of Camera.PreviewCallback but in your method public void process(Preview p) You want an object of Preview.
If you have written this code in side an anonymous class, then this won't point to the Preview class. It will point to the object of inner anonymous class.
Thus you need to write A.process(Preview.this)
I have to maintain a code to add more flexibility to a final static variable in a class.
The variable is no more a global constant and may be changed.
The problem is that the class is in a common library and used in different projects.
Do you have an approach or a design pattern better than copying and pasting the class code from the common library to my specific application and refactoring it?
Example:
Commons project
Class CommonClass {
public final static var globalSomething = somethingGlobal;
public static method(){ //CommonClass.globalSomething is used here}
}
In my App (and other apps that reference commons) we can use the static attribute and also call the method:
---> var b = CommonClass.somethingGlobal;
---> var c = CommonClass.method() //we know that CommonClass.globalSomething is used here
Expectations:
Ability to change CommonClass.somethingGlobal in my app and take these changes in call CommonClass.method()
I can modify (add methods) in the common class but i have to keep the same initial behavior (not to break other project referencing common project)
If I got you right, you want to implement this as a parameter.
Looking at your example:
var c = CommonClass.method() //we know that CommonClass.globalSomething is used here
there is already something wrong with it. You shouldn't have to know that you have to set CommonClass.somethingGlobal correctly before calling the method. This way the client has to know the implementation, violating the principle of information hiding. If the value is required, introduce it as parameter:
Class CommonClass {
public static void method(var globalSomething){}
}
An alternative would be making both your variable and your method non-static and use a constructor:
Class CommonClass {
public var globalSomething = somethingGlobal;
public CommonClass(var globalSomething) {
this.globalSomething = globalSomething;
}
public void method(){}
}
PS: Your example code is not java. I corrected it partially in my answer.
I am currently working on a project that needs to be refactored (it was not written by me and the original developer is not around). I see in that application that rather many classes have only private constructors and one or more static methods (getter/setter of the current class object). They also have non-static methods. I give you one example:
Class UndoManager that manages the actions taken on the application for performing undo/redo. It has only private constructors. When the application is loaded, UndoManager.setManager() is called. This method loads the undo history from a file or constructs a new one using a private constructor.
Later, every class can access this instance of UndoManager with syngronized static method .getManager().
In code:
public class UndoManager extends SimpleObservable<UndoManager> {
private static UndoManager instance;
private final Stack<Action> undoHistory;
private final Stack<Action> redoHistory;
public synchronized static void setManager(UndoManager undoManager) {
UndoManager instance = getManager();
instance.clear();
instance.undoHistory.addAll(undoManager.undoHistory);
instance.redoHistory.addAll(undoManager.redoHistory);
instance.notifyObservers(instance);
}
public synchronized static UndoManager getManager() {
if (instance == null)
instance = new UndoManager();
return instance;
}
private UndoManager() {
this.undoHistory = new Stack<Action>();
this.redoHistory = new Stack<Action>();
}
/.../
}
In this application multiple classes are used like this. They are not helper classes but classes that should have only one instance.
My question is:
is this kind of access good style? If not, how would you refactor the class and it's access?
I'm sorry if it is a duplicate, but I have searched in stackoverflow and google for a while but somehow I didn't find a satisfying answer. Thank you for any help.
This looks like a singleton pattern.
It is part of the great familly of designs patterns you might know them.
The point of this is to ensure that there is only one instance of this object used throughout your application. Indeed when you call getManager() it will return a new instance the first time and next times it will return the formerly created instance.
it's a design pattern that called Singleton. it's a lazy load and used for managers classes and service classes for example. they are for classes that you want an instance but only one instance of them.
there is usually a method to get the instance like your getManager method and a private constructor like you have
EDIT: Sorry, I just started programming in Java. It turned out to be a problem with an out of range array access... I am used to error messages about this kind of thing being automatic...
(using Netbeans 7.0.1)
I have been customizing JTextArea and JTable. I do so by adding a new Java class to my project and then declaring it extends the particular class I want (in my case, either JTextArea or JTable).
I had been using it normally, adding these new classes to JDialogs and JInternalFrames without any problem. I do so by just dragging it to my JDialog or JInternalFrame...
But recently, for some reason, I started getting this error messages "Component cannot be instantiated. Please make sure it is a JavaBeans component."
The JInternalFrames that were accepting the old customized classes still accepts them. But if I try to add the new customized class, it gives me that error message and, afterwards, it starts showing the same message to the old customized classes too...
Something really weird is going on. I copied the same code of a (previously) customized class to a new class (changing the name of the class, of course). Then I try to add this to my JInternalFrame. It gives me the error message! If, before this, I try to add the same customized class (with the original name), it adds the class normally....
This is annoying and I can't solve it. Can anyone help me please?
Thanks a lot for this answer but, if you want to know the reason here you are.
Typically this appears on two position:
an overridden method on your component.
a normal method on your component.
For example:
package UI.Components;
public class LabelComponent extends javax.swing.JLabel {
private javax.swing.JLabel label;
public TextFieldComponent() {
label = new javax.swing.JLabel(_label);
add(label);
}
#Override
public void setText(String text) {
label.setText(text);
}
}
The method setText(String text) is called say in the supper class constructor then it the overridden new method would be called in the case of the (label) variable which is used on this method still no being initialized so a java.lang.NullPointerException will be thowed.
solution:
1) try ... catch:
#Override
public void setText(String text) {
try {
label.setText(text);
} catch (Exception e) {
}
}
2) check:
use null initialization on declaration
private javax.swing.JLabel label = null;
then check on the method
#Override
public void setText(String text) {
if(label != null)
label.setText(text);
}
3)use initialization on declaration:
private javax.swing.JLabel label = label = new javax.swing.JLabel();
and then use setText method in your constructor
label.setText(_label);
note:
in the case of reason (2) a normal method on your component, it is the same as (1) but you may call the method before initialize the variable or assign null to the variable before calling the method and so on and it can being solved by the same ways.
I too faced the same problem, after some search in the web I found the solution for this problem. I don't have a deep understanding of why and how this problem occurs, but I can share with you the solution I found.
When you get such error msg, goto the menu View-->IDE Log or you can open the log from windows_user_Home\.netbeans\7.0\var\log
In that log you have to locate the error msg you got, for example,
INFO [org.netbeans.modules.form.BeanSupport]: Cannot create default instance of: test.Application1
java.lang.NullPointerException
at test.Application1.initLabel(Application1.java:906)
So the problem is in line 906 of your .java file. Open that file and comment those lines and then you will able to overcome the problem.
You can add the component to the Form or jInternalFrame or ...
After adding the component, you can again uncomment those lines. Just Clean and Build your project.
Hope this helps..
Goodluck
reachSDK
I have encountered the similar problem, however in different context.
I have two separate projects, a swing built user interface, and another one that poses as class library.
I added a class to the class library, headed over to the user interface, and implemented this newly added class from the library into the swing interface project in shape of an existing custom JFrame. So what happened to me now that the class loader of course could not find the class because the library project required compiling. The issue was fixed by compiling it.
It's a little embarassing to not know how to fix this on my own, seeing how I have a bit of experience with Java, however until now I've never really done anything other than web programming with Java.
I'm trying to create a wizard, and trying to generalize creation of the fields presented in the window. As such, I don't have direct control over the actual component JTextField but a wrapper class which handles the finer details. However I would like to know when the value has changed, so I've added a "addVetoableChangeListener" method which allows me to register a VetoableChangeListener to the JTextField itself.
I've verified that the method gets called and that it passes the listener onto the JTextField in debug. However, nothing gets called. No exception is launched, and my breakpoint inside the method which implements the interface VetoableChangeListener is never called.
Is there something I'm not getting? Does the listener have to be some sort of component before it works correctly or does it simply have to implement the interface? Perhaps I'm overlooking an obvious error because I've been concentrating on it for too long, and I'm hoping it'll be evident to one of you. A simpler version of what I'm attempting is:
public class TomcatConfigPanel extends WizardKeyValuePanel implements VetoableChangeListener {
protected void initPanel(JPanel mainPanel) {
addField("port", "8080");
IWizardField portField = getField("port");
portField.addVetoableChangeListener(this);
}
public void vetoableChange(PropertyChangeEvent evt)
throws PropertyVetoException {
// Stuff that would drive you Lovecraft insane if you saw what was written here
}
}
public class WizardTextField implements IWizardField {
private JLabel label;
private JTextField field;
public WizardTextField() {
// some initialization stuff ...
}
public void addVetoableChangeListener(VetoableChangeListener listener) {
field.addVetoableChangeListener(listener);
}
}
The VetoableChangeListener will only be called if a constrained property is being changed on the JTextField. A constrained property is a property whose setter method throws a PropertyVetoException. So, if your code never calls any such setter method on the JTextField, your listener won't ever be called. Read http://download.oracle.com/javase/tutorial/javabeans/properties/constrained.html for more details.
I haven't found any constrained property in JTextField (and in all its class hirarchy) in the API doc, so I doubt your listener could ever be called.