Edit: [Solved] : It seems that the getTableCellRendererComponent of the CustomTableCellRenderer is called every time I'm trying to increase and decrease the font size.
So I updated my code to take care of font size in this method itself. I'm using a static variable to keep track of the current font size ( this is because this value will affect the font size of all tables that are currently being shown in my UI )
I've updated the code to take care of this. Any comments/feedback on this would be welcome. Thanks once again!
The issue that I'm trying to solve is to dynamically change (ie increase/decrease) the font size in a JTable based on a click on a different JLabel
I have a panel that hosts two labels (for increasing/decreasing font size) and a JTable. To this table I've attached a custom TableCellRenderer.
The code for creating the table is as follows:
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());
The custom TableCellRenderer is implemented as follows:
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;
}
}
For the button's I've attached a mouse listener and on mouseClicked event, I'm trying to do the following to achieve the functionality:
if(table.getDefaultRenderer(table.getColumnClass(0)) instanceof CustomTableCellRenderer){
CustomTableCellRenderer tableCellRenderer = (CustomTableCellRenderer) table.getDefaultRenderer(table.getColumnClass(0));
tableCellRenderer.increaseFontSize();
table.repaint();
}
However I see no change in the table. What am I doing wrong? And how do I achieve the desired functionality?
Any comments/suggestions would be really appreciated. Thanks!
basically you have to set JTable#setFont()
in the Renderer you can increase FontSize depends of
a) value in the TableCell
b) row or column index
c) isSelected and hasFocus
in the Renderer is better to override for isSelected and hasFocus
for JTable without changes in the TabelModel and ColumnModel to use prepareRender exclusivelly (my view I love prepareRenderer)
EDIT
I'd be to change JTable#setFont(), rather than increase that into Renderer, then to apply changed Font to the Row, Column or Selection
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);
}
});
}
}
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.