我正在尝试使用Jtable显示数据(英语和非英语语言的组合)。
由于我也有非英语数据,因此我尝试为每个单元格(jtextfield)设置字体。
我还想在Jtable中渲染后编辑数据,但是我无法在jtext fileds上实现焦点侦听器事件(即,未调用focusgained()中的代码)。
我已使用以下渲染器在jtable中以每个单元格作为文本字段显示数据
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JTextField tf=new JTextField();
if (value != null) {
if (value.startsWith("_")) {
value=value.substring(1);
tf.setFont(fntEng);
} else if (fnt_Local != null && fnt_Local.canDisplayUpTo(value.toString()) == -1) {
tf.setFont(fnt_Local);
}
setText(value.toString());
} else {
tf.setFont(fnt_Local);
tf.setText((String) value);
}
tf.setForeground(Color.BLACK);
tf.setBackground(Color.WHITE);
tf.setText(value.toString());
tf.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
JTextField tf1=(JTextField)e.getComponent();
tf1.setText("Focusgained");
}
public void focusLost(FocusEvent e) {
}
});
return tf;
}
};
最佳答案
如果我正确地理解了您的问题,那么您只需要编辑时使用的相同字体以及要重新显示的字体。而且您无法使编辑器正常工作。
首先...
不要混淆编辑器和渲染器的概念。渲染应留给渲染器,而编辑应留给编辑器。有关更多阅读,请参见See How to use Tables: Editors and Renderers
至于你的情况
渲染图
无需自定义渲染器即可完成设置字体之类的简单操作。只需覆盖prepareRenderer
。您可以使用column
值确定哪一列应为哪种字体。例如
JTable table = new JTable(data, cols) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
if (col == IMPACT_COL) {
c.setFont(IMPACT_FONT);
} else if (col == ARIAL_COL) {
c.setFont(ARIAL_BOLD_FONT);
}
return c;
}
};
编辑中
只需将字体设置为文本字段,然后为所需的列设置一个新的单元格编辑器,以使其与您选择用来呈现该列的字体相同。这样,您将在编辑时以及在渲染时看到相同的字体。就像是:
private static final int IMPACT_COL = 0;
private static final Font IMPACT_FONT = new Font("impact", Font.PLAIN, 20);
...
JTextField impactField = getFontEditorField(IMPACT_FONT);
TableColumn impactColumn = table.getColumnModel().getColumn(IMPACT_COL);
impactColumn.setCellEditor(new DefaultCellEditor(impactField));
...
public JTextField getFontEditorField(Font font) {
JTextField field = new JTextField();
field.setFont(font);
return field;
}
完整的例子
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class ChangeFontInEditorDemo {
private static final int IMPACT_COL = 0;
private static final int ARIAL_COL = 1;
private static final Font IMPACT_FONT = new Font("impact", Font.PLAIN, 20);
private static final Font ARIAL_BOLD_FONT = new Font("arial", Font.BOLD, 20);
public ChangeFontInEditorDemo() {
JFrame frame = new JFrame();
frame.add(new JScrollPane(getTable()));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JTable getTable() {
String[][] data = { { "Data", "Data" }, { "Data", "Data" } };
String[] cols = { "Col", "Col" };
JTable table = new JTable(data, cols) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
Component c = super.prepareRenderer(renderer, row, col);
if (col == IMPACT_COL) {
c.setFont(IMPACT_FONT);
} else if (col == ARIAL_COL) {
c.setFont(ARIAL_BOLD_FONT);
}
return c;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(300, 125);
}
};
table.setRowHeight(20);
JTextField impactField = getFontEditorField(IMPACT_FONT);
TableColumn impactColumn = table.getColumnModel().getColumn(IMPACT_COL);
impactColumn.setCellEditor(new DefaultCellEditor(impactField));
JTextField arialBoldField = getFontEditorField(ARIAL_BOLD_FONT);
TableColumn arialColumn = table.getColumnModel().getColumn(ARIAL_COL);
arialColumn.setCellEditor(new DefaultCellEditor(arialBoldField));
return table;
}
public JTextField getFontEditorField(Font font) {
JTextField field = new JTextField();
field.setFont(font);
return field;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ChangeFontInEditorDemo();
}
});
}
}