Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Display more then one button in one column

hello there,

i have a JTable and have a column with a button. now i want to add another button to this column.how can i do this. i tried doing it.but even my previous button is not listening to clcik action.what could i have done wrong.my code is attached below..please help.

cheers
zolf
finishTable=new JTable(finishTableModel);
		finishTable.setDefaultRenderer(Object.class,new CustomeTableCellRenderer()); 
		finishTable.setAutoCreateRowSorter(true);
		ButtonCell buttonCell=new ButtonCell(b){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			@Override
			public void actionPerformed(ActionEvent e) {
				super.actionPerformed(e);
				//System.out.println(finishTable.getSelectedRow());
				//System.out.println(finishTableModel.getValueAt(finishTable.getSelectedRow(),1));
				int prodCode = Integer.parseInt(finishTableModel.getValueAt(finishTable.getSelectedRow(),1).toString());
				createProductFrame(rightToLeftAlligned,prodCode);
			}
		};
		ButtonCell buttonCellDelete=new ButtonCell(b){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			@Override
			public void actionPerformed(ActionEvent e) {
				super.actionPerformed(e);
				System.out.println(finishTable.getSelectedRow());
				System.out.println(finishTableModel.getValueAt(finishTable.getSelectedRow(),1));
				//int prodCode = Integer.parseInt(finishTableModel.getValueAt(finishTable.getSelectedRow(),1).toString());
				//createProductFrame(rightToLeftAlligned,prodCode);
			}
		};
		finishTable.getColumnModel().getColumn(0).setMaxWidth(40);
		finishTable.removeColumn(finishTable.getColumnModel().getColumn(1));
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCellDelete);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCellDelete);
		finishTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		finishTable.setColumnSelectionAllowed(false);

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You would need to add another button on a panel below the other one.
Avatar of Zolf

ASKER


that is what i did

finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCellDelete);
finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCellDelete);

/**
       * Inits Finish Product table
       */
      private void initFinishTable()
      {
            DefaultTableModel dtm=(DefaultTableModel)finishTable.getModel();
            dtm.setRowCount(0);
            PreparedStatement stmt=null;
            ResultSet rs=null;
            try
            {
                  stmt=conn.prepareStatement("SELECT FinishProductID,FinishProductBrandName,FinishProductGenericName,FinishProductCode,selling_price,FinishProductSalesLimit FROM FinishProduct");
                  rs=stmt.executeQuery();
                  while(rs.next())
                  {
                        dtm.addRow(new Object[]{dtm.getRowCount()+1,rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),"Edit","Delete"});
                  }
            } catch (SQLException SQLEx) {
                  processError(SQLEx);
            }finally
            {
                  rs=DBTools.clearRes(rs);
                  stmt=DBTools.clearRes(stmt);
            }
      }
if you want to show two buttons on a cell, you need to add the buttons in a panel and return the panel as the cell renderer. But this needs extra effort to make the focus and clieck event to appropriate button. Instead, you can show them two columns, i.e., one button in a cell! If you need to show them as single cell, you may set the cell border appropriately and use groupbable column header!
Avatar of Zolf

ASKER


>>If you need to show them as single cell, you may set the cell border appropriately and use groupbable column header!

i want them to be should in one cell.
can you please give me an example
>>i want them to be should in one cell.

It's just a question of modifying your current renderer and editor. Can you post them please, starting with the CellRenderer?
Avatar of Zolf

ASKER


i have pasted the code below.do you also need the ButtonCell Code
//		Init tables models
		finishTableModel=new DefaultTableModel(){
			private static final long serialVersionUID = 1L;
 
			public boolean isCellEditable(int row, int column) {
				if(column==7)
				{
					return true;
				}else
				{
					return false;
				}
			}
		};
		finishTableModel.setColumnIdentifiers(new String[]{"Sr. No.","ID","Brand Name","Generic Name","FinishProductCode","Price","Sales Limit","Action"});
 
finishTable=new JTable(finishTableModel);
		finishTable.setDefaultRenderer(Object.class,new CustomeTableCellRenderer()); 
		finishTable.setAutoCreateRowSorter(true);
		ButtonCell buttonCell=new ButtonCell(b){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			@Override
			public void actionPerformed(ActionEvent e) {
				super.actionPerformed(e);
				//System.out.println(finishTable.getSelectedRow());
				//System.out.println(finishTableModel.getValueAt(finishTable.getSelectedRow(),1));
				int prodCode = Integer.parseInt(finishTableModel.getValueAt(finishTable.getSelectedRow(),1).toString());
				createProductFrame(rightToLeftAlligned,prodCode);
			}
		};
		finishTable.getColumnModel().getColumn(0).setMaxWidth(40);
		finishTable.removeColumn(finishTable.getColumnModel().getColumn(1));
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCell);
		finishTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		finishTable.setColumnSelectionAllowed(false);

Open in new window

Avatar of Zolf

ASKER



please help
the code of ButtonCell and if there are superclasses for that would be helpful!
Avatar of Zolf

ASKER


public class ButtonCell extends AbstractCellEditor 
	implements TableCellRenderer, TableCellEditor, ActionListener
{
 
	private static final long serialVersionUID = 1L;
	
    private JButton renderButton;
    private JButton editButton;
    private String text;
        /**
     * 
     */
    public ButtonCell(ImageIcon a)
    {
        super();
        renderButton = new JButton(a);
        //renderButton.setMargin(new Insets(0,0,0,0));
        //renderButton.setBorder(null);
        //renderButton.setToolTipText("Edit");
        editButton = new JButton(a);
        //editButton.setToolTipText("Edit");
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        
    }
    public ButtonCell()
    {
        super();
        renderButton = new JButton();
        //renderButton.setToolTipText("Edit");
        editButton = new JButton();
        //editButton.setToolTipText("Edit");
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        
    }
    /* (non-Javadoc)
     * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(
    		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
    	renderButton.setText( (value == null) ? "" : value.toString() );
        JPanel panel=new JPanel();
        panel.setBackground(table.getBackground());
    	panel.add(renderButton);
    	int heightWanted = (int)panel.getPreferredSize().getHeight();
    	if (heightWanted > table.getRowHeight(row))
    	{
    		table.setRowHeight(row, heightWanted);
    	}
    	return panel;
    }
 
    /* (non-Javadoc)
     * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
     */
    public Component getTableCellEditorComponent(
    		JTable table, Object value, boolean isSelected, int row, int column)
    {
    	text = (value == null) ? "" : value.toString();
    	editButton.setText( text );
    	JPanel panel=new JPanel();
    	panel.setBackground(table.getBackground());
    	panel.add(editButton);
    	return panel;
    }
 
    /* (non-Javadoc)
     * @see javax.swing.CellEditor#getCellEditorValue()
     */
    public Object getCellEditorValue()
    {
        return text;
    }
 
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e)
    {
        fireEditingStopped();
    }

Open in new window

try this change,
public class ButtonCell extends AbstractCellEditor 
	implements TableCellRenderer, TableCellEditor, ActionListener
{
 
	private static final long serialVersionUID = 1L;
	
    private JButton renderButton;
    private JButton editButton;
    private JButton renderButton1;
    private JButton editButton1;
    private String text;
        /**
     * 
     */
    public ButtonCell(ImageIcon a)
    {
        super();
        renderButton = new JButton(a);
        //renderButton.setMargin(new Insets(0,0,0,0));
        //renderButton.setBorder(null);
        //renderButton.setToolTipText("Edit");
        editButton = new JButton(a);
        //editButton.setToolTipText("Edit");
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
    }
    public ButtonCell()
    {
        super();
        renderButton = new JButton();
        //renderButton.setToolTipText("Edit");
        editButton = new JButton();
        //editButton.setToolTipText("Edit");
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
    }
    /* (non-Javadoc)
     * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
     */
    public Component getTableCellRendererComponent(
    		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
//NOW THE VALUE SHOULD BE HAVING VALUES FOR BOTH THE BUTTON, LETS SAY YOU HAVE COMMA SEPARATED VALUES
    	//renderButton.setText( (value == null) ? "" : value.toString() );
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
renderButton.setText( values[0] ) ;
renderButton1.setText( values[1] ) ;
        JPanel panel=new JPanel();
        panel.setBackground(table.getBackground());
    	panel.add(renderButton);
    	panel.add(renderButton1);
    	int heightWanted = (int)panel.getPreferredSize().getHeight();
    	if (heightWanted > table.getRowHeight(row))
    	{
    		table.setRowHeight(row, heightWanted);
    	}
    	return panel;
    }
 
    /* (non-Javadoc)
     * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
     */
    public Component getTableCellEditorComponent(
    		JTable table, Object value, boolean isSelected, int row, int column)
    {
    	//text = (value == null) ? "" : value.toString();
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
editButton.setText( values[0] ) ;
editButton1.setText( values[1] ) ;
    	editButton.setText( text );
    	JPanel panel=new JPanel();
    	panel.setBackground(table.getBackground());
    	panel.add(editButton);
    	panel.add(editButton1);
    	return panel;
    }
 
    /* (non-Javadoc)
     * @see javax.swing.CellEditor#getCellEditorValue()
     */
    public Object getCellEditorValue()
    {
        return text;
    }
 
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e)
    {
        fireEditingStopped();
    }
 
Open in New Window 

Open in new window

Avatar of Zolf

ASKER


i get error.

String[] values = value == null ? new String[]{ "Edit", "Delete" } : value.toString().split( "," ) ;


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
      at custom.java.swing.table.ButtonCell.getTableCellRendererComponent(ButtonCell.java:69)
      at javax.swing.JTable.prepareRenderer(Unknown Source)
      at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
      at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
Avatar of Zolf

ASKER



also my other classes which use ButtonCell is giving me error
as I said, you should be having comma separated values for that cell,

//NOW THE VALUE SHOULD BE HAVING VALUES FOR BOTH THE BUTTON, LETS SAY YOU HAVE COMMA SEPARATED VALUES

you also can encapsulate into a class and have an instance of that for a cell. That time you need to get the values from the new instance and set them to the buttons.
Avatar of Zolf

ASKER



where.please give me example in my code above.appreciate your help
I think, for the Sales Limit column you want to have two buttons in a cell.

for earlier button, you set the value of FinishProductSalesLimit from DB, now sice you have another button what value you want to set for the second button?
Avatar of Zolf

ASKER


at present my table looks like this. i am trying to put another button next to edit button. so that i dont have to create another column to have a button for delete row.
1.JPG
oh ok, got it, try the below change,

dtm.addRow(new Object[]{dtm.getRowCount()+1,rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),"Edit,Delete"});
and one more thing, you need to set delete icon for the delete button...
Avatar of Zolf

ASKER


i still get the same error.
can you paste the buttoncell code after the changes?
Avatar of Zolf

ASKER



this is my code
 
finishTableModel=new DefaultTableModel(){
			private static final long serialVersionUID = 1L;
 
			public boolean isCellEditable(int row, int column) {
				if(column==7)
				{
					return true;
				}else
				{
					return false;
				}
			}
		};
		finishTableModel.setColumnIdentifiers(new String[]{"Sr. No.","ID","Brand Name","Generic Name","FinishProductCode","Price","Sales Limit","Action"});
 
 
finishTable=new JTable(finishTableModel);
		finishTable.setDefaultRenderer(Object.class,new CustomeTableCellRenderer()); 
		finishTable.setAutoCreateRowSorter(true);
		ButtonCell buttonCell=new ButtonCell(b){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			@Override
			public void actionPerformed(ActionEvent e) {
				super.actionPerformed(e);
				//System.out.println(finishTable.getSelectedRow());
				//System.out.println(finishTableModel.getValueAt(finishTable.getSelectedRow(),1));
				int prodCode = Integer.parseInt(finishTableModel.getValueAt(finishTable.getSelectedRow(),1).toString());
				updateFinishProductFrame(rightToLeftAlligned,prodCode);
			}
		};
		ButtonCell buttonCellDelete=new ButtonCell(b){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			@Override
			public void actionPerformed(ActionEvent e) {
				super.actionPerformed(e);
				System.out.println(finishTable.getSelectedRow());
				System.out.println(finishTableModel.getValueAt(finishTable.getSelectedRow(),1));
				//int prodCode = Integer.parseInt(finishTableModel.getValueAt(finishTable.getSelectedRow(),1).toString());
				//createProductFrame(rightToLeftAlligned,prodCode);
			}
		};
		finishTable.getColumnModel().getColumn(0).setMaxWidth(40);
		finishTable.removeColumn(finishTable.getColumnModel().getColumn(1));
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCellDelete);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCellDelete);
		finishTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		finishTable.setColumnSelectionAllowed(false);

Open in new window

Avatar of Zolf

ASKER


this is the code i have now for ButtonCell
public class ButtonCell extends AbstractCellEditor 
implements TableCellRenderer, TableCellEditor, ActionListener
{
 
private static final long serialVersionUID = 1L;
 
private JButton renderButton;
private JButton editButton;
private JButton renderButton1;
private JButton editButton1;
private String text;
    /**
 * 
 */
public ButtonCell(ImageIcon a)
{
    super();
    renderButton = new JButton(a);
    //renderButton.setMargin(new Insets(0,0,0,0));
    //renderButton.setBorder(null);
    //renderButton.setToolTipText("Edit");
    editButton = new JButton(a);
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
}
public ButtonCell()
{
    super();
    renderButton = new JButton();
    //renderButton.setToolTipText("Edit");
    editButton = new JButton();
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
}
/* (non-Javadoc)
 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
 */
public Component getTableCellRendererComponent(
		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
//NOW THE VALUE SHOULD BE HAVING VALUES FOR BOTH THE BUTTON, LETS SAY YOU HAVE COMMA SEPARATED VALUES
	//renderButton.setText( (value == null) ? "" : value.toString() );
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
renderButton.setText( values[0] ) ;
renderButton1.setText( values[1] ) ;
    JPanel panel=new JPanel();
    panel.setBackground(table.getBackground());
	panel.add(renderButton);
	panel.add(renderButton1);
	int heightWanted = (int)panel.getPreferredSize().getHeight();
	if (heightWanted > table.getRowHeight(row))
	{
		table.setRowHeight(row, heightWanted);
	}
	return panel;
}
 
/* (non-Javadoc)
 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
 */
public Component getTableCellEditorComponent(
		JTable table, Object value, boolean isSelected, int row, int column)
{
	//text = (value == null) ? "" : value.toString();
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
editButton.setText( values[0] ) ;
editButton1.setText( values[1] ) ;
	editButton.setText( text );
	JPanel panel=new JPanel();
	panel.setBackground(table.getBackground());
	panel.add(editButton);
	panel.add(editButton1);
	return panel;
}
 
/* (non-Javadoc)
 * @see javax.swing.CellEditor#getCellEditorValue()
 */
public Object getCellEditorValue()
{
    return text;
}
 
/* (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e)
{
    fireEditingStopped();
}
 
}

Open in new window

Avatar of Zolf

ASKER



and this where i create my table
private void initFinishTable()
	{
		DefaultTableModel dtm=(DefaultTableModel)finishTable.getModel();
		dtm.setRowCount(0);
		PreparedStatement stmt=null;
		ResultSet rs=null;
		try
		{
			stmt=conn.prepareStatement("SELECT FinishProductID,FinishProductBrandName,FinishProductGenericName,FinishProductCode,selling_price,FinishProductSalesLimit FROM FinishProduct");
			rs=stmt.executeQuery();
			while(rs.next())
			{
				//dtm.addRow(new Object[]{dtm.getRowCount()+1,rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),"Edit"});
				dtm.addRow(new Object[]{dtm.getRowCount()+1,rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),"Edit,Delete"});
 
			}
		} catch (SQLException SQLEx) {
			processError(SQLEx);
		}finally
		{
			rs=DBTools.clearRes(rs);
			stmt=DBTools.clearRes(stmt);
		}
	}

Open in new window

>>            finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCellDelete);
            finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCellDelete);
>>

remove these lines.

also make sure you done the below change,

dtm.addRow(new Object[]{dtm.getRowCount()+1,rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),"Edit,Delete"});

also, add the below statement in the getTableCellrenderer top,

System.out.println( value ) ;

and paste the printed values!



Avatar of Zolf

ASKER


this is the output i get. i also updatedmy code to what you mentioned. look below
Edit,Delete
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at custom.java.swing.table.ButtonCell.getTableCellRendererComponent(ButtonCell.java:71)
      at javax.swing.JTable.prepareRenderer(Unknown Source)
      at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
      at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
      at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
finishTable.getColumnModel().getColumn(0).setMaxWidth(40);
		finishTable.removeColumn(finishTable.getColumnModel().getColumn(1));
		finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCell);
		finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCell);
		//finishTable.getColumnModel().getColumn(6).setCellRenderer(buttonCellDelete);
		//finishTable.getColumnModel().getColumn(6).setCellEditor(buttonCellDelete);
		finishTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		finishTable.setColumnSelectionAllowed(false);

Open in new window

okay, now a different exception... I think, you haven't initialized renderButton1!

can you post the ButtonCell code?
Avatar of Zolf

ASKER


public class ButtonCell extends AbstractCellEditor 
implements TableCellRenderer, TableCellEditor, ActionListener
{
 
private static final long serialVersionUID = 1L;
 
private JButton renderButton;
private JButton editButton;
private JButton renderButton1;
private JButton editButton1;
private String text;
    /**
 * 
 */
public ButtonCell(ImageIcon a)
{
    super();
    renderButton = new JButton(a);
    //renderButton.setMargin(new Insets(0,0,0,0));
    //renderButton.setBorder(null);
    //renderButton.setToolTipText("Edit");
    editButton = new JButton(a);
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
}
public ButtonCell()
{
    super();
    renderButton = new JButton();
    //renderButton.setToolTipText("Edit");
    editButton = new JButton();
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    //INITIALIZE THE NEW BUTTONs WITH APPROPRIATE...
}
/* (non-Javadoc)
 * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
 */
public Component getTableCellRendererComponent(
		JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
//NOW THE VALUE SHOULD BE HAVING VALUES FOR BOTH THE BUTTON, LETS SAY YOU HAVE COMMA SEPARATED VALUES
	//renderButton.setText( (value == null) ? "" : value.toString() );
	System.out.println( value ) ;
 
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
renderButton.setText( values[0] ) ;
renderButton1.setText( values[1] ) ;
    JPanel panel=new JPanel();
    panel.setBackground(table.getBackground());
	panel.add(renderButton);
	panel.add(renderButton1);
	int heightWanted = (int)panel.getPreferredSize().getHeight();
	if (heightWanted > table.getRowHeight(row))
	{
		table.setRowHeight(row, heightWanted);
	}
	return panel;
}
 
/* (non-Javadoc)
 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
 */
public Component getTableCellEditorComponent(
		JTable table, Object value, boolean isSelected, int row, int column)
{
	//text = (value == null) ? "" : value.toString();
String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
editButton.setText( values[0] ) ;
editButton1.setText( values[1] ) ;
	editButton.setText( text );
	JPanel panel=new JPanel();
	panel.setBackground(table.getBackground());
	panel.add(editButton);
	panel.add(editButton1);
	return panel;
}
 
/* (non-Javadoc)
 * @see javax.swing.CellEditor#getCellEditorValue()
 */
public Object getCellEditorValue()
{
    return text;
}
 
/* (non-Javadoc)
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
public void actionPerformed(ActionEvent e)
{
    fireEditingStopped();
}

Open in new window

try these changes,

public ButtonCell(ImageIcon a)
{
    super();
    renderButton = new JButton(a);
    //renderButton.setMargin(new Insets(0,0,0,0));
    //renderButton.setBorder(null);
    //renderButton.setToolTipText("Edit");
    editButton = new JButton(a);
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
//YOU NEED TO SET APPROPRIATE ICON
    renderButton1 = new JButton(a);
    editButton1 = new JButton(a);
    editButton1.setFocusPainted( false );
    editButton1.addActionListener( this );
    //YOU NEED TO CHANGE THE ACTIONLISTENRE SO THAT IT HANDLES EDIT AND DELETE
}
public ButtonCell()
{
    super();
    renderButton = new JButton();
    //renderButton.setToolTipText("Edit");
    editButton = new JButton();
    //editButton.setToolTipText("Edit");
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    renderButton1 = new JButton();
    editButton1 = new JButton();
    editButton1.setFocusPainted( false );
    editButton1.addActionListener( this );
}
Avatar of Zolf

ASKER


ksivananth

yes i had not initialise the other button. now i can see both the button in one column.also how do i manage the buttons i have used in my other classes.there i need only one button.
but now i get error when i click on the buttons.
>>//YOU NEED TO SET APPROPRIATE ICON

may be you can do this way,

public ButtonCell(ImageIcon edit, ImageIcon del)
{
    super();
    renderButton = new JButton(edit);
    editButton = new JButton(edit);
    editButton.setFocusPainted( false );
    editButton.addActionListener( this );
    renderButton1 = new JButton(del);
    editButton1 = new JButton(del);
    editButton1.setFocusPainted( false );
    editButton1.addActionListener( this );
    //YOU NEED TO CHANGE THE ACTIONLISTENRE SO THAT IT HANDLES EDIT AND DELETE
}
>>also how do i manage the buttons i have used in my other classes.there i need only one button.

then have two classes,

OneButtonCell, TwoButtonCell...

>>now i get error when i click on the buttons.

what err?
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER


let me close this and open another question.thanks very much.
Please continue here if this is not complete and delete the new question (see rules)
Avatar of Zolf

ASKER


actually the question i had asked here was answered (insert two buttons in one cell). i had another question which i thought was not correct from my side to ask here.that is why i opened a new question.