Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jbutton with Array

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
JButton[][] button1;
ImageIcon imgIcon;
imgIcon = new ImageIcon("none.gif");
button1 = new JButton[2][2];
for (int i = 0; i<1; i++){
for (int j = 0; j<1; j++){
buttonPlace[j].setIcon(imgIcon);
}
}

how can i solve null poiner error?
why i can't create array Jbutton?
 
Hi,
You have to instantiate each element of you array.
--
Globos
 
Hi Kay,

Perhaps you would like to take a look at the example below:

import javax.swing.*;
import java.awt.*;

public class Test
{
public static void main(String[] args)
{
JFrame f = new Frame1();
f.show();
}
}

class Frame1 extends JFrame
{
public Frame1()
{
setSize(300,300);
getContentPane().setLayout(null);
JButton[][] button = new JButton[2][2];
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
button[j] = new JButton(&quot;Hi&quot;);
(button[j]).setBounds(new Rectangle(i*100,j*50,75,25));
getContentPane().add(button[j],null);
}
}
}
}

all you have to do is to add in the line 'button[j].setIcon(...);' after the line 'button[j] = new JButton(&quot;Hi&quot;);' and it should work.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top