problem with setIcon()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerico
    New Member
    • Sep 2006
    • 39

    problem with setIcon()

    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 NullPointerExce ption is generated.For example,

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

    Thanks for any help.

    jerico
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by jerico
    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 NullPointerExce ption is generated.For example,

    private JButton c1;
    JButton c1 = new JButton();
    Icon bug = new ImageIcon("imag e/coms1.gif");//this image is for up
    Icon bug3= new ImageIcon("imag e/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

    Code:
     private JButton c1; 
    JButton c1 = new JButton();
    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.

    Comment

    • jerico
      New Member
      • Sep 2006
      • 39

      #3
      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 GridBagConstrai nts 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(g b);
      gbc= new GridBagConstrai nts();
      Icon bug = new ImageIcon("imag e/coms1.GIF");
      Icon bug1 = new ImageIcon("imag e/hor.gif");
      Icon bug2= new ImageIcon("imag e/ver.gif");
      Icon bug3= new ImageIcon("imag e/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("AGE NT(38)");
      c2.setText("AGE NT(37)");
      c3.setText("NMS (42)");
      c4.setText("AGE NT(33)");
      c5.setText("AGE NT(47)");

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

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

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

      c1.setIconTextG ap(0);
      c2.setIconTextG ap(0);
      c3.setIconTextG ap(0);
      c4.setIconTextG ap(0);
      c5.setIconTextG ap(0);

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

      l.setBorderPain ted ( false );
      // suppress button press decoration
      c1.setContentAr eaFilled( false );
      c2.setContentAr eaFilled( false );
      c3.setContentAr eaFilled( false );
      c4.setContentAr eaFilled( false );
      c5.setContentAr eaFilled( false );

      l.setContentAre aFilled( false );

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

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

      l.setFocusPaint ed( false );

      c1.setHorizonta lTextPosition( SwingConstants. CENTER );
      c1.setVerticalT extPosition( SwingConstants. TOP );

      c2.setVerticalT extPosition(Swi ngConstants.BOT TOM);
      c2.setHorizonta lTextPosition(S wingConstants.L EFT);

      c3.setHorizonta lTextPosition( SwingConstants. CENTER );
      c3.setVerticalT extPosition( SwingConstants. BOTTOM );

      c4.setVerticalT extPosition(Swi ngConstants.BOT TOM);
      c4.setHorizonta lTextPosition(S wingConstants.R IGHT);

      c5.setHorizonta lTextPosition( SwingConstants. CENTER );
      c5.setVerticalT extPosition( SwingConstants. BOTTOM );

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

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

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

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

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

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

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

      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(c1 ,2,3,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL;
      addComponent(c2 ,4,1,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(c3 ,4,3,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(c4 ,4,5,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(c5 ,6,3,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(l1 ,3,3,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(l2 ,4,2,1,1);
      gbc.fill = GridBagConstrai nts.HORIZONTAL ;
      addComponent(l3 ,4,4,1,1);
      gbc.fill = GridBagConstrai nts.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.nex tInt())%4;//item.length;
      m = give(item[i]);
      if(m.equals("1" ))
      {

      if(i== 1)
      {

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

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

      }
      }
      }
      try
      {
      Thread.sleep(10 00);
      }
      catch(Interrupt edException 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.setConstrain ts(c,gbc);
      con.add(c);
      }

      public static void main (String args[])
      {
      my app = new my();
      app.setVisible( true);
      app.addWindowLi stener(
      new WindowAdapter()
      {
      public void windowClosing( WindowEvent e)
      {
      System.exit(0);
      }
      }
      ) ;
      }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by jerico
        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 GridBagConstrai nts 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(g b);
        gbc= new GridBagConstrai nts();
        Icon bug = new ImageIcon("imag e/coms1.GIF");
        Icon bug1 = new ImageIcon("imag e/hor.gif");
        Icon bug2= new ImageIcon("imag e/ver.gif");
        Icon bug3= new ImageIcon("imag e/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("AGE NT(38)");
        c2.setText("AGE NT(37)");
        c3.setText("NMS (42)");
        c4.setText("AGE NT(33)");
        c5.setText("AGE NT(47)");

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

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

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

        c1.setIconTextG ap(0);
        c2.setIconTextG ap(0);
        c3.setIconTextG ap(0);
        c4.setIconTextG ap(0);
        c5.setIconTextG ap(0);

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

        l.setBorderPain ted ( false );
        // suppress button press decoration
        c1.setContentAr eaFilled( false );
        c2.setContentAr eaFilled( false );
        c3.setContentAr eaFilled( false );
        c4.setContentAr eaFilled( false );
        c5.setContentAr eaFilled( false );

        l.setContentAre aFilled( false );

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

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

        l.setFocusPaint ed( false );

        c1.setHorizonta lTextPosition( SwingConstants. CENTER );
        c1.setVerticalT extPosition( SwingConstants. TOP );

        c2.setVerticalT extPosition(Swi ngConstants.BOT TOM);
        c2.setHorizonta lTextPosition(S wingConstants.L EFT);

        c3.setHorizonta lTextPosition( SwingConstants. CENTER );
        c3.setVerticalT extPosition( SwingConstants. BOTTOM );

        c4.setVerticalT extPosition(Swi ngConstants.BOT TOM);
        c4.setHorizonta lTextPosition(S wingConstants.R IGHT);

        c5.setHorizonta lTextPosition( SwingConstants. CENTER );
        c5.setVerticalT extPosition( SwingConstants. BOTTOM );

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

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

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

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

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

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

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

        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(c1 ,2,3,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL;
        addComponent(c2 ,4,1,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(c3 ,4,3,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(c4 ,4,5,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(c5 ,6,3,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(l1 ,3,3,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(l2 ,4,2,1,1);
        gbc.fill = GridBagConstrai nts.HORIZONTAL ;
        addComponent(l3 ,4,4,1,1);
        gbc.fill = GridBagConstrai nts.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.nex tInt())%4;//item.length;
        m = give(item[i]);
        if(m.equals("1" ))
        {

        if(i== 1)
        {

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

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

        }
        }
        }
        try
        {
        Thread.sleep(10 00);
        }
        catch(Interrupt edException 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.setConstrain ts(c,gbc);
        con.add(c);
        }

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

        Then
        Code:
         JButton c1 = new JButton(); 
        JButton c2 = new JButton();
        JButton c3 = new JButton();
        JButton c4 = new JButton();
        JButton c5 = new JButton();
        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
        Code:
         c1 = new JButton(); 
        c2 = new JButton();
        c3 = new JButton();
        c4 = new JButton();
        c5 = new JButton();
        Inside the constructor.

        Comment

        • jerico
          New Member
          • Sep 2006
          • 39

          #5
          Thanks sir.You have saved me.

          Jerico

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by jerico
            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".

            Comment

            Working...