Jtable 显示和隐藏列并保存配置。
Jtable show and hide column and save the configuration.
我正在使用 JXTable,它有一个 setColumnControlVisible(true),它在滚动上方的右上角显示一个按钮,我们可以通过按下它来显示和隐藏列。我想记住应用程序关闭时的更改,但还无法弄清楚,这是我到目前为止所尝试的。我检查了JxTable 的src,但没有如何获取隐藏的列索引或列号。
package paractice;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.swingx.JXTable;
public class TableTest extends JFrame{
private static final long serialVersionUID = 1L;
private JXTable table;
public TableTest() {
setLayout(new BorderLayout());
table = new JXTable(new model());
//add(table.getTableHeader(), BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
table.setColumnControlVisible(true);
setSize(700, 700);
}
public class model extends AbstractTableModel{
String[] columns = {"column1", "column2", "column3", "column4", "column5"};
@Override
public String getColumnName(int column) {
return columns[column];
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
return super.isCellEditable(arg0, arg1);
}
@Override
public void setValueAt(Object arg0, int row, int col) {
super.setValueAt(arg0, row, col);
fireTableCellUpdated(row, col);
}
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return 0;
}
public Object getValueAt(int arg0, int arg1) {
return null;
}
}
public static void main(String args[]) {
TableTest test = new TableTest();
test.setVisible(true);
}
}
but didn't how to get the column index or column number which is hidden.
比较 TableModel 和 JTableHeader。
只需创建一个简单的循环来检查 TableModel 的所有列名称,看看 table 是否包含该列。类似于:
for (int i = 0; i < model.getColumnCount(); i++)
{
Object name = model.getColumnName();
TableColumn column = table.getColumn( name );
if (column == null)
// column is hidden do your processing
}
然后下次显示table时就可以得到所有隐藏列的名称然后使用:
table.removeColumn( table.getColumn( name ) );
我正在使用 JXTable,它有一个 setColumnControlVisible(true),它在滚动上方的右上角显示一个按钮,我们可以通过按下它来显示和隐藏列。我想记住应用程序关闭时的更改,但还无法弄清楚,这是我到目前为止所尝试的。我检查了JxTable 的src,但没有如何获取隐藏的列索引或列号。
package paractice;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.swingx.JXTable;
public class TableTest extends JFrame{
private static final long serialVersionUID = 1L;
private JXTable table;
public TableTest() {
setLayout(new BorderLayout());
table = new JXTable(new model());
//add(table.getTableHeader(), BorderLayout.NORTH);
add(new JScrollPane(table), BorderLayout.CENTER);
table.setColumnControlVisible(true);
setSize(700, 700);
}
public class model extends AbstractTableModel{
String[] columns = {"column1", "column2", "column3", "column4", "column5"};
@Override
public String getColumnName(int column) {
return columns[column];
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
return super.isCellEditable(arg0, arg1);
}
@Override
public void setValueAt(Object arg0, int row, int col) {
super.setValueAt(arg0, row, col);
fireTableCellUpdated(row, col);
}
public int getColumnCount() {
return columns.length;
}
public int getRowCount() {
return 0;
}
public Object getValueAt(int arg0, int arg1) {
return null;
}
}
public static void main(String args[]) {
TableTest test = new TableTest();
test.setVisible(true);
}
}
but didn't how to get the column index or column number which is hidden.
比较 TableModel 和 JTableHeader。
只需创建一个简单的循环来检查 TableModel 的所有列名称,看看 table 是否包含该列。类似于:
for (int i = 0; i < model.getColumnCount(); i++)
{
Object name = model.getColumnName();
TableColumn column = table.getColumn( name );
if (column == null)
// column is hidden do your processing
}
然后下次显示table时就可以得到所有隐藏列的名称然后使用:
table.removeColumn( table.getColumn( name ) );