Page 2 of 2 FirstFirst 12
Results 21 to 37 of 37
Like Tree6Likes

Thread: the icon won't change on clicking

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

    Default Re: the icon won't change on clicking

    You need a way to get the value of the icon from where you set it to where you use it.
    Add a setTheIcon method to the class where you use it
    call that method from where you set it.

    To get the reference to be able to call the setTheIcon method, pass a reference of the class with that method to the class that will call that method.
    rajasohaibmaroof likes this.

  2. #22
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    means that i should create 2 methods in buttonEditor class and in each method i should place differ icons and i should call them in here
    {
    if(button.getText().equals("Edit"))
    {
    label="Update";
    icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));


    System.out.println("converted to update");
    }

  3. #23
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: the icon won't change on clicking

    Please don't use quote tags where you should be using code tags.

    Note their difference:

    [quote] ... [/quote]

    [code] ... [/code]

  4. #24
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    sorry my mistake. should i do that? what i said above

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

    Default Re: the icon won't change on clicking

    I don't have anything more to add. I've given you the scheme I used.
    You can try that or come up with one of your own.

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

    Default Re: the icon won't change on clicking

    thats what i am saying i want to use your tried scheme can u please past the corrected code because i want to use that

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

    Default Re: the icon won't change on clicking

    Sorry, I can't post code for you.
    I'm leaving the details to you to work out.

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

    Default Re: the icon won't change on clicking

    ok thank you sir

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

    Default Re: the icon won't change on clicking

    i am not getting it can u please explain a little bit more i am going in this direction
    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")));
    setThesaveIcon();
    
    
    System.out.println("converted to update");
    }
    else
    {
    label="Edit";
    System.out.println("converted to edit");
    //icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
    setTheEditIcon();
    
    }
    }
    
                    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 void setTheEditIcon(){
            icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
        }
        public void setThesaveIcon(){
            icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));
        }
    
        public Object getCellEditorValue() {
           button.setIcon(icon);
            return new String(label);
    
    
        }
    
        public boolean stopCellEditing() {
            isPushed = false;
            return super.stopCellEditing();
        }
    
        protected void fireEditingStopped() {
            super.fireEditingStopped();
        }
    
       
        
    }
    :(

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

    Default Re: the icon won't change on clicking

    You don't need to get a new image for the icon every time you want to use it. You can get the icon's value one time and use it repeatedly after that.

    My solution is not working as I test it more.
    I think you have to use the value variable to decide what icon to use.

  11. #31
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    :( not getting it at all .where is the problem? :'(

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

    Default Re: the icon won't change on clicking

    There is a problem in my code. When the icon is changed, it is changed for all the buttons.
    I'm not sure where the logic needs to be. I think maybe in the getTableCellEditorComponent() method you should use the value variable to control what icon to use.

  13. #33
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: the icon won't change on clicking

    Where is you SSCCE?

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

    Default Re: the icon won't change on clicking

    here it is
    Quote Originally Posted by rajasohaibmaroof View Post
    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);
          }
        });
      }
    }

  15. #35
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: the icon won't change on clicking

    I think that you should mainly concentrate on changing your button's icon in its renderer, not its editor. For example:

    Java Code:
    class ButtonRenderer extends JButton implements TableCellRenderer {
       // TODO: Change the resource path Strings for your app:
       public static final String EDIT_IMAGE_RES = "/images/editImage.jpg";  // **** change this!!! ****
       public static final String UPDATE_IMAGE_RES = "/images/updateImage.jpg";  // **** change this!!! ****
       
       private ImageIcon editIcon;
       private ImageIcon updateIcon;
       
       
       public ButtonRenderer() {
          editIcon = new ImageIcon(getClass().getResource(EDIT_IMAGE_RES));
          updateIcon = new ImageIcon(getClass().getResource(UPDATE_IMAGE_RES));
       }
    
       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"));
          }
          String label = (value == null) ? "" : value.toString();
          setText(label);
          
          Icon icon = (label.equalsIgnoreCase("Edit")) ? editIcon : updateIcon;
          setIcon(icon);
    
          return this;
       }
    }
    rajasohaibmaroof likes this.

  16. #36
    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    0

    Default Re: the icon won't change on clicking

    thank you i will try this technique tomorrow because its mid night over here :) and i will tell u how it went

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

    Default Re: the icon won't change on clicking

    thank u thank u thanku alooooooooooooot it worked like a charm:)

Page 2 of 2 FirstFirst 12

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
  •