473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem with setIcon()

39
Hi,
I am developing an application which needs to dynamically show whether a host is up or down.I have used buttons on which i am pasting the icon indicating up or down.So, when a node is down, I am replacing the icon that indicates up with the icon that indicates down using setIcon() function.But instead of changing the icon a NullPointerException is generated.For example,

private JButton c1;
JButton c1 = new JButton();
Icon bug = new ImageIcon("image/coms1.gif");//this image is for up
Icon bug3= new ImageIcon("image/computer.gif");//for down
c1.setIcon(bug);
if(node is down)
c1.setIcon(bug3);

Thanks for any help.

jerico
Nov 13 '06 #1
5 12746
r035198x
13,262 8TB
Hi,
I am developing an application which needs to dynamically show whether a host is up or down.I have used buttons on which i am pasting the icon indicating up or down.So, when a node is down, I am replacing the icon that indicates up with the icon that indicates down using setIcon() function.But instead of changing the icon a NullPointerException is generated.For example,

private JButton c1;
JButton c1 = new JButton();
Icon bug = new ImageIcon("image/coms1.gif");//this image is for up
Icon bug3= new ImageIcon("image/computer.gif");//for down
c1.setIcon(bug);
if(node is down)
c1.setIcon(bug3);

Thanks for any help.

jerico
There is nothing wrong with seIcon. The JButton c1 is the one that is null at the point of setting the icon. You have

Expand|Select|Wrap|Line Numbers
  1.  private JButton c1; 
  2. JButton c1 = new JButton();
  3.  
which does not compile like that so I'm assuming you have these at different places in your code. Post the code and let's see what's null.
Nov 13 '06 #2
jerico
39
Hi,this is the program containing the problem.Sorry, I could not make the indentation proper.

jerico

