Solved

How to create array of JFrame object ?

Posted on 2003-11-12
8
473 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
Windows 10 IE Certificate Issue 10 51
Error in @AspectJ Based AOP with Spring 2 18
jmss example java 2 23
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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