熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

java中如何實現表格的多表頭顯示

2022-06-13   來源: JSP教程 

  首先聲明這些內容都是借鑒來的稍微改動了一下
  
  主要就是三個應用類ColumnGroup GroupableTableHeaderGroupableTableHeaderU和一個示例類GroupableHeaderExample
  
  ColumnGroup類主要負責多表頭的構建
  
  import javaawtComponent;
  import javaawtDimension;
  import javautilEnumeration;
  import javautilVector;
  
  import javaxswingJLabel;
  import javaxswingJTable;
  import javaxswingUIManager;
  import javaxswingtableDefaultTableCellRenderer;
  import javaxswingtableJTableHeader;
  import javaxswingtableTableCellRenderer;
  import javaxswingtableTableColumn;
  
  public class ColumnGroup {
  protected TableCellRenderer renderer;
  
  protected Vector v;
  
  protected String text;
  
  protected int margin = ;
  
  public ColumnGroup(String text) {
  this(null text);
  }
  
  public ColumnGroup(TableCellRenderer renderer String text) {
  if (renderer == null) {
  thisrenderer = new DefaultTableCellRenderer() {
  public Component getTableCellRendererComponent(JTable table
  Object value boolean isSelected boolean hasFocus
  int row int column) {
  JTableHeader header = tablegetTableHeader();
  if (header != null) {
  setForeground(headergetForeground());
  setBackground(headergetBackground());
  setFont(headergetFont());
  }
  setHorizontalAlignment(JLabelCENTER);
  thissetText((value == null) ? : valuetoString());
  setBorder(UIManagergetBorder(TableHeadercellBorder));
  return this;
  }
  };
  } else {
  thisrenderer = renderer;
  }
  thistext = text;
  v = new Vector();
  }
  
  /**
  * @param obj
  *      TableColumn or ColumnGroup
  */
  public void add(Object obj) {
  if (obj == null) {
  return;
  }
  vaddElement(obj);
  }
  
  /**
  * @param c
  *      TableColumn
  * @param v
  *      ColumnGroups
  */
  public Vector getColumnGroups(TableColumn c Vector g) {
  gaddElement(this);
  if (ntains(c))
  return g;
  Enumeration enumeration = velements();
  while (enumerationhasMoreElements()) {
  Object obj = enumerationnextElement();
  if (obj instanceof ColumnGroup) {
  Vector groups = (Vector) ((ColumnGroup) obj)getColumnGroups(c
  (Vector) gclone());
  if (groups != null)
  return groups;
  }
  }
  return null;
  }
  
  public TableCellRenderer getHeaderRenderer() {
  return renderer;
  }
  
  public Object getHeaderValue() {
  return text;
  }
  
  public int getSize() {
  return v == null ? : vsize();
  }
  
  public Dimension getSize(JTable table) {
  Component comp = renderergetTableCellRendererComponent(table
  getHeaderValue() false false );
  int height = compgetPreferredSize()height;
  int width = ;
  Enumeration enumeration = velements();
  while (enumerationhasMoreElements()) {
  Object obj = enumerationnextElement();
  if (obj instanceof TableColumn) {
  TableColumn aColumn = (TableColumn) obj;
  width += aColumngetWidth();
  width += margin;
  } else {
  width += ((ColumnGroup) obj)getSize(table)width;
  }
  }
  return new Dimension(width height);
  }
  
  /**
  *
  * 創建日期:( ::)
  *
  * @return javalangString
  */
  public javalangString getText() {
  return text;
  }
  
  /**
  * 列表頭中刪除Column 創建日期:( ::)
  */
  public boolean removeColumn(ColumnGroup ptg TableColumn tc) {
  boolean retFlag = false;
  if (tc != null) {
  for (int i = ; i < ptg.v.size(); i++) {
  Object tmpObj = ptg.v.get(i);
  if (tmpObj instanceof ColumnGroup) {
  retFlag = removeColumn((ColumnGroup) tmpObj, tc);
  // 如果找到返回
  if (retFlag)
  break;
  } else if (tmpObj instanceof TableColumn) {
  // 判斷是否查找的對象
  if (tmpObj == tc) {
  ptg.v.remove(i);
  retFlag = true;
  break;
  }
  }
  }
  }
  return retFlag;
  }
  
  /**
  * 列表頭中刪除ColumnGrp 刪除返回true,否則返回false; 創建日期:(2002-9-25 15:49:00)
  */
  public boolean removeColumnGrp(ColumnGroup ptg, ColumnGroup tg) {
  boolean retFlag = false;
  if (tg != null) {
  for (int i = 0; i < ptg.v.size(); i++) {
  Object tmpObj = ptg.v.get(i);
  if (tmpObj instanceof ColumnGroup) {
  // 判斷是否查找的對象
  if (tmpObj == tg) {
  ptg.v.remove(i);
  retFlag = true;
  break;
  } else {
  retFlag = removeColumnGrp((ColumnGroup) tmpObj, tg);
  // 如果找到返回
  if (retFlag)
  break;
  
  }
  } else if (tmpObj instanceof TableColumn) {
  break;
  }
  }
  }
  return retFlag;
  }
  
  public void setColumnMargin(int margin) {
  this.margin = margin;
  Enumeration enumeration = v.elements();
  while (enumeration.hasMoreElements()) {
  Object obj = enumeration.nextElement();
  if (obj instanceof ColumnGroup) {
  ((ColumnGroup) obj).setColumnMargin(margin);
  }
  }
  }
  
  public void setHeaderRenderer(TableCellRenderer renderer) {
  if (renderer != null) {
  this.renderer = renderer;
  }
  }
  
  /**
  *
  * 創建日期:(2003-7-14 10:53:26)
  *
  * @param newText
  *      java.lang.String
  */
  public void setText(java.lang.String newText) {
  text = newText;
  }
  }
  
  2、GroupableHeader繼承自JTableHeader,為多表頭結構的管理者
  
  import java.util.Enumeration;
  import java.util.Vector;
  
  import javax.swing.table.JTableHeader;
  import javax.swing.table.TableColumn;
  import javax.swing.table.TableColumnModel;
  
  
  /**
  * GroupableTableHeader
  * @author flyx
  */
  
  public class GroupableTableHeader extends JTableHeader {
  private static final String uiClassID = "GroupableTableHeaderUI";
  protected Vector columnGroups = null;
  public GroupableTableHeader(TableColumnModel model) {
  super(model);
  setUI(new GroupableTableHeaderUI());
  setReorderingAllowed(false);
  setRequestFocusEnabled(false);
  }
  public void addColumnGroup(ColumnGroup g) {
  if (columnGroups == null) {
  columnGroups = new Vector();
  }
  columnGroups.addElement(g);
  }
  /**
  *
  * 創建日期:(2001-8-31 15:54:29)
  */
  public void clearColumnGroups() {
  columnGroups = null;
  }
  public ColumnGroup[] getColumnGroups() {
  ColumnGroup[] retg=null;
  if (columnGroups.size()>0) {
  retg=new ColumnGroup[columnGroups.size()];
  pyInto(retg);
  }
  return retg;
  }
  public Enumeration getColumnGroups(TableColumn col) {
  if (columnGroups == null)
  return null;
  Enumeration enum = columnGroups.elements();
  while (enum.hasMoreElements()) {
  ColumnGroup cGroup = (ColumnGroup) enum.nextElement();
  Vector v_ret = (Vector) cGroup.getColumnGroups(col, new Vector());
  if (v_ret != null) {
  return v_ret.elements();
  }
  }
  return null;
  }
  /**
  * Identifies whether or not this component can receive the focus.
  * A disabled button, for example, would return false.
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19359.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.