public class my extends JFrame implements Runnable
{

private Container con ;
private GridBagLayout gb ;
private GridBagConstraints gbc;
private Icon bug,bug1,bug3;
private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
Thread runner ;
Random rg= new Random();
msconnect ms = new msconnect();
String[] item = ms.sel();
public my()
{
super("NETWOK LOOKUP");
con = getContentPane();
gb = new GridBagLayout();
con.setLayout(gb);
gbc= new GridBagConstraints();
Icon bug = new ImageIcon("image/coms1.GIF");
Icon bug1 = new ImageIcon("image/hor.gif");
Icon bug2= new ImageIcon("image/ver.gif");
Icon bug3= new ImageIcon("image/computer.gif");

JButton c1 = new JButton();
JButton c2 = new JButton();
JButton c3 = new JButton();
JButton c4 = new JButton();
JButton c5 = new JButton();
c1.setIcon(bug);
c2.setIcon(bug);
c3.setIcon(bug);
c4.setIcon(bug);
c5.setIcon(bug);
c1.setText("AGENT(38)");
c2.setText("AGENT(37)");
c3.setText("NMS(42)");
c4.setText("AGENT(33)");
c5.setText("AGENT(47)");

JButton l= new JButton(new ImageIcon("image/welcomew.gif"));

//minmize the size of a button
c1.setMargin(new Insets(0,0,0,0));
c2.setMargin(new Insets(0,0,0,0));
c3.setMargin(new Insets(0,0,0,0));
c4.setMargin(new Insets(0,0,0,0));
c5.setMargin(new Insets(0,0,0,0));

l.setMargin(new Insets(0,0,0,0));

c1.setIconTextGap(0);
c2.setIconTextGap(0);
c3.setIconTextGap(0);
c4.setIconTextGap(0);
c5.setIconTextGap(0);

// suppress borders
c1.setBorderPainted ( false );
c2.setBorderPainted ( false );
c3.setBorderPainted ( false );
c4.setBorderPainted ( false );
c5.setBorderPainted ( false );

l.setBorderPainted ( false );
// suppress button press decoration
c1.setContentAreaFilled( false );
c2.setContentAreaFilled( false );
c3.setContentAreaFilled( false );
c4.setContentAreaFilled( false );
c5.setContentAreaFilled( false );

l.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
c1.setDefaultCapable( false );
c2.setDefaultCapable( false );
c3.setDefaultCapable( false );
c4.setDefaultCapable( false );
c5.setDefaultCapable( false );
l.setDefaultCapable( false );

// hide focus rectangle
c1.setFocusPainted( false );
c2.setFocusPainted( false );
c3.setFocusPainted( false );
c4.setFocusPainted( false );
c5.setFocusPainted( false );

l.setFocusPainted( false );

c1.setHorizontalTextPosition( SwingConstants.CENTER );
c1.setVerticalTextPosition( SwingConstants.TOP );

c2.setVerticalTextPosition(SwingConstants.BOTTOM);
c2.setHorizontalTextPosition(SwingConstants.LEFT);

c3.setHorizontalTextPosition( SwingConstants.CENTER );
c3.setVerticalTextPosition( SwingConstants.BOTTOM );

c4.setVerticalTextPosition(SwingConstants.BOTTOM);
c4.setHorizontalTextPosition(SwingConstants.RIGHT) ;

c5.setHorizontalTextPosition( SwingConstants.CENTER );
c5.setVerticalTextPosition( SwingConstants.BOTTOM );

JButton l1 = new JButton(bug2);
JButton l2 = new JButton(bug1);
JButton l3 = new JButton(bug1);
JButton l4 = new JButton(bug2);

l1.setMargin(new Insets(0,0,0,0));
l2.setMargin(new Insets(0,0,0,0));
l3.setMargin(new Insets(0,0,0,0));
l4.setMargin(new Insets(0,0,0,0));

// suppress borders
l1.setBorderPainted ( false );
l2.setBorderPainted ( false );
l3.setBorderPainted ( false );
l4.setBorderPainted ( false );

// suppress button press decoration
l1.setContentAreaFilled( false );
l2.setContentAreaFilled( false );
l3.setContentAreaFilled( false );
l4.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
l1.setDefaultCapable( false );
l2.setDefaultCapable( false );
l3.setDefaultCapable( false );
l4.setDefaultCapable( false );

// hide focus rectangle
l1.setFocusPainted( false );
l2.setFocusPainted( false );
l3.setFocusPainted( false );
l4.setFocusPainted( false );

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l,0,0,0,2);

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c1,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(c2,4,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c3,4,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c4,4,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c5,6,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l1,3,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l2,4,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l3,4,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l4,5,3,1,1);
setLocation(200,170);
show();*/
runner = new Thread(this);
runner.start();

}

public void run()
{
String m="" ;
while(true)
{
int i= Math.abs(rg.nextInt())%4;//item.length;
m = give(item[i]);
if(m.equals("1"))
{

if(i== 1)
{

c1.setIcon(new ImageIcon("image/computer.gif"));

}
if(i== 3)
{
c5.setIcon(bug3);

}
}
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{ }

}
private void addComponent( Component c , int r , int col , int w , int h)
{
gbc.gridx = col ;
gbc.gridy = r;
gbc.gridwidth = w;
gbc.gridheight = h;
gb.setConstraints(c,gbc);
con.add(c);
}

public static void main (String args[])
{
my app = new my();
app.setVisible(true);
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
}
) ;
}
}
Nov 13 '06 #3
r035198x
13,262 8TB
Hi,this is the program containing the problem.Sorry, I could not make the indentation proper.

jerico

