Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on 

Two Button Error continue

hello there,

 ksivananth :
i get null point exception. let me create two class to what you suggested. one for one button anothe rtwo button. and i will get back to you.

cheers
zolf
Java

Avatar of undefined
Last Comment
ksivananth
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

?

Is the previous question complete - evidently not perhaps? If not, continue in the original please
Can you please exacly tell what is your question? I think your question is incompelete
Avatar of sciuriware
sciuriware

Please delete this question.
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


i wanted to have two JButton in one column instead of two columns one for each button. the two button i am using to edit and delete a row in my jtable.

EE ksivananth : helped me to get the two button in one column. now my problem is when i click on the button the form of the button closes and also both have the same icon even thought i assigned different icon for them.
please help

this is the link to my previous question
https://www.experts-exchange.com/questions/23584351/Display-more-then-one-button-in-one-column.html


Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


snapshot of the problem
2.JPG
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


i changed some code and now the button form does not change.but i get a new error
i know where the problem lies. its in here.i need to pass the values array.

public Object getCellEditorValue()
    {
          System.out.println(values);
        return values;
    }
Avatar of ksivananth
ksivananth
Flag of United States of America image

can you post the new code( TwoButtonCell )?
Avatar of ksivananth
ksivananth
Flag of United States of America image

also specify what error you are getting?
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
      at custom.java.swing.table.ButtonCellTwo.getTableCellRendererComponent(ButtonCellTwo.java:73)
      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)
      at javax.swing.plaf.ComponentUI.update(Unknown Source)

ksivananth:
please help me to sort this.
 
public class ButtonCellTwo extends AbstractCellEditor 
	implements TableCellRenderer, TableCellEditor, ActionListener
{
 
	private static final long serialVersionUID = 1L;
 
	private JButton renderButton;
	private JButton editButton;
	private JButton renderButton1;
	private JButton delButton;
	private String[] values;
        /**
     * 
     */
    public ButtonCellTwo(ImageIcon edit, ImageIcon del)
 
    {
    	super();
        renderButton = new JButton(edit);
        renderButton1 = new JButton(del);
        editButton = new JButton(edit);
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );       
        delButton= new JButton(del);
        delButton.setFocusPainted( false );
        delButton.addActionListener( this );
        
    }
    public ButtonCellTwo()
    {
    	super();
        renderButton = new JButton();
        renderButton1 = new JButton();
        //renderButton.setToolTipText("Edit");
        editButton = new JButton();
        //editButton.setToolTipText("Edit");
        editButton.setFocusPainted( false );
        editButton.addActionListener( this );
        
        delButton = new JButton();
        //editButton.setToolTipText("Edit");
        delButton.setFocusPainted( false );
        delButton.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)
    {
    	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)
    {
    	values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
    	editButton.setText( values[0] ) ;
    	delButton.setText( values[1] ) ;
    	//editButton.setText( text );
    	JPanel panel=new JPanel();
    	panel.setBackground(table.getBackground());
    	panel.add(editButton);
    	panel.add(delButton);
    	return panel;
    }
 
    /* (non-Javadoc)
     * @see javax.swing.CellEditor#getCellEditorValue()
     */
    public Object getCellEditorValue()
    {
    	System.out.println(values);
        return values;
    }
 
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e)
    {
        fireEditingStopped();
    }
 
 
}

Open in new window

Avatar of ksivananth
ksivananth
Flag of United States of America image

do the below change and post back the output,

    public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
System.out.println( "VALUE: " + value ) ;
          String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
check the size of values variable how much it is coming. May be there you are facing problem.
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER

VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
[Ljava.lang.String;@107108e
1
VALUE: [Ljava.lang.String;@107108e
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
      at custom.java.swing.table.ButtonCellTwo.getTableCellRendererComponent(ButtonCellTwo.java:76)
      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)
      at javax.swing.plaf.ComponentUI.update(Unknown Source)
      at javax.swing.JComponent.paintComponent(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JViewport.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JLayeredPane.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JComponent.paintChildren(Unknown Source)
      at javax.swing.JComponent.paint(Unknown Source)
      at javax.swing.JLayeredPane.paint(Unknown Source)
      at javax.swing.JComponent.paintToOffscreen(Unknown Source)
      at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
      at javax.swing.RepaintManager.paint(Unknown Source)
      at javax.swing.JComponent._paintImmediately(Unknown Source)
      at javax.swing.JComponent.paintImmediately(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
      at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
      at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER



i think the problem lies here
public Object getCellEditorValue()
    {
          System.out.println(values);
        return values;
    }
Avatar of ksivananth
ksivananth
Flag of United States of America image

looks like the issue is in the data population,

>>[Ljava.lang.String;@107108e
1
VALUE: [Ljava.lang.String;@107108e
>>

 what are these values?

try the below changes,

          String[] values = value == null ? new String[]{ "", "" } : value.toString().split( "," ) ;
if( values.length < 2 ) values = new String[]{ "", "" } ;

and do the same in the getCellEditorComponent too!

Note: the above fixes the ArrayIndexoutofBoundException but the real isssue is in data population, you need to correct there!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>>>

looks like the issue is in the data population,

>>[Ljava.lang.String;@107108e
1
VALUE: [Ljava.lang.String;@107108e
>>
>>>>

The value is *already* a String[] so

>>value.toString().split( "," ) ;

will fail

Another notional hack would be

if (value instanceof String[]) //etc

but you need to get your 'value' sorted out


Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


the output i get is this.in getCellEditorValue() method i have put a println.and as you can see below i get that garbage values in there.please help me.

VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
getCellEditorValue()       [Ljava.lang.String;@913dc1
1
VALUE: [Ljava.lang.String;@913dc1
VALUE: Edit,Delete
VALUE: Edit,Delete
VALUE: Edit,Delete
Avatar of ksivananth
ksivananth
Flag of United States of America image

try this,

public Object getCellEditorValue()
    {
        return "Edit,Delete";
    }
Avatar of ksivananth
ksivananth
Flag of United States of America image

>>The value is *already* a String[] so

>>value.toString().split( "," ) ;

will fail

Another notional hack would be

if (value instanceof String[]) //etc
>>

if he correct the model population, this is not needed at all!
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


that problem got solved. but now how do i give each button different action listeners.because both are responding to edit listeners
Avatar of ksivananth
ksivananth
Flag of United States of America image

override the actionPerformed method as below,

public void actionPerformed( ActionEvent ae ){
  JButton btn = ae.getSource() ;
  String text = btn.getText() ;
  if( "Edit".equals( text ) ){
    //perform edit  action here
  }else if( "Delete".equals( text ) ){
    //perform delete
  }else{
    System.out.println( "Invalid Button Action!" ) ;
  }
}
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER


i replaced the actionPerformed with yours.but i get error on this line JButton btn = ae.getSource() ;
saying cannot convert from object to jbutton

ButtonCellTwo buttonCell=new ButtonCellTwo(b,deleteJB){
                  /**
                   *
                   */
                  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);
                  }*/
                  
                  public void actionPerformed( ActionEvent ae ){
                          JButton btn = ae.getSource() ;
                          String text = btn.getText() ;
                          if( "Edit".equals( text ) ){
                            //perform edit  action here
                          }else if( "Delete".equals( text ) ){
                            //perform delete
                          }else{
                            System.out.println( "Invalid Button Action!" ) ;
                          }
                        }
            };
Avatar of ksivananth
ksivananth
Flag of United States of America image

sorry, you need to cast it,

JButton btn = ( JButton )ae.getSource() ;
Avatar of ksivananth
ksivananth
Flag of United States of America image

also make sure you have the implementation for edit and delete actions in that method!
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER



can you please tell me what you meant by this.

>>The value is *already* a String[] so

>>value.toString().split( "," ) ;

will fail

Another notional hack would be

if (value instanceof String[]) //etc
>>

if he correct the model population, this is not needed at all!
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>if he correct the model population, this is not needed at all!

Well of course, which is which i said (which you didn't quote):

>>but you need to get your 'value' sorted out
Avatar of ksivananth
ksivananth
Flag of United States of America image

I think, you should be fine after the below change,

    public Object getCellEditorValue()
    {
        return "Edit,Delete";
    }
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>>>
can you please tell me what you meant by this.

>>The value is *already* a String[] so

>>value.toString().split( "," ) ;

will fail


>>>>

Certainly. The value 'value' that you were getting was somehow or other *already* of type String[]. You can't split() the toString method of an array

Avatar of ksivananth
ksivananth
Flag of United States of America image

>>Well of course, which is which i said (which you didn't quote):

That is what I have mentioned in my original comment,

>>looks like the issue is in the data population,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>You can't split() the toString method of an array

... hence you would get an ArrayIndexOutOfBounds exception for an index > 0
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

I suspect the problem is that you're not using the row and column parameters correctly in order to ensure that getCellEditorComponent works properly
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Zolf
Zolf
Flag of United Arab Emirates image

ASKER



thanks mate for your time and patience.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>
but if he changed that to

public Object getCellEditorValue()
    {
        return "Edit,Delete";
    }

there shouldn't be any issues!
>>

Well you wouldn't want to hardcode that. I would suggest

public Object getCellEditorValue()
    {
        return Arrays.toString(values).replaceAll("^\\[|\\]$", ""); // But safer to return the String value of the array the 'hard way'
    }

Avatar of ksivananth
ksivananth
Flag of United States of America image

>>public Object getCellEditorValue()
    {
        return Arrays.toString(values).replaceAll("^\\[|\\]$", ""); // But safer to return the String value of the array the 'hard way'
    }
>>

not really this is needed, rather he can store Object value instead of array values and return that here,

public Object getCellEditorValue()
    {
        return value ;
    }
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo