简体   繁体   中英

How to modify javax.swing.TreeCellRenderer in order to stroke the text in the cell

有人知道如何修改javax.swing.TreeCellRenderer以便在单元格中描边文本吗?

If you want to get stroked out text in some columns, you should use the renderer. If you need this font for all cells, you can simply modify the font of the table. Here is the example for both variants:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.font.TextAttribute;
import java.util.Collections;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("unchecked")
public class TableRendererTest {

    public static void main(String[] args) {
        JFrame frm = new JFrame("Renderer test");
        DefaultTableModel model = new DefaultTableModel(new String[] {"First", "Second", "Third" }, 3);
        model.setValueAt("Test String", 0, 0);
        model.setValueAt("Corner String", 2, 0);
        model.setValueAt("Last cell", 2, 2);

        // table with strike-out renderer (first column is stroked out)
        JTable tbl = new JTable(model);
        tbl.getColumnModel().getColumn(0).setCellRenderer(new StrikeOutRenderer());
        frm.add(new JScrollPane(tbl), BorderLayout.NORTH);

        // table with strike-out font (all cells are stroked out)
        JTable another = new JTable(model);
        another.setFont(
                another.getFont().deriveFont(Collections.singletonMap(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON)));
        frm.add(new JScrollPane(another), BorderLayout.SOUTH);
        frm.pack();
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private static class StrikeOutRenderer extends DefaultTableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component res = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            res.setFont(res.getFont().deriveFont(Collections.singletonMap(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON)));
            return res;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM