zoukankan      html  css  js  c++  java
  • 实现Java JTable的应用案例

    代码如下

    import Java.awt.Component;   
    import java.awt.Dimension;   
    import java.awt.FontMetrics;   
      
    import javax.swing.JLabel;   
    import javax.swing.JTable;   
    import javax.swing.UIManager;   
    import javax.swing.table.DefaultTableCellRenderer;   
    import javax.swing.table.JTableHeader;   
    import javax.swing.table.TableColumnModel;   
      
    public class Main extends DefaultTableCellRenderer {   
            private static final long serialVersionUID = 1L;
    
            public Component getTableCellRendererComponent(JTable table, Object value,   
                boolean isSelected, boolean hasFocus, int row, int column) {   
      
            JTableHeader header = table.getTableHeader();   
            setForeground(header.getForeground());   
            setBackground(header.getBackground());   
            setFont(header.getFont());   
            setOpaque(true);   
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));   
      
            // 得到列的宽度   
            TableColumnModel columnModel = table.getColumnModel();   
            int width = columnModel.getColumn(column).getWidth();   
      
            value = getShowValue(value.toString(), width);   
            setText(value.toString());   
            setSize(new Dimension(width, this.getHeight()));   
      
            setHorizontalAlignment(JLabel.CENTER);   
      
            return this;   
        }   
      
        private Object getShowValue(String value, int colWidth) {   
            FontMetrics fm = this.getFontMetrics(this.getFont());   
            int width = fm.stringWidth(value.toString());   
            int test = fm.stringWidth("好");   
            System.out.println(test * value.length());   
            System.out.println(width);   
            if (width < colWidth) {   
                return value;   
            }   
            StringBuffer sb = new StringBuffer("<html>");   
            char str;   
            int tempW = 0;   
            for (int i = 0; i < value.length(); i++) {   
                str = value.charAt(i);   
                tempW += fm.charWidth(str);   
                if (tempW > colWidth) {   
                    sb.append("<br>");   
                    tempW = 0;   
                }   
                sb.append(str);   
            }   
            sb.append("</html>");   
            return sb.toString();   
        }   
    }
    

      运行结果
    <ignore_js_op>

    详细说明:http://java.662p.com/thread-3683-1-1.html

  • 相关阅读:
    MySQL优化
    MySQL 的 SQL 操作
    笔记本电脑同时使用两个网络
    top
    logrotate
    正则表达式学习总结
    HttpClient parameter 和body 传输同时进行
    Node.js背景
    前后端分离的理解
    shiro 的subject 以及Context 对象的具体的含义。
  • 原文地址:https://www.cnblogs.com/huasili/p/4102938.html
Copyright © 2011-2022 走看看