gpt4 book ai didi

java - 动态更改 JTable 字体大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:58 24 4
gpt4 key购买 nike


编辑:[已解决]:似乎每次我尝试增大和减小字体大小时都会调用 CustomTableCellRenderer 的 getTableCellRendererComponent。

所以我更新了我的代码以处理此方法本身的字体大小。我正在使用静态变量来跟踪当前字体大小(这是因为该值会影响当前显示在我的 UI 中的所有表格的字体大小)

我已经更新了代码来处理这个问题。欢迎对此提出任何意见/反馈。再次感谢!


我要解决的问题是根据对不同 JLabel 的单击动态更改(即增加/减小)JTable 中的字体大小

我有一个面板,其中包含两个标签(用于增大/减小字体大小)和一个 JTable。我为该表附加了一个自定义 TableCellRenderer。

创建表的代码如下:

JTable table = new JTable(data, tableHeader){
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row,
int column) {
Component component = super.prepareRenderer(renderer, row, column);
int rendererWidth = component.getPreferredSize().width;
TableColumn tableColumn = getColumnModel().getColumn(column);
tableColumn.setPreferredWidth(Math.max(rendererWidth +
getIntercellSpacing().width + 10,
tableColumn.getPreferredWidth()));
return component;
}

};
table.setPreferredScrollableViewportSize(new Dimension(5000, 200));
table.setFillsViewportHeight(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setDefaultRenderer(table.getColumnClass(0), new CustomTableCellRenderer());

自定义的TableCellRenderer实现如下:

public class CustomTableCellRenderer extends DefaultTableCellRenderer {

private Font customFont;

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

customFont = component.getFont();
customFont = new Font(customFont.getName(), customFont.getStyle(), MainUI.DEFAULT_TABLE_FONT_SIZE);
component.setFont(customFont);

if (value.toString().matches("(?i:.*error.*)")) {
c.setForeground(Color.RED);
} else {
c.setForeground(Color.BLACK);
}
return c;
}

public void increaseFontSize(){
MainUI.DEFAULT_TABLE_FONT_SIZE = getFont().getSize() + 1;
}

public void decreaseFontSize(){
MainUI.DEFAULT_TABLE_FONT_SIZE = getFont().getSize() + 1;
}

}

对于按钮,我附加了鼠标监听器和 mouseClicked 事件,我正在尝试执行以下操作以实现功能:

if(table.getDefaultRenderer(table.getColumnClass(0)) instanceof CustomTableCellRenderer){
CustomTableCellRenderer tableCellRenderer = (CustomTableCellRenderer) table.getDefaultRenderer(table.getColumnClass(0));
tableCellRenderer.increaseFontSize();
table.repaint();
}

但是我没有看到表格中有任何变化。我究竟做错了什么?我如何实现所需的功能?

如有任何意见/建议,我们将不胜感激。谢谢!

最佳答案

  • 基本上你必须设置 JTable#setFont()

  • 在渲染器中你可以增加 FontSize 取决于

    a) TableCell 中的值

    b) 行或列索引

    c) isSelected 和 hasFocus

  • 在渲染器中最好覆盖 isSelected 和 hasFocus

  • 对于不更改 TabelModel 和 ColumnModel 的 JTable 以独占使用 prepareRender(我认为我喜欢 prepareRenderer)

编辑

我会更改 JTable#setFont(),而不是将其增加到渲染器中,然后将更改后的字体应用于行、列或选择

enter image description here

enter image description here

enter image description here

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 = (table.getFont()).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(table.getFont());
}
} 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);
add(scrollPane);
JButton increase = new JButton("+");
increase.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Font font = table.getFont();
font = font.deriveFont((float) (font.getSize2D() * 1.10));
table.setFont(font);
}
});
add(increase, BorderLayout.EAST);
JButton decrease = new JButton("-");
decrease.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Font font = table.getFont();
font = font.deriveFont((float) (font.getSize2D() * .90));
table.setFont(font);
}
});
add(decrease, BorderLayout.WEST);
}

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);
}
});
}
}

关于java - 动态更改 JTable 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11146319/

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