Solved

How to create array of JFrame object ?

Posted on 2003-11-12
8
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 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