简体   繁体   中英

how to avoid duplicating JTable columns in java?

i want to add a cloumn to a jtable when a radio button is being clicked. but when i click it twice two columns are being added to the table. here's my code

 dtm = (DefaultTableModel) viewTable.getModel();
    dtm.setRowCount(0);
    TableColumnModel model=viewTable.getColumnModel();
    boolean found=false;
    for (int i = 0; i < viewTable.getColumnCount(); i++) {
        if (model.getColumn(i).getIdentifier().equals("customer Id")) {
            found=true;
            break;
        }


    if (found==false) {
       dtm.addColumn("customer Id");

    }

don't know how to fix it..

This is a clumsy solution but it will work.

You can create a new boolean variable in your class and this variable represents if a column was set or not. Like:

class MyClass{
    boolean isColumnAdded

    public MyClass(){
    isColumnAdded = false;
    }

    private void radioButtonActionPerformed(java.awt.event.ActionEvent evt){
        if(!isColumnAdded){
        //add column
        isColumnAdded = true;
        }

    }

}

This code would help you. Call the below method on actionPerformed of the check box and if it is true. Validating it based on the column header.

private static void addColumn( final JTable table, final String newColumnHeader )
{
    final JTableHeader header = table.getTableHeader();
    final int columnCount = header.getColumnModel().getColumnCount();
    boolean addColumn = true;
    for( int index = 0; index < columnCount; index ++ )
    {
        final Object headerValue = header.getColumnModel().getColumn(index).getHeaderValue();
        if( newColumnHeader.equals( headerValue ) )
        {
            JOptionPane.showMessageDialog(null, "Column already exists" );
            addColumn = false;
            break;
        }
    }

    if( addColumn )
    {
        final TableColumn newCol = new TableColumn();
        newCol.setHeaderValue(newColumnHeader);
        table.getColumnModel().addColumn(newCol);
    }

}

It is good to disable the checkbox if it is already clicked ;) if you do not want a huge code.

To start with, JRadioButton has a selected property. You should be checking this state to determine if the column needs to be removed or added...

Assume that each column name is unique, you could use something like...

TableColumnModel model = viewTable.getColumnModel();
int index = -1;
try {
    index = model.getColumnIndex("customer Id");
} catch (IllegalArgumentException e) {
    // I know, sucks...
}
if (index < 0) {
    // Add new column, if JRadioButton.isSelected
} else {
    // Remove old column...
    // JRadioButton.isSelected is false...
}

To find and add/remove the column.

Have a look at How to Use Buttons, Check Boxes, and Radio Buttons for some more details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM