栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jtable中整个行的删除线

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jtable中整个行的删除线

为什么是HTML,为什么不直接使用TextAttribute

充分尊重HFOE,并分享他的知识(forums.sun.com)

从代码

import java.awt.*;import java.awt.font.TextAttribute;import java.util.Map;import javax.swing.*;import javax.swing.border.Border;import javax.swing.border.CompoundBorder;import javax.swing.border.EmptyBorder;import javax.swing.border.MatteBorder;import javax.swing.table.*;public class TablePrepareRenderer extends Jframe {    private static final long serialVersionUID = 1L;    private JTable table;    public TablePrepareRenderer() {        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};        Object[][] data = { {"Buy", "IBM", new Integer(1000), new Double(80.50), false}, {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true}, {"Sell", "Apple", new Integer(3000), new Double(7.35), true}, {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}        };        DefaultTableModel model = new DefaultTableModel(data, columnNames) { private static final long serialVersionUID = 1L; @Override public Class getColumnClass(int column) {     return getValueAt(0, column).getClass(); }        };        table = new JTable(model) { private static final long serialVersionUID = 1L; private Border outside = new MatteBorder(1, 0, 1, 0, Color.red); private Border inside = new EmptyBorder(0, 1, 0, 1); private Border highlight = new CompoundBorder(outside, inside); @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {     Component comp = super.prepareRenderer(renderer, row, column);     JComponent jc = (JComponent) comp;     Map attributes = (new Font("Serif", Font.PLAIN, 12)).getAttributes();     //attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);     attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);     if (!isRowSelected(row)) {         comp.setForeground(Color.black);         comp.setBackground(row % 2 == 0 ? Color.white : Color.orange);         int modelRow = convertRowIndexToModel(row);         String type = (String) getModel().getValueAt(modelRow, 0);         if (type.equals("Sell")) {  comp.setFont(new Font(attributes));  comp.setForeground(Color.red);         } else {  comp.setFont(new Font("Serif", Font.BOLD, 12));         }     } else {         comp.setFont(table.getFont());     }     jc.setBorder(BorderFactory.createCompoundBorder(jc.getBorder(), BorderFactory.createEmptyBorder(0, 0, 0, 5)));     return comp; }        };        table.setPreferredScrollableViewportSize(table.getPreferredSize());        JScrollPane scrollPane = new JScrollPane(table);        getContentPane().add(scrollPane);    }    public static void main(String[] args) {        SwingUtilities.invokeLater(new Runnable() { @Override public void run() {     TablePrepareRenderer frame = new TablePrepareRenderer();     frame.setDefaultCloseOperation(EXIT_ON_CLOSE);     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true); }        });    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号