Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create array of JFrame object ?

Posted on 2003-11-12
8
Medium Priority
?
484 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 750 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

886 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