/**
* @author chega
* this class extends JTable and has two sub JTable class in it ,they are:RowHeadTable and LeftTopCornor,they are also JTable,
* RowHeadTable is the rowHeadView of JScrollPane ,and LeftTopCornor is the upper-top cornor of JScrollPane, you can use
* TableWithRowHead like this:<br/>
* <b>
*
* JScrollPane pane = new JScrollPane(table);<br/><br/>
* pane.setRowHeaderView(table.getRowHeadTable());<br/><br/>
* pane.setCorner(JScrollPane.UPPER_LEFT_CORNER, table.getLeftTopCornor());<br/><br/>
* </b>
* If you select a row in RowHeadTable ,then the row at the same row index of TableWithRowHead will also be selectd,so vice-verse
* if you select the only row in LeftTopCornor , the all rows in TableWithRowHead and RowHeadTable will be selected.
* <br/>
* for some reason , the LeftTopTable and RowHeadTable set selectionBackground to white, you can also set it to other new value, but
* some details should be considered.
* <br/>
* <br/>
* <b>Attention:</b>
* some method should not be invoked:table.setRowHeight() and table.getTableHeader().setHeight(),
* alternative methods are supplied:setTableRowHeight() setTableHeadRowHeight().
*/
public class TableWithRowHead extends JTable{
private static final long serialVersionUID = 1L;
private RowHeadTable rowHead = null;
private TableWithRowHead table = null;
private LeftTopCornor leftTopCornor;
public static void main(String...args){
JFrame frame = new JFrame();
final TableWithRowHead table = new TableWithRowHead(new DefaultTableModel(5,5));
table.setAutoCreateRowSorter(true);
table.hideColumn(0);
JScrollPane pane = new JScrollPane(table);
pane.setRowHeaderView(table.getRowHeadTable());
pane.setCorner(JScrollPane.UPPER_LEFT_CORNER, table.getLeftTopCornor());
frame.add(pane,BorderLayout.CENTER);
JButton jb = new JButton("print selected rows");
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
print(table.getSelectedRows());
print(table.getRealRows());
}
private void print(int[] selectedRows) {
System.out.println("===================");
for(int i=0;i<selectedRows.length;i++){
System.out.println(selectedRows[i]+"\t");
}
}
});
frame.add(jb,BorderLayout.SOUTH);
frame.setSize(500,400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public TableWithRowHead(Vector data, Vector columns){
super(data,columns);
this.init();
}
public TableWithRowHead(int row,int col){
super(row,col);
this.init();
}
public TableWithRowHead(DefaultTableModel model){
super(model);
this.init();
}
/**
* this method must be invoked in constructor
*/
private void init(){
this.table = this;
this.rowHead = new RowHeadTable();
this.leftTopCornor = new LeftTopCornor();
TableSelectionListener selectionListener = new TableSelectionListener();
this.table.getSelectionModel().addListSelectionListener(selectionListener);
TableMouseListener l = new TableMouseListener();
this.rowHead.addMouseListener(l);
this.leftTopCornor.addMouseListener(l);
}
public JTable getRowHeadTable(){
return this.rowHead;
}
public JTable getLeftTopCornor(){
return this.leftTopCornor;
}
/**
* make the row in RowHeadTable selected
* @param row
*/
public void setSelected(int row){
this.rowHead.setValueAt(Boolean.TRUE, row, 0);
// this.rowHead.addRowSelectionInterval(row, row);
}
/**
* 设置行选中(this method is for TableWithRowHead)
* @param rows
*/
public void setSelected(int[] rows){
this.rowHead.clearSelected();
this.rowHead.clearSelection();
for(int i=0;i<rows.length;i++){
this.setSelected(rows[i]);
}
//如果选中全部了,则leftTopCornor也选中,否则不选中
if(rows.length==table.getRowCount()){
this.leftTopCornor.setValueAt(Boolean.TRUE, 0, 0);
this.leftTopCornor.selectAll();
}else{
this.leftTopCornor.setValueAt(Boolean.FALSE, 0, 0);
this.leftTopCornor.clearSelection();
}
}
/**
* 返回选中的行(this method is for TableWithRowHead)
*/
public int[] getRealRows(){
int[] selectedRows = this.getSelectedRows();
int[] realRows = new int[selectedRows.length];
for(int i=0;i<realRows.length;i++){
realRows[i] = this.convertRowIndexToModel(selectedRows[i]);
}
return realRows;
}
/**
* 隐藏某列
* @param col
*/
public void hideColumn(int col){
JTableHeader tableHeader = this.getTableHeader();
tableHeader.getColumnModel().getColumn(col).setPreferredWidth(0);
tableHeader.getColumnModel().getColumn(col).setMaxWidth(0);
tableHeader.getColumnModel().getColumn(col).setMinWidth(0);
table.getColumnModel().getColumn(col).setPreferredWidth(0);
table.getColumnModel().getColumn(col).setMaxWidth(0);
table.getColumnModel().getColumn(col).setMinWidth(0);
}
/**
* set row height to a new value <b>height</b>
* @author chega
*
*/
public void setTableRowHeight(int height){
this.table.setRowHeight(height);
this.rowHead.setRowHeight(height);
}
/**
*
* @param row
* @param height
*/
public void setTableRowHeight(int row,int height){
this.table.setRowHeight(row,height);
this.rowHead.setRowHeight(row, height);
}
/**
* set the table header height
*/
public void setTableHeadHeight(int height){
this.table.getTableHeader().setPreferredSize(new Dimension(0,height));
this.leftTopCornor.setRowHeight(height);
}
//==============innner class========================
private class RowHeadTable extends JTable{
public RowHeadTable(){
super();
this.setAlignmentX(SwingConstants.CENTER);
this.setSelectionBackground(Color.white);
DefaultTableModel model = new RowHeadTableModel(table.getRowCount(),1);
this.setModel(model);
this.getColumnModel().getColumn(0).setPreferredWidth(16);
this.setPreferredScrollableViewportSize(new Dimension(16,0));
}
private void clearSelected(){
for(int i=0;i<this.getRowCount();i++){
this.setValueAt(Boolean.FALSE, i, 0);
}
}
}
private class LeftTopCornor extends JTable{
public LeftTopCornor(){
super();
this.setToolTipText("全选");
this.setAlignmentX(SwingConstants.CENTER);
this.setSelectionBackground(Color.white);
DefaultTableModel model = new RowHeadTableModel(1,1);
this.setModel(model);
this.setRowHeight(table.getTableHeader().getPreferredSize().height);
}
}
private class RowHeadTableModel extends DefaultTableModel{
public RowHeadTableModel(int row,int col){
super(row,col);
}
@Override
public boolean isCellEditable(int row,int col){
return true;
}
@Override
public Class<?> getColumnClass(int col){
return Boolean.class;
}
}
class TableSelectionListener implements ListSelectionListener{
@Override
public void valueChanged(ListSelectionEvent e) {
if(e.getValueIsAdjusting()){
return ;
}
// int[] selectedRows = table.getRealRows();
int[] selectedRows = table.getSelectedRows();
table.setSelected(selectedRows);
}
}
class TableMouseListener extends MouseAdapter{
@Override
public void mouseReleased(MouseEvent e){
if(e.getSource() instanceof RowHeadTable){
int row = rowHead.rowAtPoint(new Point(e.getX(),e.getY()));
if(row==-1){
return ;
}
if((Boolean)rowHead.getValueAt(row, 0)){
table.addRowSelectionInterval(row,row);
}else{
table.removeRowSelectionInterval(row, row);
leftTopCornor.setValueAt(Boolean.FALSE, 0, 0);
leftTopCornor.clearSelection();
}
}
else if(e.getSource() instanceof LeftTopCornor){
if((Boolean)leftTopCornor.getValueAt(0, 0)){//select all
table.selectAll();
}else{
table.clearSelection();
leftTopCornor.clearSelection();
rowHead.clearSelection();
}
}
}
}
}
分享到:
相关推荐
- **行标题(ColumnHeader)**:表格的列标题部分,可以通过调用 `getTableHeader()` 方法获取。 - **行对象(Column Object)**:表格的具体数据部分。 在上述示例中,我们首先创建了一个包含数据和列名的表格,...
在标题提到的"Swing 多选栏控件"中,很可能是指使用了`JList`的多选模式,或者可能是定制的组件,如带有左右两部分的双列选择器,用户可以通过双击将项目在两列之间移动。 1. **JList**:`JList`是一个可以显示一组...
实例代码可能会展示如何创建带有文本或图标的按钮,以及如何处理按钮点击事件。 3. **JLabel** - JLabel用于显示文本、图像或两者组合。你将看到如何创建和配置标签,以及如何动态更新标签内容。 4. **...
1. **JTable增强**:SwingX提供了JXTable,它扩展了标准的JTable,增加了诸如行选择模式、列排序和过滤、表格编辑等高级功能。 2. **JToolBar增强**:JXToolBar提供了更丰富的定制选项,允许开发者创建更复杂的工具...
- 实现日程安排通常需要一个`Event`类,包含事件的开始时间、结束时间、标题、描述等属性。 - 使用`Date`或`Calendar`对象来存储事件的时间,便于比较和排序。 - 可能会有一个`Schedule`类或接口,用于管理多个...
- **多选支持**:KTABLE允许用户同时选择多个行或单元格,这对于数据操作和批量处理非常有用。 - **自定义列类型**:开发者可以定义自己的列类型,比如日期、数字、图片等,以展示不同类型的数据。 - **高效渲染*...
实例045 用数组设置JTable表格的列名与列宽 55 3.2 数组操作 57 实例046 数组的下标界限 57 实例047 按钮控件数组实现计数器界面 58 实例048 复选框控件数组 59 实例049 用数组反转字符串 60 3.3 数组排序与查询 61 ...