Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all columns from JTable?

Tags:

java

swing

jtable

How to remove all columns? Thanks.

like image 720
BILL Avatar asked May 26 '11 04:05

BILL


People also ask

How do you clear a JTable?

You must remove the data from the TableModel used for the table. If using the DefaultTableModel , just set the row count to zero. This will delete the rows and fire the TableModelEvent to update the GUI. JTable table; …

How to delete column in JTable?

Removing a column from a JTable means deleting the column containing the data. For removing the column, use the removeColumn() method, for this, you need the index of column that have to be deleted from the JTable.

How do you delete a column in Java?

If you want to remove columns from the middle of the model, then you will need to: extend the DefaultTableModel and create your own removeColumn(int column) method. This method would need to loop through every row in the Vector and use the Vector. remove(int) method to remove the column for ever row.


2 Answers

If you're using DefaultTableModel, set column count to zero.

like image 159
Denis Tulskiy Avatar answered Oct 02 '22 21:10

Denis Tulskiy


Faster Way::Dynamic Remove all the Column on JTable

//JTable variable name is jTable1_info....
model_info=(DefaultTableModel)jTable1_info.getModel();
//Clear all the column name on jtable jpanel-4
model_info.setColumnCount(0);

Before code on this Example

//import the DefaultTableModel
import javax.swing.table.DefaultTableModel;

public final class homepage extends javax.swing.JFrame implements ActionListener
{
    DefaultTableModel model_info;
    public homepage() throws InterruptedException
    {
        initComponents();
        create_column();//call method create jtable column names                
    }
}
private void create_column()
{
    model_info=(DefaultTableModel)jTable1_info.getModel();
    model_info.addColumn("Column_Name");
    model_info.addColumn("Column_type");
    model_info.addColumn("Column_size");        
}
like image 24
Chetan Bhagat Avatar answered Oct 02 '22 22:10

Chetan Bhagat