作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何使 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/
当用户按下我的 TextView 时,我想将 TextView 设为粗体。这就是我正在尝试的: // styles.xml bold normal // my_selecto
我希望能够将 TextView 设为粗体。这就是我设置它的外观的方式(我需要在代码中这样做): nameText.setTextAppearance(getApplicationContext(),
我需要将这个 UIBarButtonItem 文本设为粗体。我听起来很简单,但我想不通。 let nextButton = UIBarButtonItem(title: "Login", style:
我正在使用 CSS 动画在页面加载时淡入一些元素。动画完成后,文本“弹出”并变得比我想要的更粗。 更让我困惑的是,它似乎只在文本为某种红色阴影时才会发生。 运行下面的代码片段以查看它的实际效果。请注意
我是一名优秀的程序员,十分优秀!