Solved

How to create array of JFrame object ?

Posted on 2003-11-12
8
464 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
 

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
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

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

22 Experts available now in Live!

Get 1:1 Help Now