Solved

how to add a two dimension array JTextField to a JPanel?

Posted on 2003-11-13
3
846 Views
Last Modified: 2012-08-13
Hi, I'm writing an assignment which is almost the same as the little game "Master Mind".
I'm supposed to design a text field which contains 10 rows and 6 columns for users to input their guess (in my case, different kinds of colors), and there's a "OK" botton at the end of each row.
In order to make the checking job easier, I used "static JTextField[][] input_color = new JTextField[10][6]". and I wrote sth as follows:
public testFrame () {
        trial.setLayout(new GridLayout(10, 6));
        for(int i = 0; i < 10; i ++)
          for(int j = 0; j < 6; j ++){
            trial.add(input_color[i][j]);        
        }
}
It compiled well, but the problem came when running it. The error message said "Exception in thread "main" java.lang.NullPointerException".
How should I deal with it? What's the right way to add this stuff to the panel?
Thanks!!!
 
0
Comment
Question by:Valley617
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 20 total points
ID: 9741036
You have to initialize each row with new array and each entry with new JTextField():
for(int i = 0; i < 10; i ++){
    input_color[i] = new JTextField[6];
    for(int j = 0; j < 6; j ++){
        input_color[i][j] = new JTextField();        
    }
}
0
 

Author Comment

by:Valley617
ID: 9741430
Yup!! Thanks!!
btw, can I simply use "input_color[i].setEditable(false)" to make every element in that row is non-editable?
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 9747030
Same 2-dimentional loop with input_color[i][j].setEditable(false), after you have input_color elements initialized.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
servlet example 17 53
how to install java on RHEL image on EC2 4 41
ejb mdb examples 1 10
How to configure empty element in XML Document parser? 15 36
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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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 …

830 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