简体   繁体   English

从jtable中删除突出显示的列和数据

[英]Remove highlighted columns and data from jtable

Is there a way I can delete the highlighted selected columns in this jtable using the remove button? 有没有一种方法可以使用“删除”按钮删除此jtable中突出显示的选定列? I know there's a way for rows but I'm not sure how to do this for selected columns. 我知道有一种行的方法,但是我不确定如何对选定的列执行此操作。

private void RemoveColBActionPerformed(java.awt.event.ActionEvent evt) {
  // Removes the highlighted column 
}

private void AddBActionPerformed(java.awt.event.ActionEvent evt) {
  //Add Data
  lMessage.setText("");
  DefaultTableModel model = (DefaultTableModel) JtableData.getModel();
  if (!ProdNameTF.getText().trim().equals("")) {
    model.addRow(new Object[] {
      ProdNameTF.getText(), CategoryCB.getSelectedItem().toString(), PriceTF.getText()
    });
  } else {
    lMessage.setText("Message Left Blank");
  }
}

在此处输入图片说明

You can remove columns from the JTable view. 您可以从JTable视图中删除列。 The data will still be contained in the TableModel, it just won't be displayed in the JTable. 数据仍将包含在TableModel中,只是不会显示在JTable中。

So the basic code would be: 因此,基本代码为:

TableColumnModel tcm = table.getColumnModel();
tcm.removeColumn( tcm.getColumn(...) );

For a more complex solution that allows the user to hide/show columns as they wish check out the Table Column Manager . 对于更复杂的解决方案,允许用户根据需要隐藏/显示列,请查看表列管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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