gpt4 book ai didi

java - CellRenderer - 使文本变粗

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:25 25 4
gpt4 key购买 nike

如何使 JTable 中的文本变为粗体,我尝试了以下方法,它确实适用于颜色,但为了使文本变为粗体,我在某处出错了

    class ColourRender extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable tblData,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component cellComponent = super.getTableCellRendererComponent(
tblData, value, isSelected, hasFocus, row, column);

if (tblData.getValueAt(row, 3).equals("M")) {
cellComponent.setForeground(forMen);

} else {
cellComponent.setForeground(forWomen);

}
return cellComponent;
}

class boldRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(JTable tblData,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component cellComponent = super.getTableCellRendererComponent(
tblData, value, isSelected, hasFocus, row, column);

if (tblData.getValueAt(row, 1).equals(bib)) {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.BOLD));
} else {
cellComponent.setFont(cellComponent.getFont().deriveFont(
Font.PLAIN));
}

return cellComponent;
}
}
}

这个在与表相关的类中

tblData.getColumn("Bib no").setCellRenderer(new CustomRenderer());
tblData.getColumn("M/F").setCellRenderer(new CustomRenderer());

最佳答案

你创建了一个粗体字体但什么都不做:你永远不会在任何地方调用 setFont(...)

即,

if (tblData.getValueAt(row, 1).equals(bib)) {
// you must call setFont(...) for a font to change
cellComponent.setFont(cellComponent.getFont().deriveFont(Font.BOLD));
} else {
// don't forget to set the font back if condition not true
cellComponent.setFont(cellComponent.getFont().deriveFont(Font.PLAIN));
}

// changing foreground color:
if (tblData.getValueAt(row, 3).equals("M")) {
cellComponent.setForeGround(MEN_COLOR); // a constant
} else {
cellComponent.setForeGround(myTable.getForeground());
}

对于颜色,在您的 else block 中将前景色设置为 null 以使您的颜色变回默认值。


编辑
例如:

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class CustomRendererExample extends JPanel {
public static final String[] COLUMNS = { "M/F", "Fubar" };
public static final String[] FUBARS = { "Fubar", "Snafu" };
public static final String[] M_F = { "M", "F" };
protected static final Color M_COLOR = Color.red;
private DefaultTableModel dm = new DefaultTableModel(COLUMNS, 5);
private JTable table = new JTable(dm);

public CustomRendererExample() {
add(new JScrollPane(table));
DefaultCellEditor mfEditor = new DefaultCellEditor(new JComboBox<>(M_F));
table.getColumn(COLUMNS[0]).setCellEditor(mfEditor);
table.getColumn(COLUMNS[0]).setCellRenderer(
new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component superRenderer = super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);

if (value != null && value.equals(M_F[0])) {
superRenderer.setForeground(M_COLOR);
} else {
superRenderer.setForeground(null);
}

return superRenderer;
}
});

DefaultCellEditor fubarEditor = new DefaultCellEditor(new JComboBox<>(
FUBARS));
table.getColumn(COLUMNS[1]).setCellEditor(fubarEditor);
table.getColumn(COLUMNS[1]).setCellRenderer(
new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component superRenderer = super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);

if (value != null && value.equals(FUBARS[0])) {
superRenderer.setFont(superRenderer.getFont().deriveFont(Font.BOLD));
} else {
superRenderer.setFont(superRenderer.getFont().deriveFont(Font.PLAIN));
}

return superRenderer;
}
});

}

private static void createAndShowGui() {
CustomRendererExample mainPanel = new CustomRendererExample();

JFrame frame = new JFrame("Custom Renderer Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

关于java - CellRenderer - 使文本变粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22325138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com