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:)
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(" ");
}
}
}
Re: the icon won't change on clicking
Are you sure that you are reading the image file ok?
Re: the icon won't change on clicking
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?
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 .:)
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 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();
}
}
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;
}
}
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);
}
});
}
}
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.
Re: the icon won't change on clicking
ok im going to explain the editor class what should it do according to my knowledge
Re: the icon won't change on clicking
Quote:
{
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 ?
Re: the icon won't change on clicking
see the database_save and databse_edit are the two icons .this is the difference
Quote:
icon=new ImageIcon((getClass().getResource("/javaapplication9/database_save.png")));
icon=new ImageIcon((getClass().getResource("/javaapplication9/database_edit.png")));
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.
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
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();
}
}
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.
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
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
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.
Re: the icon won't change on clicking
sorry i do not understand what are u trying to say?please elaborate
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.
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
Re: the icon won't change on clicking
i also tried by using seperat seticon() every where Quote:
button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/javaapplication9/database_edit.png")));