Igor
Igor

Reputation: 1592

Table Cell Editor issue

I'm building a custom table cell editor so it adjusts row height during editing. I have this code, but instead of resizing the cell it seams to resize the whole panel, or the frame. When I try to enter a character in a cell the main frame width narrows down to a couple of pixels. Can anyone see the problem?

class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {

    MyTextpane component = new MyTextpane();
    MyTable table;
    private int row;
    private int col;

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
            int rowIndex, int vColIndex) {

        ((MyTextpane) component).setText((String) value);
        component.addKeyListener(new KeyListener1());
        this.table =(MyTable) table;
        this.row = rowIndex;
        this.col = vColIndex;
        return component;
    }

    public Object getCellEditorValue() {
        return ((MyTextpane) component).getText();
    }

    public class KeyListener1 implements KeyListener {

        @Override
        public void keyTyped(KeyEvent ke) {
        }

        @Override
        public void keyPressed(KeyEvent ke) {
        }

        @Override
        public void keyReleased(KeyEvent ke) {
            adjustRowHeight(table, row, col);
        }
        private java.util.List<java.util.List<Integer>> rowColHeight = new ArrayList<java.util.List<Integer>>();
        private void adjustRowHeight(JTable table, int row, int column) {
        //The trick to get this to work properly is to set the width of the column to the
        //textarea. The reason for this is that getPreferredSize(), without a width tries
        //to place all the text in one line. By setting the size with the with of the column,
        //getPreferredSize() returnes the proper height which the row should have in
        //order to make room for the text.
        int cWidth = table.getTableHeader().getColumnModel().getColumn(column).getWidth();
        setSize(new Dimension(cWidth, 1000));
        int prefH = getPreferredSize().height;
        while (rowColHeight.size() <= row) {
            rowColHeight.add(new ArrayList<Integer>(column));
        }
        java.util.List<Integer> colHeights = rowColHeight.get(row);
        while (colHeights.size() <= column) {
            colHeights.add(0);
        }
        colHeights.set(column, prefH);
        int maxH = prefH;
        for (Integer colHeight : colHeights) {
            if (colHeight > maxH) {
                maxH = colHeight;
            }
        }
        if (table.getRowHeight(row) != maxH) {
            table.setRowHeight(row, maxH);
        }
    }

    }
}

Upvotes: 1

Views: 1514

Answers (2)

trashgod
trashgod

Reputation: 205785

As an alternative to resizing the row while editing, consider TablePopupEditor, which uses JTextArea.

Upvotes: 1

mKorbel
mKorbel

Reputation: 109813

have look at

  • my answer about doLayout(could be fired from CellEditor)

  • or (more than confortable way to use TextUtils) comment by @kleopatra about getPreferredSize

  • this could (very) confusing the users,

  • because I miss JScrollPane, there have to override MaxSize, max size is height & weight for JScrollPane, otherwise part of CellEditor can going outside of screeens bounds .........,

  • don't do that, put there JScrollPane with JTextComponents, override PreferredSize for CellEditor,


everything are wrong, my view,

  • create applications modal popup window (based only on JDialog, becasue JWindow doesn't alloved input to the JTextComponent) with JTextComponent, implements there KeyBindings for ESC key, the same for lost Fucus for JDialog, then could be undecorated without any issue

put there Save JButton

  • output from Save Button reditect to the selected cell, you can't lost focus from application modal inside JTable

  • contents should be formatted, filtered, modified one JDialog for all cells from JTable

Upvotes: 1

Related Questions