• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Delete rows from Jtable when the checkbox is checked

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic