Page 1 of 2 12 LastLast
Results 1 to 20 of 37
Like Tree6Likes

Thread: the icon won't change on clicking

  1. #1
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default the icon won't change on clicking

    hi all
    i have to change the image icon of the particular button in jable when i click on it. and have to rechange its icon and text when i click on it again .
    so far m able to change its text when i click on it . but the icon wont change along with the text. comment will appreciated:)
    Java Code:
    /*
    
    
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author Administrator
     */
    public class EditButtonEditor extends DefaultCellEditor {
    
        protected JButton button;
        private String label;
        private boolean isPushed;
        Icon icon;
    
        public EditButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton();
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("ouch");
                    if(isPushed)
    {
    if(button.getText().equals("Edit"))
    {
    label="Update";
    //icon=new ImageIcon((getClass().getResource("/com/company/librarySystem/bm/ui/database_save.png")));
    button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/company/librarySystem/bm/ui/database_save.png")));
    //button.setIcon(new javax.swing.ImageIcon("/com/company/librarySystem/bm/ui/database_update.png"));
    
    System.out.println("converted to update");
    }
    else
    {
    label="Edit";
    System.out.println("converted to edit");
    //icon=new ImageIcon((getClass().getResource("/com/company/librarySystem/bm/ui/database_edit.png")));
    button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/company/librarySystem/bm/ui/database_edit.png")));
    //button.setIcon(new ImageIcon("/com/company/librarySystem/bm/ui/database_edit.png"));
    
    }
    }
    
                    fireEditingStopped();
                }
            });
        }
    
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            buttonPressed(table, row, column);
            if (isSelected) {
                table.repaint();
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } else {
                button.setForeground(table.getForeground());
                button.setBackground(table.getBackground());
            }
            label = (value == null) ? "" : value.toString();
            button.setText(label);
           button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/company/librarySystem/bm/ui/database_edit.png")));
           // button.setIcon(new ImageIcon("/com/company/librarySystem/bm/ui/database_edit.png"));
           icon=new ImageIcon((getClass().getResource("/com/company/librarySystem/bm/ui/database_edit.png")));
           // button.setIcon(icon);
            isPushed = true;
            return button;
        }
    
        public Object getCellEditorValue() {
           //button.setIcon(icon);
            return new String(label);
    
    
        }
    
        public boolean stopCellEditing() {
            isPushed = false;
            return super.stopCellEditing();
        }
    
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }
    
        private void buttonPressed(JTable table, int row, int column) {
           
            if (column == 3) {
                NewJFrame frame = (NewJFrame) SwingUtilities.getWindowAncestor(table);
                frame.deleteButtonClicked(row, column);
            } else {
                System.out.println("   ");
            }
        }
        
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    Are you sure that you are reading the image file ok?

  3. #3
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    yes i am sure .

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    Do you have a small testing version of your code that compiles, executes and demonstrates the problem that you can post?

  5. #5
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    no that is the problem i do not know how to make SSCCE. any ways i try to make a small code .:)

  6. #6
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    i picked this code from net n edit it so that i can explain my problem in simple way. in this code . just one constant image is appering which is used in button renderer but its not shuffling with the that button text where as i did code so that it should change with text here is my code
    Java Code:
     import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author Administrator
     */
    public class ButtonEditor extends DefaultCellEditor {
    
        protected JButton button;
        private String label;
        private boolean isPushed;
        Icon icon;
    
        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton();
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("ouch");
                    if(isPushed)
    {
    if(button.getText().equals("Edit"))
    {
    label="Update";
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));
    
    
    System.out.println("converted to update");
    }
    else
    {
    label="Edit";
    System.out.println("converted to edit");
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
    
    
    }
    }
    
                    fireEditingStopped();
                }
            });
        }
    
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
           
            if (isSelected) {
                table.repaint();
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } else {
                button.setForeground(table.getForeground());
                button.setBackground(table.getBackground());
            }
            label = (value == null) ? "" : value.toString();
            button.setText(label);
          // button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication9/database_edit.png")));
           // button.setIcon(new ImageIcon("/javaapplication9/database_edit.png"));
            icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
           // button.setIcon(icon);
            isPushed = true;
            return button;
        }
    
        public Object getCellEditorValue() {
           //button.setIcon(icon);
            return new String(label);
    
    
        }
    
        public boolean stopCellEditing() {
            isPushed = false;
            return super.stopCellEditing();
        }
    
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }
    
       
        
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
     
    /**
     * @version 1.0 11/09/98
     */
    public class ButtonRenderer extends JButton implements TableCellRenderer {
     
      
      
     public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (isSelected) {
          setForeground(table.getSelectionForeground());
          setBackground(table.getSelectionBackground());
        } else {
          setForeground(table.getForeground());
          setBackground(UIManager.getColor("Button.background"));
        }
          //  setIcon(new ImageIcon("/javaapplication9/database_edit.png"));
             String label = (value ==null) ? "" : value.toString();
        setText( label );
        setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication9/database_edit.png")));
            
            return this;
        }
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
     
    /**
     * @version 1.0 11/09/98
     */
    public class JButtonTableExample extends JFrame {
     
      public JButtonTableExample(){
        super( "JButtonTable Example" );
        
        DefaultTableModel dm = new DefaultTableModel();
        dm.setDataVector(new Object[][]{{"Edit","foo"},
                                        {"Edit","bar"}},
                         new Object[]{"Edit","String"});
                         
        JTable table = new JTable(dm);
        table.getColumn("Edit").setCellRenderer(new ButtonRenderer());
        table.getColumn("Edit").setCellEditor(new ButtonEditor(new JCheckBox()));
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add( scroll );
        setSize( 400, 100 );
        setVisible(true);
      }
     
      public static void main(String[] args) {
        JButtonTableExample frame = new JButtonTableExample();
        frame.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
      }
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    What is supposed to happen when I execute the code.
    I see that the variable icon is set many times but never used.
    I see that the setIcon() method always uses the same value.

  8. #8
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    ok im going to explain the editor class what should it do according to my knowledge

  9. #9
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    {
    if(button.getText().equals("Edit"))
    {
    label="Update";
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));


    System.out.println("converted to update");
    }
    it should change the button's text and icon . text is being changed but not the icon. am i right ?

  10. #10
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    see the database_save and databse_edit are the two icons .this is the difference
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    Yes the icon does not change.
    I don't see any code that will change the icon. The setIcon code is commented out.
    rajasohaibmaroof likes this.

  12. #12
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    oh sorry its a mistake when i was trying different techniques. but it still does not work i tried every possible way that why i was commenting it agian to check some other thing . here is editor class. it still does not work
    Java Code:
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.DefaultCellEditor;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    
    /**
     *
     * @author Administrator
     */
    public class ButtonEditor extends DefaultCellEditor {
    
        protected JButton button;
        private String label;
        private boolean isPushed;
        Icon icon;
    
        public ButtonEditor(JCheckBox checkBox) {
            super(checkBox);
            button = new JButton();
            button.setOpaque(true);
            button.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("ouch");
                    if(isPushed)
    {
    if(button.getText().equals("Edit"))
    {
    label="Update";
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));
    
    
    System.out.println("converted to update");
    }
    else
    {
    label="Edit";
    System.out.println("converted to edit");
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
    
    
    }
    }
    
                    fireEditingStopped();
                }
            });
        }
    
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
           
            if (isSelected) {
                table.repaint();
                button.setForeground(table.getSelectionForeground());
                button.setBackground(table.getSelectionBackground());
            } else {
                button.setForeground(table.getForeground());
                button.setBackground(table.getBackground());
            }
            label = (value == null) ? "" : value.toString();
            button.setText(label);
          // button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication9/database_edit.png")));
           // button.setIcon(new ImageIcon("/javaapplication9/database_edit.png"));
            icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
            button.setIcon(icon);
            isPushed = true;
            return button;
        }
    
        public Object getCellEditorValue() {
           button.setIcon(icon);
            return new String(label);
    
    
        }
    
        public boolean stopCellEditing() {
            isPushed = false;
            return super.stopCellEditing();
        }
    
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }
    
       
        
    }

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    I see that a different image is displayed when I keep the button pressed.
    It goes back to the original image when the button is released.
    rajasohaibmaroof likes this.

  14. #14
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    yes yes exactly i want it to keep the icon even when i release the button . i it should be changed when i click on the button again

  15. #15
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    but here it just displays the edit icon when i keep the button pressed not even save icon

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    By adding a new method to change the icon's value that is used in the setIcon() call, I can get the image on the button to change.
    rajasohaibmaroof likes this.

  17. #17
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    sorry i do not understand what are u trying to say?please elaborate

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: the icon won't change on clicking

    The one and only setIcon() call ALWAYS uses the same icon image. You need a way to change that value.
    rajasohaibmaroof likes this.

  19. #19
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    but i am unable to figured that way out! that's the problem i am in learning process. please help me out

  20. #20
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    i also tried by using seperat seticon() every where
    button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication9/database_edit.png")));

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. change icon of frame?
    By Manish87 in forum AWT / Swing
    Replies: 9
    Last Post: 04-15-2011, 04:11 AM
  3. Swing: Change the icon on a JComponent
    By xytho33 in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2010, 06:53 AM
  4. Replies: 3
    Last Post: 06-27-2010, 02:57 AM
  5. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM

Posting Permissions

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