I have this piece of code :
public class Profile extends Container{
public Profile(String value) {
Container profilo_1 =new Container();
Container profilo_2 =new Container();
// (1) THIS ADD A BUTTON TO THE MAIN CLASS
this.createButton().setLabel("Modifica Profilo");
// (2) NOW I NEED TO ADD A BUTTON INTO THE INSTANCE OF profilo_2 BUT IT FAILS
profilo_2.add(this.createButton());
this.add(profilo_1);
this.add(profilo_2);
}
}
the point (2) fails, because it said that im about to adding a child to this container, but it is owner already by a container...
In fact, if i do this :
ILabel label=new ILabel();
profilo_2.add(label);
it said to me that ILabel() is abract and cannot be instantiated!
How can I fix it? Cheers to everybody :)
Guessing wildly, since this depends on your code... Try this (moreless what Piotr said)
profilo_2.add(profilo_2.createButton());
Try changing to
Button button2 = this.createButton();
button2.setLabel("EDIT");
profilo_2.add(button2);
By the way this has nothing to do with abstract classes, from what I see
EDIT: Though you say that #1 "adds a button to the main class", so does that mean that createButton() does this.add(button) ? If so then you should probably change that function so that isn't done every time you create a button.
The problem is probably that when you create a button with "this.createButton", that button has its parent set to "this" (in this context), and when you try to add it to profilo_2, it throws an error. Instead you should createButton on profilo_2 directly, then the parent will be the correct one (and perhaps you won´t have to add() it either?)
Probably, setLabel() returns something which cannot be passed to Container::add(..). Please provide your code for Container
Related
I am working in eclipse making a temperature converter app. I made a JButton btnConvert. However, when I go to make the event listener it tells me btnConvert cannot be resolved.
I noticed it isn't showing up where the other controls are listed on the outline either and I clearly have it in my code:
JButton btnConvert = new JButton("Convert");
Can anyone help? I have everything imported that needs to be imported and didn't run into any issues until this.
If the button was only created in a method, then it will only be visible within the scope of that method, and if you're trying to access it outside of the method it was created in, it will say it can't find the button. Instead, you may want to use an instance variable, it would look something like this:
public class WhateverYourClassNameIs
{
public JButton btnConvert;
public static void main(String[] args)
{
//define the instance variable, this doesn't have to be in main
// it can be wherever you are initializing the code you're working on
btnConvert = new JButton("Convert");
}
}
I'm somewhat new to Java and I was wondering if there was a way to change the function of a class somehow.
Class pager = new Pager();// everything is initialized
pager.dostuff() = function(){};
Is there a specific name for this and is it possible to do in Java? If not, is there a language that does allow it?
Thank you for your time.
Edit:
To clarify the question because originally it seemed like I wanted to initialize the new class with different functions. That is not the case. I want to change it after it's already been created. The reason for this is that I'm working with android and the class I'm getting is from the xml. Is it possible to change the class' function when I get the class like so?
Pager pager = (ViewPager) findViewById(R.id.pager)
I feel like I'm going to have to create a new class, which is ok but I wanted to see if I could do it this way.
In java you can override method at runtime like -
Pager pager = new Pager(){
#Override
public void dostuff(){
....
}
};
Runtime it will create subclass of Pager and override the doStuff method.
Of course ..It is possible through overriding of a class through an annonymous inner class while inheriting all the properties of the class being overriden.
Pager pager = new Pager(){
public void dostuff(){
....
}
}
PS:- Beware,Outside the anonymous inner class ,you may invoke only those methods present in your parent class through the instance of your annonymous inner class
Java does not offer a functionallity like this.
Maybe it is possible for you to override the function in a sub-class.
What you Showed above can be done in Ruby (i think).
Override
Pager pager = new Pager() {
#Override
public void doStuff() {
doSomethingElseFunct();
}
};
I am new to Java. My problem is that I have a class names MyClassExp. I have extended it from JFrame. Inside the class, I initiate an object of another class named TabbedFrame. TabbedFrame also extends a class DemoFrame . In DemoFrame, I have the title of the page set using keyword 'super' like:
super("Some Title");
Now when I run my MyClassExp, even after creating a JFrame as:
new JFrame("New Title");
I'm still getting the same title i.e Some Title. Is there any way to solve this problem? I've tried a lot to solve it but failed :'(
Use the API method public void setTitle(String title).
- Inside the MyClassExp class's constructor use this.setTitle(String title) method.
Just use setTitle("yourTitleName");
for example:
setTitle("Currency Converter");
textField.addKeyListener(this);
combo.addItemListener(this);
label.addMouseListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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.
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.