Solved

VisualStudio J++ beginner

Posted on 2000-04-18
11
287 Views
Last Modified: 2006-11-17
Can anyone give me a start with Vis Studio J++? I am "from" Visual Basic. I know that everything in Java is objects, but I cant reconcile in my mind what I get on the screen with that framework at the moment. For example, if I take a form, put a text box on it (edit object in J++), then try to give the edit box a string literal like "hello world", I cant decide where to put the code. I get eros saying that it cant reference edit1 without a valid object. I see a lrage grey area on the screen which changes if I put text into the edit object via the Properties window. BUt when I try to put a piece of settext code into the class structure I run across the above scenario. Anyone with any tips greatly appreciated. I have loads of books etc. and this is not laziness; I just want to get a real user's view as to how to go about thinking through these first steps. Thanks.
0
Comment
Question by:futureminds
  • 5
  • 5
11 Comments
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
When you place the edit object on the form, you are creating a Java Bean.  This is a class that can by instantiated when your form is created.  If you want to place text in edit1, you need to find where an instance of edit1 is created, and then call it's setText() method like you've been trying.

Most visual editors work with JavaBeans.  It is a technology that allows you to create Beans or Components.  Each one is a class that follows the JavaBean specification.  When you place something on the form, a Bean is created, a class that can be visually editted.  When you use the form, the Bean, and all Beans it has inside (like your edit box) is instantiated.  That is when you actually have an instance of the Bean edit1.

Please post code/comments if this doesn't help :)

Laminamia :)
0
 

Author Comment

by:futureminds
Comment Utility
Hi Laminamia:

I appreciate your answering. However I am in a bit of a dilemma because I cannot really relate your comments to the Very little I know. I see no referece at all in the Waite book I have on Java to Java Beans. I realise there are books about it in plenty and that you are probably right, but I am looking more for an explanation of exactly where the code for the exampleedit object would go, and why,perhaps, if Java is objects, there is not an interface like the VB one where you point and click at such an object and get put inot the coding interface for that object. Sorry. Maybe you can explain it differently.
Thanks
FM.
0
 
LVL 1

Expert Comment

by:phiro
Comment Utility
edit1.setText("Hello world");
Most function calls in Java are done by get and set function.
So
String s = edit1.getText() //would retrieve your text and place it in a String object.
0
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
Futureminds...didn't you say you tried the setText method in the code?

Laminamia
0
 

Author Comment

by:futureminds
Comment Utility
Hi Laminamia:

This is the entire code on my screen. The only parts I put into it are those which I have underlined; the rest came from VJ++ itself, including the part which appears in grey on my screen and which I have tried to mark below. The task list displays the error message that it "cannot reference the member edit1 without an object". But it seems to manage to do it when the code in the grey area that J++ itself generated, put the word "edit1" in the edit box! I am stumped as to what is going on. If I could make sense of what happens to my "function call", I might be able to see the picture a bit more clearly. That was what I meant when asking this question.

CODE STARTS:

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

/**
 * This class can take a variable number of parameters on the command
 * line. Program execution begins with the main() method. The class
 * constructor is not invoked unless an object of type 'Form1' is
 * created in the main() method.
 */
public class Form1 extends Form
{
      public Form1()
      {
            // Required for Visual J++ Form Designer support
            initForm();

//UNDERLINED            
            dothisforme();
//END OF UNDERLINING

            // TODO: Add any constructor code after initForm call
      }

      /**
       * Form1 overrides dispose so it can clean up the
       * component list.
       */
      public void dispose()
      {
            super.dispose();
            components.dispose();
      }

//UNDERLINED
      public static void dothisforme()
      {
                        
            edit1.setText("havedoneit");
            
      }
//END OF UNDERLINING


                         
GREY PART ON MY SCREEN, WHICH I HAVE NOT ALTERED AT ALL ...                                      
      
      /**
       * NOTE: The following code is required by the Visual J++ form
       * designer.  It can be modified using the form editor.  Do not
       * modify it using the code editor.
       */
      Container components = new Container();
      Edit edit1 = new Edit();

      private void initForm()
      {
            this.setText("Startup Form");
            this.setAutoScaleBaseSize(13);
            this.setClientSize(new Point(292, 271));

            edit1.setLocation(new Point(32, 32));
            edit1.setSize(new Point(224, 20));
            edit1.setTabIndex(0);
            edit1.setText("edit1");

            this.setNewControls(new Control[] {
                                          edit1});
      }

/ END OF GREY PART ON MY SCREEN, WHICH I HAVE NOT ALTERED AT ALL ...                                      

      /**
       * The main entry point for the application.
       *
       * @param args Array of parameters passed to the application
       * via the command line.
       */
      public static void main(String args[])
      {
      
            Application.run(new Form1());
//UNDERLINED
            Form1.dothisforme();
            
      }
}

//END OF UNDERLINING
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Accepted Solution

by:
Laminamia063099 earned 40 total points
Comment Utility
The problem is that dothisforme is a static method (you call it on the class), and edit1 is an instance of a class.  In java, you can not call instance variables (nonstatic variables) from a static method.  Remove the Form1.dothisforme from the main.  Remove the static modifier on the dothisforme method declaration.  Now, your dothisforme method call in the Form1 constructor should successfully change the edit box text.  Try it, and if that doesn't change the text, post the changed code.

Laminamia :)
0
 

Author Comment

by:futureminds
Comment Utility
Adjusted points from 30 to 40
0
 

Author Comment

by:futureminds
Comment Utility
Hi Laminamia:

Yes, your directions worked fine. Now I need to understand two more parts of this jigsaw, and I hope you agre these are not additional questions, but part of the same novices problem that I am experiencing. But I am increasing the points to 50 in recognition of the fact that it may seem I am digressing, but I feel I am not. The two foggy areas in what I have just done are: I thought that constructors were methods to tell the environment that an instance of a particular class now existed - an object. What use is it therefore to put a method - such as my dothisforme - into a constructor when the constructor will only be called once at the beginning?

Secondly, I thought that the "real" programme only starts to be run when the main method fires, and that was the place to put, from my dim-witted perspective, a bunch of top-down programmed calls to this and that objects, functions, etc?

And finally, (he says), "static" in VB etc was a qualifier applied to local variables so they hold their values between function calls. How can a method be static? And if it has to be removed from the class, what then would methods be allowed inside calsses of they cant do anything?

Sorry about the crassness of these points, but I hope you see what I am sloshing around in. I will be really pleased if you can help me with these points. I am sure I will raise the ponts to 75 if its possible to clear the fog for me.
0
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
Sorry, I must have missed your extra questions.  On static, in VB and C/C++ static is used on variables so that they hold their value.  In Java, however, the qualifier static means that it exists (the method or the variable) without an instance of the class.  This could be a static variable in which you store the number of instances of an object, or you could store static information that is the same for all classes.  A static method can be called without an instance of the class.

On constructors: A constructor is called when an object is created.  It is not just called to tell the environment that an object is created, but it is an integral part of actually creating and initializing the object.  You may call any method you need to in a constructor if you need to for initialization.

I hope this answers your question.  If you would like to see some examples, drop another comment and I'll leave some.

Laminamia ;)
0
 

Author Comment

by:futureminds
Comment Utility
Cripes; I think I nearly understood all that. Call me happy, but I might be getting somewhere ...

Take care
futurem.
0
 
LVL 3

Expert Comment

by:Laminamia063099
Comment Utility
Good luck!

Laminamia :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now