2 Replies - 624 Views - Last Post: 27 August 2012 - 02:12 AM Rate Topic: -----

#1 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Remove selected row in a JTable

Posted 26 August 2012 - 04:00 AM

I want to remove the selected row in the JTable that I have created in accordance with my own model. But there are no errors in the code. But the table is not updated even after the item is being removed. I think there is some problem in the code.
Here is the code...
import java.awt.*;
import javax.swing.*;
import java.awt.ActiveEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;



public class Updater extends JFrame{
private JButton addButton;
private JButton removeButton;
private JButton removeAllButton;
private JTable table;
MyTableModel model;


public Updater(Object [][] obj , String [] header){
	
	super("Runtime Updater");
setLayout(new FlowLayout());
	// constructor of JTable model
	model = new MyTableModel(obj, header);
	// the table from that model
	 table = new JTable(model);
	add(new JScrollPane(table));
	table.setRowSelectionAllowed(true);
	table.setColumnSelectionAllowed(false);
	
	
	///add additional items
	
	addButton = new JButton("Add");
	addButton.setMnemonic(KeyEvent.VK_A);
	removeButton = new JButton("Remove");
	removeButton.setMnemonic(KeyEvent.VK_R);
	
	//adding action to the addButton
	
	addButton.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e){
	String user=		JOptionPane.showInputDialog(null,"Enter User:");
	String pass=		JOptionPane.showInputDialog(null,"Enter Password:");
	
	model.addItem(user, pass); //this is a method defined in the MyTableModel class, not a predefined one...
			}	
	});
	
add(addButton);	
	
	// adding action to removeButton

removeButton.addActionListener(new ActionListener(){
	 public void actionPerformed(ActionEvent e){
	int selectedRow = table.getSelectedRow();
	
	JOptionPane.showMessageDialog(null,"The removed item : "+selectedRow);
	model.removeRow(selectedRow);
	}
});
add(removeButton);
}



public class MyTableModel extends AbstractTableModel{

	ArrayList<Object []>al;
	
	String [] header;
	
	
 public MyTableModel(Object [][] obj, String [] header){
	// save the header
		this.header = header;	
		// and the rows
		al = new ArrayList<Object[]>();
		// copy the rows into the ArrayList
		for(int i = 0; i < obj.length; ++i)
			al.add(obj[i]);
	 
 }
	
	
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return header.length;
	}

	public int getRowCount() {
		// TODO Auto-generated method stub
		return al.size();
	}

	
	public Object getValueAt(int row, int col) {
		// TODO Auto-generated method stub
		return al.get(row)[col];
	}
	public String getColumnName(int index) {
		return header[index];
	}
	void addItem(String user, String pass) {
		// make it an array[2] as this is the way it is stored in the ArrayList
		// (not best design but we want simplicity)
		String[] str = new String[2];
		str[0] = user;
		str[1] = pass;
		al.add(str);
		// inform the GUI that I have change
		fireTableDataChanged();
	}
	void removeRow(int row){
		int [] remover = new int[1];
		remover[0]=row;
		al.remove(remover);
		fireTableDataChanged();
	}
	
}
public static void main(String[] args) {
	// TODO Auto-generated method stub
	String [] header ={"User","Password"};
	Object [][] rowItems ={
			{"Red","Alert"},{"Robin","Hood"},{"Maximus","general"},{"Galeo","Romeo"},
			{"Euclid","Percy"},{"VamPercy","Nethd"}
	};
	Updater upd = new Updater(rowItems,header);
	
	
	
	upd.setVisible(true);
	upd.setSize(500,600);
	upd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	JFrame.setDefaultLookAndFeelDecorated(false);
}



}





I can't figure it out?
Ignore the comments, those are there for my learning purpose.

This post has been edited by vbabey: 26 August 2012 - 04:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Remove selected row in a JTable

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 991
  • Posts: 2,201
  • Joined: 05-April 11

Re: Remove selected row in a JTable

Posted 26 August 2012 - 05:09 AM

void removeRow(int row){
	int [] remover = new int[1];
	remover[0]=row;
	al.remove(remover);
	fireTableDataChanged();
}



See this won't work.
al.remove(remover) will try to find the object 'remover' in the list and then remove it.
Instead you will want to remove the item at 'row' index.

Simply call al.remove(row)
Was This Post Helpful? 2
  • +
  • -

#3 vbabey  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 30-July 12

Re: Remove selected row in a JTable

Posted 27 August 2012 - 02:12 AM

Thank you very much CasiOo, I followed some table tutorials from pbl.
So I tried to move on. Adding features to the table I created by myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1