close button


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: close button

  1. #1
    Join Date
    Feb 2005
    Posts
    55

    close button

    System.exit(0) exits the whole system it also closes the main frame as well. So i decided to set the frame i want to be closed to be not visible, by using the setVisible method. The problem is that it wont compile and it is saying non-static method setVisible(boolean) cannot be referenced from a static context. How do I fix the problem.

    public void actionPerformed(ActionEvent event)
    {
    if(event.getSource() == close)
    {
    InputForm.setVisible(false);
    InputForm.dispose();
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    youre saying:

    InputForm.setVisible(false)

    but InputForm is a class on disk, not an object in memory. how can i tell? well, it starts with a capital letter. imagine that you made your input form like this:

    InputForm ipFrm = new InputForm(...)...

    then you would close it like this:

    ipFrm.setVisible(false);



    because you have written the name of the class (the file on disk that determines how an object will be built) rather than the object in computer memory itself, java is going to check for a special case called a static reference. there are SOME times in java that you can call a method without actually having an object in memory for it.. when you start a program for the first time, there are no objects in memory. that is why your main() HAS to be static; so it can be called without there being an object first. if it werent static, then it would be chicken-and-egg

    incidentally, i advice newcomers to java, to NEVER, EVER, EVER use statics (apart from in conjuction with main() ) - if you start relying on them, or sticking them in your code willy-nilly, you wont learn Object Oriented programming, and you will never really make a great programmer. statics have their place, but in university second or third year

    by the way;
    this is why we should always follow convention when naming something. an experience programmer like me comes along, and assumes you ahve obeyed convention of "class names start with a capital letter" (e.g. heres some you will have used: String, Integer, JOptionPane)
    it would be wrong to write: String MyString1 = "hello";
    because a programmer like me would see you post a message about "..blah.. MyString1.. problem..." ans say "well, your MyString1 class file is.. blah".. but MyString1 isnt a class, its a String variable..


    ---
    understanding the difference between classes and objects, (or static and dynamic) is one of the key things you will learn about java
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center