public class my extends JFrame implements Runnable
{

private Container con ;
private GridBagLayout gb ;
private GridBagConstraints gbc;
private Icon bug,bug1,bug3;
private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
Thread runner ;
Random rg= new Random();
msconnect ms = new msconnect();
String[] item = ms.sel();
public my()
{
super("NETWOK LOOKUP");
con = getContentPane();
gb = new GridBagLayout();
con.setLayout(gb);
gbc= new GridBagConstraints();
Icon bug = new ImageIcon("image/coms1.GIF");
Icon bug1 = new ImageIcon("image/hor.gif");
Icon bug2= new ImageIcon("image/ver.gif");
Icon bug3= new ImageIcon("image/computer.gif");

JButton c1 = new JButton();
JButton c2 = new JButton();
JButton c3 = new JButton();
JButton c4 = new JButton();
JButton c5 = new JButton();
c1.setIcon(bug);
c2.setIcon(bug);
c3.setIcon(bug);
c4.setIcon(bug);
c5.setIcon(bug);
c1.setText("AGENT(38)");
c2.setText("AGENT(37)");
c3.setText("NMS(42)");
c4.setText("AGENT(33)");
c5.setText("AGENT(47)");

JButton l= new JButton(new ImageIcon("image/welcomew.gif"));

//minmize the size of a button
c1.setMargin(new Insets(0,0,0,0));
c2.setMargin(new Insets(0,0,0,0));
c3.setMargin(new Insets(0,0,0,0));
c4.setMargin(new Insets(0,0,0,0));
c5.setMargin(new Insets(0,0,0,0));

l.setMargin(new Insets(0,0,0,0));

c1.setIconTextGap(0);
c2.setIconTextGap(0);
c3.setIconTextGap(0);
c4.setIconTextGap(0);
c5.setIconTextGap(0);

// suppress borders
c1.setBorderPainted ( false );
c2.setBorderPainted ( false );
c3.setBorderPainted ( false );
c4.setBorderPainted ( false );
c5.setBorderPainted ( false );

l.setBorderPainted ( false );
// suppress button press decoration
c1.setContentAreaFilled( false );
c2.setContentAreaFilled( false );
c3.setContentAreaFilled( false );
c4.setContentAreaFilled( false );
c5.setContentAreaFilled( false );

l.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
c1.setDefaultCapable( false );
c2.setDefaultCapable( false );
c3.setDefaultCapable( false );
c4.setDefaultCapable( false );
c5.setDefaultCapable( false );
l.setDefaultCapable( false );

// hide focus rectangle
c1.setFocusPainted( false );
c2.setFocusPainted( false );
c3.setFocusPainted( false );
c4.setFocusPainted( false );
c5.setFocusPainted( false );

l.setFocusPainted( false );

c1.setHorizontalTextPosition( SwingConstants.CENTER );
c1.setVerticalTextPosition( SwingConstants.TOP );

c2.setVerticalTextPosition(SwingConstants.BOTTOM);
c2.setHorizontalTextPosition(SwingConstants.LEFT);

c3.setHorizontalTextPosition( SwingConstants.CENTER );
c3.setVerticalTextPosition( SwingConstants.BOTTOM );

c4.setVerticalTextPosition(SwingConstants.BOTTOM);
c4.setHorizontalTextPosition(SwingConstants.RIGHT) ;

c5.setHorizontalTextPosition( SwingConstants.CENTER );
c5.setVerticalTextPosition( SwingConstants.BOTTOM );

JButton l1 = new JButton(bug2);
JButton l2 = new JButton(bug1);
JButton l3 = new JButton(bug1);
JButton l4 = new JButton(bug2);

l1.setMargin(new Insets(0,0,0,0));
l2.setMargin(new Insets(0,0,0,0));
l3.setMargin(new Insets(0,0,0,0));
l4.setMargin(new Insets(0,0,0,0));

// suppress borders
l1.setBorderPainted ( false );
l2.setBorderPainted ( false );
l3.setBorderPainted ( false );
l4.setBorderPainted ( false );

// suppress button press decoration
l1.setContentAreaFilled( false );
l2.setContentAreaFilled( false );
l3.setContentAreaFilled( false );
l4.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
l1.setDefaultCapable( false );
l2.setDefaultCapable( false );
l3.setDefaultCapable( false );
l4.setDefaultCapable( false );

// hide focus rectangle
l1.setFocusPainted( false );
l2.setFocusPainted( false );
l3.setFocusPainted( false );
l4.setFocusPainted( false );

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l,0,0,0,2);

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c1,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(c2,4,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c3,4,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c4,4,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c5,6,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l1,3,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l2,4,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l3,4,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l4,5,3,1,1);
setLocation(200,170);
show();*/
runner = new Thread(this);
runner.start();

}

