
Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me.

No answers Yet.... :(

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
public class RemoveDataFromTable{
JTable table;
JButton button;
public static void main(String[] args){
new RemoveDataFromTable();
}
public RemoveDataFromTable(){
JFrame frame = new JFrame("Getting Cell Values in JTable");
JPanel panel = new JPanel();
String data[][] = {{"Angelina","Mumbai"},{"Martina","Delhi"}};
String col[] = {"Name","Address"};
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
button=new JButton("Remove Data");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
int index=1;
int count=table.getRowCount();
for(int i=0;i<count;i++){
SetData("",i, 0);
SetData("",i, 1);
}
}
});
panel.add(pane);
panel.add(button);
frame.add(panel);
frame.setSize(500,500);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void SetData(Object obj, int row_index, int col_index){
table.getModel().setValueAt(obj,row_index,col_index);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.