Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default iamge icon where to put it

    where should the image file go (in Netbeans) in order to appear?

    bt = new JButton("" + room); // room number in int
    bt.setIcon(new ImageIcon("keyboard.png"));

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iamge icon where to put it

    Check out the JDK API for JButton and AbstractButton. You can set an icon via the constructor. But there are also different types of icons for Jbuttons. Also, please note the following:

    Java Code:
    int v = 203;
    String nStr = v + "";  // perhaps convenient but relatively inefficient
    
    nStr = Integer.toString(v);  // very efficient - otherwise, why would they provide it.
    // or
    nStr = String.valueOf(v);  // which actually calls the previous method.

    Regards,
    Jim
    Last edited by jim829; 03-16-2013 at 04:55 PM. Reason: YAT--Yet Another Typo
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    There are 100 study rooms for students
    the student select the rooms and the buttons I create like this:

    Java Code:
     public JPanel CreateButtons() {
            
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    bt = new JButton("" + room);
                    bt.setIcon(new ImageIcon("keyboard.png"));
                    buttonpanel.add(bt);
                    bt.addActionListener(buttonlistener);
                }
            }
    
            return buttonpanel;
        }
    
         public class ButtonListener implements ActionListener {
           public void actionPerformed(ActionEvent e)
          {
             roomnumber = e.getActionCommand();
             System.out.println(" room number " + roomnumber);
    
          }
       }
    Last edited by willemjav; 03-15-2013 at 11:35 PM.

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    But Jim the icons do not appear (same when set through the constructor string/ icon).
    Does the icon file goes into the same folder as the class code?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iamge icon where to put it

    The icon can be anywhere as long as you know the path name when creating it. However, your code above is still not setting an icon in the JButton. You are assigning text. You need to set the icon *after* you assign the JButton to bt. And in this case you will be assigning the same icon to all the buttons.

    Regards,
    Jim





    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    Jim in the above code this sets the icon
    bt.setIcon(new ImageIcon("keyboard.png"));
    right the same icon for different rooms!
    but the icon does not show up....

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    after room eg 329 has been pressed, how do I set that one to disable?

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iamge icon where to put it

    Is there any more of your code? You need to create a JFrame and add the button panel to it. You also need a static main entry point. I need all your code so I can see the problem.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    Java Code:
    package roomassigment;
    
    import javax.swing.*;
    
    /**
     *
     * @author willem
     */
    public class RoomAssigment  {
        public static void main(String[] args)
       {
          ButtonPanel frame = new ButtonPanel();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
       }
    }


    Java Code:
    package roomassigment;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    /**
     *
     * @author willem
     */
    public class ButtonPanel extends JFrame {
    
        private String roomnumber;
        JFrame frame;
        JPanel panel, FastSearchPanel, buttonpanel;
        JButton bt;
        ImageIcon icon;
    
        ActionListener buttonlistener = new  ButtonListener();
    
    
    
        public ButtonPanel() {
          setTitle("Roomwindow");
          setSize(800, 600);
          panel = new JPanel(new BorderLayout());
          buttonpanel = new JPanel();
          FastSearchPanel = new JPanel();
          buttonpanel.setLayout(new GridLayout(10,10));
    
          
          panel.add(CreateButtons(), BorderLayout.CENTER);
          panel.add(FastSearchPanel, BorderLayout.NORTH);
          // add panel to frame
    
          Container contentPane = getContentPane();
          contentPane.add(panel);
        //  frame.add(CreateButtons());
       }
    
      
    
    
    
    
        public JPanel CreateButtons() {
            
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    bt = new JButton("" + room, new ImageIcon("keyboard.png"));
                    bt.setHorizontalTextPosition( SwingConstants.LEFT );
                    buttonpanel.add(bt);
                    bt.addActionListener(buttonlistener);
                }
            }
    
            return buttonpanel;
        }
    
         public class ButtonListener implements ActionListener {
           public void actionPerformed(ActionEvent e)
          {
             roomnumber = e.getActionCommand();
             System.out.println(" room number " + roomnumber);
             bt.setEnabled(false);
             
          }
       }
    
    }

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    jim
    1) icons not appearing (is in the same folder as the code)
    2) how to setEnabled(false); a particular button?

    something like getJButton.bt(321).setEnabled(false).......

  11. #11
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    the things is this when three buttons like this are created

    bt1 = new JButton("1");
    bt2 = new JButton("2");
    bt3 = new JButton("3");

    I could say bt2.setEnabled(false);

    But what happens here
    for (int i = 1; i <4; i++) {
    bt = new JButton("" + i);
    }
    how can I now disable number 2?

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    I tried this one e.getSource().setEnabled(false);
    but did not work unfortunatily

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iamge icon where to put it

    Ok, I got your code to work. I have a whole panel of buttons.

    Try this.
    Java Code:
     class ButtonListener implements ActionListener {
          public void actionPerformed(ActionEvent e)
         {
            roomnumber = e.getActionCommand();
            System.out.println(" room number " + roomnumber);
            JButton bt = (JButton)e.getSource();
            bt.setEnabled(false);
             
         }
       }

    Once you disable the button, the actionListener won't respond. So you may want to save all the Button instances in an array so you can enable them later. It all depends on what you want to do.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    Yes after disabling the button I need to enable it again, so here is what I did Jim,
    it should work but getting a (bloody) nullpointer again: so something is wrong:

    RoomInfo[] roominfo;

    roominfo = new RoomInfo[530]; // at the constructor and the index is the roomnumber
    (which means many empty cells)

    Java Code:
    public class RoomInfo  { // a class/ object because there will be more fields
           
           private JButton roombutton;
      
    
           public void setRoomButton(JButton jb)  {
               roombutton = jb;
           }
           public JButton getRoomButton()  {
               return roombutton;
           }
    }
    
     public JPanel CreateButtons() {
            int arraycount= 0;
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                   // roominfo[room].setRoomNumber(room);
    
                    bt = new JButton("" + room);
                    roominfo[room].setRoomButton(bt); HERE I GET NULLPOINTER
    
                    buttonpanel.add(bt);
                    bt.addActionListener(buttonlistener);
                }
            }
    
            return buttonpanel;
        }
    
         public class ButtonListener implements ActionListener {
           public void actionPerformed(ActionEvent e)
          {
             int rn = Integer.parseInt(e.getActionCommand());
             System.out.println(" room number " + roomnumber);
    
             roominfo[rn].getRoomButton().setEnabled(false);
             
          }
       }

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    I believed that I got the array initialized by doing this:
    RoomInfo[] roominfo;
    roominfo = new RoomInfo[530]; // at the constructor and the index is the roomnumber
    (which means many empty cells I will get around that next)

    And next you just assign to it like this:
    bt = new JButton("" + room);
    roominfo[room].setRoomButton(bt);


    but maybe not?
    Last edited by willemjav; 03-16-2013 at 10:49 AM.

  16. #16
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    What i did is that I initialized an array of a class
    but should still initialize each class of each array cell, right?

  17. #17
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    Stupid: I needed to initialize
    initialize like this:


    for (int i = 0; i < roominfo.length; i++)
    roominfo[i] = new RoomInfo();

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: iamge icon where to put it

    Your Roominfo array will work. However, I recommend you look at ArrayList in the JDK Javadoc. You can declare it like this:
    Java Code:
    ArrayList<RoomInfo> roominfo = new ArrayList<RoomIinfo>();
    It grows dynamically to accomodate new elements. Then you can use roominfo.add() and roominfo.get() methods to add an entry or get a specific one. You can add a button to the list as you create them.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    Thanks jim I know but the study rooms are fixed!
    Here is what I came up with, it works but it might be more simple?
    (I like simple programs)


    Java Code:
    public JPanel CreateButtons() {
            int arraycount= 0;
            for (int i = 3; i <6; i++) {
                for (int ii = 0; ii <=29; ii++) {
                    int room = (i*100)+ii;
                    roominfo[arraycount].setRoomNumber(room);
                    
                    bt = new JButton("" + room);
                    roominfo[arraycount].setRoomButton(bt);
                    arraycount++;
                    bt.setHorizontalTextPosition( SwingConstants.LEFT );
                    buttonpanel.add(bt);
                    bt.addActionListener(buttonlistener);
                }
            }
    
            return buttonpanel;
        }
    
         public class ButtonListener implements ActionListener {
             
           public void actionPerformed(ActionEvent e)
          {
               int arraynumber = 0;
               int rn = Integer.parseInt(e.getActionCommand()); //button label 
               for (int i = 0; i < roominfo.length; i++) {   // which is roomnumber
                       if (roominfo[i].getRoomNumber() == rn)       
                       arraynumber = i;
               }                                      // gets the index of the room
               roominfo[arraynumber].getRoomButton().setEnabled(false);
               
               System.out.println(" room number " + rn);
               System.out.println(" array number " + arraynumber );
               System.out.println(" array number room " +
                       roominfo[arraynumber].getRoomNumber() );
             
          }
       }
       public class RoomInfo  {
           private int roomnumber;
           private JButton roombutton;
           private String studentname;
           private boolean piano;
           private long begintime, duration;
    
           public void setRoomNumber(int rm)  {
               roomnumber = rm;
           }
           public int getRoomNumber()  {
               return roomnumber;
           }
    
           public void setRoomButton(JButton jb)  {
               roombutton = jb;
           }
           public JButton getRoomButton()  {
               return roombutton;
           }
    
            public void setStudentName(String sn)  {
               studentname = sn;
           }
           public String getStudentName()  {
               return studentname;
           }
    
            public void setPiano(Boolean pf)  {
               piano = pf;
           }
           public Boolean getPiano()  {
               return piano;
           }
    
       }

  20. #20
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: iamge icon where to put it

    You see the room numbers have their own logics
    The array is now very compact but the problem is
    that the room number does not coincide with the array index!

Page 1 of 2 12 LastLast

Similar Threads

  1. icon does not change when I want it too
    By drewtrcy in forum New To Java
    Replies: 1
    Last Post: 05-05-2011, 05:10 PM
  2. Adding another icon to an icon
    By whateverme in forum New To Java
    Replies: 11
    Last Post: 03-29-2011, 08:00 PM
  3. how to set application icon??
    By chyeeqi in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 03:43 AM
  4. set.Icon
    By safiya in forum NetBeans
    Replies: 2
    Last Post: 10-17-2008, 06:46 PM
  5. icon
    By amith in forum AWT / Swing
    Replies: 1
    Last Post: 05-16-2008, 07:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •