Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: [JTable] Button in table to delete row

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default [JTable] Button in table to delete row

    Hi Everybody !

    I'm kinda new on the scene and I was hoping you guys could help me...

    I'm a beginner in java programming and I think Iwent over my head with this one.

    My programms is working perfectly except for one little detail that annoys me. I have a JTable with a button on the first column that allows to delete this row from the table. Its working well except when I try to delete the last row of the table, the button stay there and then the table freeze and i can't delete any rows because i receive a "Out of bound Exception" . But, when I use the command table.getRowCount());

    Before and after the removal of the line, the table "knows" that a line was removed but as i said, the button stay there. The screenshots in thumbnails show the situation before and after clicking the button next to the "Year" cell.

    It's good to add that i'm adding rows in the table from a button outside the table.

    Heres my code:
    Main.java

    static String [] title = {"X","Nom","SRM","Rend.","%","Kg",};
    static Object[][] contenu;
    static DefaultTableModel model1 = new DefaultTableModel(contenu, title);
    static JTable tableauGrains = new JTable(model1);
     
    tableauGrains.getColumn("X").setCellRenderer(new ButtonRenderer());
    tableauGrains.getColumn("X").setCellEditor(new ButtonEditor(new JCheckBox()));

    ButtonRenderer.java
    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.JButton;
    import javax.swing.JTable;
    import javax.swing.table.TableCellRenderer;
     
    public class ButtonRenderer extends JButton implements TableCellRenderer{
     
        public Component getTableCellRendererComponent( JTable table, Object value,
                                                        boolean isSelected, boolean isFocus,
                                                        int row, int col) {
        	Color beige = new Color(218, 217, 158);
        	setBackground(beige);
            //On écrit dans le bouton ce que contient la cellule
            setText((value != null) ? value.toString() : "");
            //on retourne notre bouton
            return this;
        }
    }
    Attached Images Attached Images


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [JTable] Button in table to delete row

    ButtonEditor.java
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;
     
    import javax.swing.DefaultCellEditor;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JOptionPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
     
    public class ButtonEditor extends DefaultCellEditor {
     
    	protected JButton button;
    	private DeleteButtonListener bListener = new DeleteButtonListener();
     
    	/**
    	 * Constructeur avec une checkBox
    	 * @param checkBox
    	 * @param count
    	 */
    	@SuppressWarnings("deprecation")
    	public ButtonEditor(JCheckBox checkBox) {
    		//Par défaut, ce type d'objet travaille avec un JCheckBox
    		super(checkBox);
    	    //On crée à nouveau notre bouton
    		button = new JButton();
    	    button.setOpaque(true);
    	    //On lui attribue un listener
    	    button.addActionListener(bListener);
     
    	}
     
    	  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    		    //On précise le numéro de ligne à notre listener
    		    bListener.setRow(row);
    		    //Idem pour le numéro de colonne
    		      //On passe aussi le tableau en paramètre pour des actions potentielles
    		    bListener.setTable(table);
     
    		    //On réaffecte le libelle au bouton
    		    button.setText( (value == null) ? "" : value.toString() );
    		    //On renvoie le bouton
    		    return button;
    		  }
     
    	  class DeleteButtonListener implements ActionListener {
     
    	        private int row;
    	        private JTable table;
     
    	        public void setRow(int row){this.row = row;}
    	        public void setTable(JTable table){this.table = table;}
     
    	        public void actionPerformed(ActionEvent event) {
    	        	if(table.getRowCount() > 0){
    	            //On affiche un message
    	            System.out.println("coucou du bouton: "+ ((JButton)event.getSource()).getText() );
    	            ((DefaultTableModel)table.getModel()).removeRow(this.row);
                        ((DefaultTableModel)table.getModel()).fireTableDataChanged();
    	          	         }
    	      }
    	   }        
    	}

    I'm sorry about the french comments, its my native language.

    It's seems like the JTable is not "refreshing" itself after the removal of the last row.

    I'm I out of my league here ?


    Thank you very much and sorry about the long post, i tried to give you the most informations possible.

    Djosimd

Similar Threads

  1. Replies: 2
    Last Post: December 12th, 2013, 11:01 PM
  2. Replies: 21
    Last Post: November 27th, 2012, 09:58 PM
  3. Replies: 2
    Last Post: August 31st, 2012, 12:36 PM