javaswingjtabletablecellrenderertablecelleditor

Executable Jar With Button setCellRenderer() not Working


I have a serious Problem. I am Using Jtable in which i added a buttons using this code.

    TableColumn buttonColumn = tableSupplier.getColumnModel().getColumn(8);
    TableButton buttons = new TableButton();
    buttons.addHandler(new TableButton.TableButtonPressedHandler() {
        public void onButtonPress(int row, int column) {
            try {
                saveImageInFolder();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Utility.getFormFieldValidator().showErrorMessage();
            }
        }
    });

    buttonColumn.setCellRenderer(buttons);
    buttonColumn.setCellEditor(buttons);

My Button Class Code is like this.

package org.chillies.validator;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import org.chillies.view.SupplierList;

public class TableButton extends AbstractCellEditor implements TableCellEditor,TableCellRenderer
{
private static final long serialVersionUID = 5647725208335645741L;

public interface TableButtonPressedHandler
    {
    /**
     * Called when the button is pressed.
     * @param row The row in which the button is in the table.
     * @param column The column the button is in in the table.
     */
    void onButtonPress(int row, int column);
}

private List<TableButtonPressedHandler> handlers;
private Hashtable<Integer, JButton> buttons;

public TableButton()
{
    handlers = new ArrayList<TableButtonPressedHandler>();
    buttons = new Hashtable<Integer, JButton>();
}

/**
 * Add a slide callback handler
 * @param handler
 */
public void addHandler(TableButtonPressedHandler handler)
{
    if (handlers != null)
    {
        handlers.add(handler);
    }
}

/**
 * Remove a slide callback handler
 * @param handler
 */
public void removeHandler(TableButtonPressedHandler handler)
{
    if (handlers != null)
    {
        handlers.remove(handler);
    }
}


/**
 * Removes the component at that row index
 * @param row The row index which was just removed
 */
public void removeRow(int row)
{
    if(buttons.containsKey(row))
    {
        buttons.remove(row);
    }
}

/**
 * Moves the component at oldRow index to newRow index
 * @param oldRow The old row index
 * @param newRow THe new row index
 */
public void moveRow(int oldRow, int newRow)
{
    if(buttons.containsKey(oldRow))
    {
        JButton button = buttons.remove(oldRow);
        buttons.put(newRow, button);
    }
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean selected, boolean focus, final int row, final int column)
{
    JButton button = null;
    if(buttons.containsKey(row))
    {
        button = buttons.get(row);
    }
    else
    {
        button = new JButton();
        if(value != null && value instanceof String)
        {
            button.setText((String)value);
        }
        button.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if(handlers != null)
                {
                    for(TableButtonPressedHandler handler : handlers)
                    {
                        handler.onButtonPress(row, column);
                    }
                }
            }
        });

        button.setBorder(null);
        button.setIcon(new ImageIcon(new ImageIcon(SupplierList.class
                .getResource("/org/chillies/resource/Add.png")).getImage()
                .getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH)));

        buttons.put(row, button);
    }

    return button;
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean selected, int row, int column)
{
    JButton button = null;
    if(buttons.containsKey(row))
    {
        button = buttons.get(row);
    }
    else
    {
        button = new JButton();
        if(value != null && value instanceof String)
        {
            button.setText((String)value);
        }

        buttons.put(row, button);
    }

    return button;
}

public void setButtonText(int row, String text)
{
    JButton button = null;
    if(buttons.containsKey(row))
    {
        button = buttons.get(row);
        button.setText(text);
    }
}

public Object getCellEditorValue()
{
    return null;
}

public void dispose()
{
    if (handlers != null)
    {
        handlers.clear();
    }
}
}

But when I Export my Project into jar file it seem to have problem with this one line of code

 buttonColumn.setCellRenderer(buttons);

when i remove this line and run Jar file the project runs fine but when i add this line it doesn't even load the page .

Code works fine in eclipse.

And i am loading my view class static (just extra information). Thanks for your time.


Solution

  • Well i got the answer of my question i have modified some of the code of TableButton.java file . after making some quick fixes i solved my issues.