Solved

How to create array of JFrame object ?

Posted on 2003-11-12
8
469 Views
Last Modified: 2010-03-31
Hi.. guys,

I'm very new to java. Now, I try to build application that have to use many windows. (Post It)
I want to create more than one jFrame for each note.
I build 2 classes.   this is the idea.
================================================
1. PostIt extend JFrame{
   //use PostItNote object as array
   PostItNote[] note;
   static i = 0;
   //everytime use press button , it will call
   note[i] = new PostItNote();
   i++;
}

2. PostItNote extend JFrame {
   // build postit note  
}
================================================

But when i press button, there is a warning "java.lang.NullPointerException"

If I use just variable just "note" , not array, it does works with no error.

How can I do?

Best Regards,

Thanya
0
Comment
Question by:thanyaj
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9737450
> PostItNote[] note;

You need to initialise your array.

eg. the following creates a 10 element array

PostItNote[] note = new PostItNote[10];

If you don't know the size up front and want your list to dynamically grow then you'd be better off using a collection class such as ArrayList.

0
 
LVL 15

Expert Comment

by:jimmack
ID: 9737961
If I may...

>> PostItNote[] note = new PostItNote[10];

Tells the JVM to reserve 10 "spaces" (object references) in which you can store PostItNotes.  These are initialised to null.

>> note[i] = new PostItNote();

Actually creates a PostItNote object and stores the reference to it in note[i].

Also, your counter "i", isn't declared properly.

>> static i = 0;

Should be:

  static int i = 0;

Or...  If you just want to create all 10 in a loop at this stage:

  for (int i = 0; i < 10; i++)
  {
     note[i] = new PostItNote();
  }

It may be a typo, but the code inside PostIt should also probably be inside a method (or constructor):

public class PostIt extends JFrame
{
    private PostItNote[] note;

    private int noteCount;

    public PostIt()
    {
        note = new PostItNote[10];
        noteCount = 0;
    }

    public void createNote()
    {
        note[noteCount] = new PostItNote();
        noteCount++;
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9738841
You might consider using JInternalFrames to do this as they are more appropriate. Although the illustration doesn't look too nice, as it's compressed in size, there's a good tutorial on this at:

http://java.sun.com/docs/books/tutorial/uiswing/components/internalframe.html
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:thanyaj
ID: 9739080

Thank you very very much.

I already knew that I miss the point that I must RESERVE SPACE for storing array of PostItNote Object ;)

Now, I create PostItNote every time I press button.

But when I close one of Jframes -> all windows are closed (exit).

            note[noteCount] = new PostItNote();
            note[noteCount].setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            noteCount++;

How can I just close what I want?
0
 

Author Comment

by:thanyaj
ID: 9739173
I'm sorry that I make some mistake about my last post.

I can use JFrame.DISPOSE_ON_CLOSE to close each JFrame.

The new problem is...

I want my program to be able to create and delete PostItNote.
If I delete PostItNote (click button on a note), I want to destroy the instance in order to reuse that instance again for new PostItNote.
How can PostIt Class know that PostItNote is destroyed?
So, They can create new PostItNote object for that instance.

I'm note sure that I explain clearly or not!  So, this is example.

In PostIt Class -> create note[0], note[1], note[2]   = new PostItNote()
In PostItNote Class -> delete note[1]

Then, PostIt Class -> create note again
How do PostIt Class know that It should use  note[1] = new PostItNote();

Thank you very much.

PS. If you know the web sites or books to recommend me, you're very welcome to ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9739199
>>But when I close one of Jframes -> all windows are closed (exit).

That's one of the reasons i suggested not using an array of JFrames. They are not really designed to be used in the way you want to use them as they don't behave independently. I would use JInternalFrame
0
 

Author Comment

by:thanyaj
ID: 9739433
I wish I could use it. But after reading document, I'm still wonder that can JInternalFrame stay outside DesktopPane?
Because I don't want all PostItNote to be stayed in a Big window. I want to move it around desktop.
0
 
LVL 92

Expert Comment

by:objects
ID: 9742053
> How can PostIt Class know that PostItNote is destroyed?

Use a WindowListener.
http://java.sun.com/docs/books/tutorial/uiswing/events/windowlistener.html

Thanks for the points :-)

http://www.objects.com.au



0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 56
how to add new optional parameter to JSP 1 41
jdbc error in jsp application 20 45
CSV file parsing thru Java 13 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

778 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