编辑 jtable 时更改单元格上的字体

Ial*_*lla 1 java fonts swing jtable tablecelleditor

我正在做一个新的项目,在编辑单元格时遇到问题。当一个单元格被编辑时,字体格式发生变化,我想设置一个特定的格式。

为此,我创建了一个 CellEditor,分配给表格中的每一列。

Cass CellEditor

public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    JComponent component = new JTextField();
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
    ((JTextField)component).setText((String)value);
    ((JTextField)component).setFont(new java.awt.Font("Arial Unicode MS", 0, 16));
    return component;
}
Run Code Online (Sandbox Code Playgroud)

分配给列

private void crearEditor(){
    for (int i = 0; i < tabla.getColumnCount(); i ++) {
        TableColumn col = tabla.getColumnModel().getColumn(i);
        col.setCellEditor(new MyTableCellEditor());
        }
}
Run Code Online (Sandbox Code Playgroud)

并编辑我使用的单元格:

boolean success = tabla.editCellAt(fila, columma);
if (success) {
  boolean toggle = false;
  boolean extend = false;
  tabla.changeSelection(fila, columma, toggle, extend);
}
Run Code Online (Sandbox Code Playgroud)

但格式未设置。任何想法 谢谢

ale*_*410 5

对我有用,这是示例:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

public class TestFrame extends JFrame {

    public static void main(String... s) {
         new TestFrame();
    }

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        final JTable t = new JTable(3,3);
        for (int i = 0; i < t.getColumnCount(); i++) {
            TableColumn col = t.getColumnModel().getColumn(i);
            col.setCellEditor(new MyTableCellEditor());
        }
        t.setRowHeight(20);
        add(new JScrollPane(t));

        JButton b = new JButton("edit");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                boolean success = t.editCellAt(1, 1);
                if (success) {
                  boolean toggle = false;
                  boolean extend = false;
                  t.changeSelection(1, 1, toggle, extend);
                }
            }
        });

        add(b,BorderLayout.SOUTH);
    }

    public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
        private JTextField component = new JTextField();
        private Font font = new Font("Arial Unicode MS", 0, 16);

        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int rowIndex, int vColIndex) {
            component.setText((String) value);
            component.setFont(font);
            return component;
        }

        @Override
        public Object getCellEditorValue() {
            return component.getText();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明