首页 > 代码库 > 实现Java JTable的应用案例

实现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 = http://www.mamicode.com/getShowValue(value.toString(), width);   "好");           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

实现Java JTable的应用案例