[英]How to remove a jTable column from another class, in java
I'm using a cardlayout, to display list of panels. 我正在使用卡片布局来显示面板列表。 In one of the panels, i have a JTable
with several columns and rows of records. 在其中一个面板中,我有一个JTable
其中包含几列和几行记录。 Now, i'd like to remove the first column of the JTable
, depending on which user logs in. Below is my code(in the login JFrame
): 现在,我想删除JTable
的第一列,具体取决于登录的用户。以下是我的代码(在登录JFrame
):
//calling the jframe that holds the cardlayout
Home home= new Home();
//calling the panel that holds the jtable
viewRecords b =new viewRecords();
//when a user logs in
//removing the 1st column of the jtable in panel viewRecords
b.jTable1.removeColumn(b.jTable1.getColumnModel().getColumn(0));
//displaying jFrame Home
Home.setVisible(true);
The problem is: it works fine when using a JFrame
to display the table, but doesn't work while using JPanel
to display the same table. 问题是:使用JFrame
显示表格时,它可以正常工作,但是使用JPanel
显示相同的表格时,它不起作用。 Any idea on how to make this work? 对如何进行这项工作有任何想法吗?
You can remove the column from the view of the table: 您可以从表视图中删除该列:
public void hideColumn(int modelColumn)
{
int viewColumn = table.convertColumnIndexToView( modelColumn );
if (viewColumn != -1)
{
TableColumnModel tcm = table.getColumnModel();
TableColumn column = tcm.getColumn(viewColumn);
tcm.removeColumn( column );
}
}
For a fancy implementation of this logic check out the Table Column Manager which allows the user to hide/show columns. 为了更好地实现此逻辑,请查看“ 表列管理器” ,该表允许用户隐藏/显示列。
this code hide fisrt column in your jtable 此代码在您的jtable中隐藏fisrt列
jTable1.getColumnModel().getColumn(0).setMinWidth(0);
jTable1.getColumnModel().getColumn(0).setPreferredWidth(0);
jTable1.getColumnModel().getColumn(0).setMaxWidth(0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.