Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Delete rows from Jtable when the checkbox is checked  RSS feed

 
ravisha andar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I want to use a Jtable with a check box as one of the coloumn.

When I check the checkboxe and click on the delete button I want the selected rows to be deleted.

Can any body help me with this ?

A sample code will help me a lot !!!

Thanks
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sample code will help me a lot !!!


You first. Let's see what you tried.

Have you gone through the JTable API and the tutorial linked from the API to discover what methods might be useful? You might also want to take a look at the DefaultTableModel API.
 
ravisha andar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried

public class RemoveRows{
public static void main(String[] args) {
new RemoveRows();
}

public RemoveRows(){
JFrame frame = new JFrame("Delete Rows");
JPanel panel = new JPanel();
String data[][] = {{"abc","100"},{"def","200"}};
String col[] = {"Name","code"};
final DefaultTableModel model = new DefaultTableModel(data,col);
final JTable table = new JTable(model);
Jbutton button = new Jbutton("Click to delete");
button.addEventListener(new ActionEvent(){
void action performed(Event e)
{

model.removeRow(table.getSelectedRow());
}


panel.add(table);
panel.add(button);

frame.add(panel);
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}



I have a problem doing that checkbox selet and delete !!!
Can you please help
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you don't even have checkboxes in your JTable yet, not that I can see. First step would be to put boolean values into your table model, so there's some data for the checkboxes to represent.
 
ravisha andar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tries to put new Bollean(true);

But i got the values true or false in the coloumn.

I have learnt that i need to override a method.

can anybody help me with this
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For checkboxes to appear you need to override the getColumnClass in your tablemodel to return Boolean in the column you want.
Read the javax.swing.table.DefaultTableModel documentation. I know its a little confusing at first but its not that hard to understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!