Java jtable用法详解_极悦注册
专注Java教育14年 全国咨询/投诉热线:444-1124-454
极悦LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java jtable用法详解

Java jtable用法详解

更新时间:2022-11-11 10:56:46 来源:极悦 浏览2078次

Java jtable如何使用?极悦小编来告诉大家。

设置单元格的渲染器为复选框,如下:

//设置表格和表头的默认渲染器
simpleTable.setDefaultRenderer(Object.class, simpleTableRender);
simpleTable.getTableHeader().setDefaultRenderer(headerRenderer);
//设置单元格的渲染器
checkBoxRenderer = new CheckBoxRenderer(simpleTable);       
simpleTable.getColumnModel().getColumn(0).setCellRenderer(checkBoxRenderer);

设置单元格的渲染器和编辑器为下拉框,如下:

simpleTable.getColumnModel().getColumn(3).setCellRenderer(comboBoxRenderer);      
simpleTable.getColumnModel().getColumn(3).setCellEditor(comboBoxCellEditor); 

设置排序方法,如下:

sorter = new TableRowSorter<LogonStatusTableModel>((LogonStatusTableModel) simpleTable.getModel());
simpleTable.setRowSorter(sorter);
ArrayList<SortKey> list = new ArrayList<SortKey>();
list.add(new TableRowSorter.SortKey(1, SortOrder.ASCENDING));
sorter.setSortKeys(list);
sorter.sort();
sorter.setSortable(0, false);

设置表头的排序标识按钮,如下:

simpleTable.getTableHeader().addMouseListener(new HeaderMouseListener());
class HeaderMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            JTableHeader h = (JTableHeader) e.getSource();
            TableColumnModel columnModel = h.getColumnModel();
            TableCellRenderer tcr = h.getDefaultRenderer();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX());
            if (viewColumn < 0)
                return;
            int column = columnModel.getColumn(viewColumn).getModelIndex();
            if (column != -1 && tcr instanceof LogonStatusContentHeaderRenderer) {
                LogonStatusContentHeaderRenderer sbr = (LogonStatusContentHeaderRenderer) tcr;
                sbr.setSelectedColumn(column);
            }
        }
        @Override
        public void mouseReleased(MouseEvent e) {
            ((JTableHeader) e.getSource()).repaint();
        }
    }

设置数据返回值类型,如下:

//返回布尔值
@Override  
public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0)
            return Boolean.class;
        else
            return super.getColumnClass(columnIndex);
}
//返回Double类型,排序时会按Double类型排序
@Override
public Class<?> getColumnClass(int columnIndex) {
        if(columnIndex==2){
            return Double.class;
        }else
            return super.getColumnClass(columnIndex);
}

设置某一列是否可以编辑,如下:

public boolean isCellEditable(int row, int column) {
        if (row >= 0) {
            SysParamStruct info = localMap.get(String.valueOf(row));
            if (info == null) {
                System.out.println("info   null");
            }
            if (info != null) {
                if (columnsOrder.elementAt(column) == EnumSysParaColumn.VALUE) {
                    return true;
                }
                if (columnsOrder.elementAt(column) == EnumSysParaColumn.STATUS) {
                    return true;
                }
            }
        }
        return false;
    }

设置列的值,如下:

public Object getValueAt(int row, int column) {
        if (row >= 0) {
            SysParamStruct info = localMap.get(String.valueOf(row));
            if (info == null) {
                System.out.println("info   null");
            }
            if (info != null) {
                if (columnsOrder.elementAt(column) == EnumSysParaColumn.PARA_NAME) {
                    return info.getShortDesc();
                } else if (columnsOrder.elementAt(column) == EnumSysParaColumn.PARA_DESC) {
                    return info.getLongDesc();
                } else if (columnsOrder.elementAt(column) == EnumSysParaColumn.VALUE) {
                    return info.getValue();
                }  else if (columnsOrder.elementAt(column) == EnumSysParaColumn.STATUS) {
                    return info.getStatus();
                }            
            }           
        }
        return "";
    }

使编辑的列值生效,如下:

public void setValueAt (Object value, int row, int column) {
        SysParamStruct info = localMap.get(String.valueOf(row));
        if (columnsOrder.elementAt(column) == EnumSysParaColumn.VALUE) {
            info.setValue((String) value);
            localMap.put(String.valueOf(row), info);
        }
        if (columnsOrder.elementAt(column) == EnumSysParaColumn.STATUS) {
            char status = 0;
            switch ((StatusType)value) {
                case Active :
                    status = StatusType.Active.getValue();
                    break;
                case Suspended :
                    status = StatusType.Suspended.getValue();              
                    break;
                case Deleted :
                    status = StatusType.Deleted.getValue();
                    break;
                default :
                    break;
            }
            info.setStatus(status);
            localMap.put(String.valueOf(row), info);
        }
        fireTableCellUpdated(row, column);
    }

ComboBoxRenderer里面的内容,如下:

@Override
    public Component getTableCellRendererComponent(
        JTable table,
        Object value,
        boolean isSelected,
        boolean hasFocus,
        int row,
        int column) {
        oddColor = ResManager.getColor("Trade.OrderBook.TableCell.OddColor");
        evenColor = ResManager.getColor("Trade.OrderBook.TableCell.EvenColor");
        fgColor = ResManager.getColor("Trade.OrderBook.TableCell.textColor");
        highlightBgColor = HVResMgr.getColor("Trade.OrderBook.TableCell.HighlightColor");
        //set line background color
        if ((row % 2) == 0) {
            bgColor = oddColor;
        }
        else {
            bgColor = evenColor;
        }
        // set highlight color
        if (isSelected) {
            bgColor = highlightBgColor;
        }
        //calculate the font size
        double ratio = 1.0;
        if (table instanceof BaseTable) {
            ratio = ((BaseTable) table).getRatio();
        }
        int fontSize = (int) (font.getSize() * ratio);
        font = new Font(font.getName(), font.getStyle(), fontSize);
        this.comboBox.setBorder(null);
        this.comboBox.setBackground(bgColor);
        this.comboBox.setForeground(fgColor);
        if (column == 3) {
            StatusType status = null;
            switch ((Character) value) {
                case 'A' :
                    status = StatusType.Active;
                    break;
                case 'S' :
                    status = StatusType.Suspended;
                    break;
                case 'D' :
                    status = StatusType.Deleted;
                    break;
                default :
                    break;
            }
            this.comboBox.setSelectedItem(status);
        }
        return this.comboBox;
    }

ComboBoxCellEditor里面的内容,如下:

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        if (column == 3) {
            StatusType status = null;
            switch ((Character) value) {
                case 'A' :
                    status = StatusType.Active;
                    break;
                case 'S' :
                    status = StatusType.Suspended;
                    break;
                case 'D' :
                    status = StatusType.Deleted;
                    break;
                default :
                    break;
            }
            this.comboBox.setSelectedItem(status);
        }
        return this.comboBox;
    }
    @Override
    public Object getCellEditorValue() {
        return this.comboBox.getSelectedItem();
    }

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>