public void run()
{
String m="" ;
while(true)
{
int i= Math.abs(rg.nextInt())%4;//item.length;
m = give(item[i]);
if(m.equals("1"))
{

if(i== 1)
{

c1.setIcon(new ImageIcon("image/computer.gif"));

}
if(i== 3)
{
c5.setIcon(bug3);

}
}
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{ }

}
private void addComponent( Component c , int r , int col , int w , int h)
{
gbc.gridx = col ;
gbc.gridy = r;
gbc.gridwidth = w;
gbc.gridheight = h;
gb.setConstraints(c,gbc);
con.add(c);
}

public static void main (String args[])
{
my app = new my();
app.setVisible(true);
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
}
) ;
}
}
There you go
Expand|Select|Wrap|Line Numbers
  1.  
  2. private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
  3.  
Declares the buttons fine. They are global to the class and this is what you want to do.

Then
Expand|Select|Wrap|Line Numbers
  1.  JButton c1 = new JButton(); 
  2. JButton c2 = new JButton();
  3. JButton c3 = new JButton();
  4. JButton c4 = new JButton();
  5. JButton c5 = new JButton();
  6.  
is not what you want to do. This hides the previously defined buttons in the class. (eg) When you access c1 from outside the constructor, the global c1 is referenced and this has not yet been initialised because the one declared in the constructor is not visible outside the constructor. You should just do
Expand|Select|Wrap|Line Numbers
  1.  c1 = new JButton(); 
  2. c2 = new JButton();
  3. c3 = new JButton();
  4. c4 = new JButton();
  5. c5 = new JButton();
  6.  
Inside the constructor.
Nov 13 '06 #4
jerico
39
Thanks sir.You have saved me.

Jerico
Nov 14 '06 #5
r035198x
13,262 8TB
Thanks sir.You have saved me.

Jerico
don't you go about calling me "sir", what if I'm younger than you... And "saved" is an overstatement.
Call me anything you want but NOT "sir".
Nov 14 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: David | last post by:
Hello I'm writting an apllication and i like to display and offscreen image. However my code doesn't seem to work. It compiles and runs properly but What i want is to associate the button of the...
0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
14
by: Rajko | last post by:
I found possible bug with set icon. I'm not sure but I don't think this is supposed to work like this. Program uses large instead of small icons. Please tell me is it known bug or maybe I missed...
2
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, I have a valid handle to an icon, but when I use SetIcon, either MFC or ATL, the icon is not displayed. The window is a dialog and the same happens to a propertysheet window. How can I...
2
by: Egbert Nierop \(MVP for IIS\) | last post by:
Igor sorry for not being clear in my original question. I have a valid handle to an icon control (type = ICON) for a propertysheet, but when I use SetIcon, either MFC or ATL, the icon is not...
3
by: jmbernz | last post by:
I really need help with this one, I'm trying to set the app icon during run time. There is no problem when the app is not group together, it still have the set icon but when group together if you...
1
by: Neverhood | last post by:
Hi folks, I'm trying to make a small game about being a drug dealer, with a 10x8 grid JPanel. On top of that i have another 10x8 grid JPanel as glassPane for the player icon. I have all the...
5
by: wajedali | last post by:
Hi I am having trouble rendering a combo box with icon near the text. Below is a simple code which demonstrates my problem. import java.awt.Component; import java.util.HashMap; import...
34
by: soty | last post by:
PLEASE I NEED HELP ON THIS CODE. I'M TRYING TO WRITE A CODE ON VENDING MACHINE. BUT FIRST OF ALL I NEED TO PUT PICTURES ON THE BUTTONS THAT I'M USING. THE ThING IS I'M HAVING ERRORS WHEN...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.