简体   繁体   English

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

[英]Change font on cell while edit jtable

I'm doing a new proyect in swing and have a problem to edit cells. 我正在做一个新的proyect,并且在编辑单元格时遇到问题。 When a cell is being editing, the font format change and I want to set a specific format. 在编辑单元格时,字体格式更改,我想设置特定的格式。

To do so, I've crear a CellEditor, asigned to every column on the table. 为此,我希望将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;
}

Asigned to the columns 分配给列

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

And to edit a cell I do with: 并编辑我要处理的单元格:

boolean success = tabla.editCellAt(fila, columma);
if (success) {
  boolean toggle = false;
  boolean extend = false;
  tabla.changeSelection(fila, columma, toggle, extend);
}

But the format not is set. 但是没有设置格式。 Any idea Thanks Regards 任何想法谢谢

Works for me, here's example: 为我工作,这是示例:

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

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM