Ialberquilla
Ialberquilla

Reputation: 133

Change font on cell while edit jtable

I’m doing a new proyect in swing and have a problem to edit cells. 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.

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

Upvotes: 1

Views: 834

Answers (1)

alex2410
alex2410

Reputation: 10994

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

enter image description here

Upvotes: 5

Related Questions