2

I am writing a small POS application, which shows a JTable with ticket information inside its cells. CellRenderer is a class which extends JPanel and implements TableCellRenderer, and contains a few JTextFields showing basic information (quantity, descripcion, price). Also, I have another class extending JPanel and implementing TableCellEditor, which is used as CellEditor. This class contains more JTextFields and also a few jButtons.

What I need is simple: when I edit any cell by clicking with the mouse (or touching the screen, which is, as far as I know, the same event), dynamically increase the height of the cell I'm going to edit, so it can show all the components inside the editor. And when I finish editing, return cell height to its previous value.

Any idea about doing it?

Thanks in advance. :-)

mKorbel
  • 109,525
  • 20
  • 134
  • 319
argabel
  • 21
  • 2
  • 1
    hmm ... agree with @mKorbel that jumpy rowHeights might get irritating for users. Would it be an option to only over-size (that is make it cover more area than its normal cell area) the editing component? – kleopatra Oct 04 '12 at 13:11

3 Answers3

1

CellRenderer is a class which extends JPanel and implements TableCellRenderer, and contains a few JTextFields showing basic information (quantity, descripcion, price). Also, I have another class extending JPanel and implementing TableCellEditor, which is used as CellEditor. This class contains more JTextFields and also a few jButtons.

better could be to create popup window (JDialog) based on event from JPopupMenu,

Dynamically Increase JTable row height when editing, and decrease when finish edit

don't confused users and wrong concept could be caused by jumping JTables row on the screen

What I need is simple: when I edit any cell by clicking with the mouse (or touching the screen, which is, as far as I know, the same event), dynamically increase the height of the cell I'm going to edit, so it can show all the components inside the editor. And when I finish editing, return cell height to its previous value.

don't do that, but have to override, is possible by

  • DefaultCellEditor#setClickCountToStart(int) for TableCellEditor

  • start, stop and cancelEdit for CellEditor

  • have to notify or re_Layout JTable, the same on stop and cancelEdit

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Yes, a `CellEditor` will be required; also consider [`TablePopupEditor`](http://stackoverflow.com/a/3591230/230513). – trashgod Oct 04 '12 at 15:55
1

Not an answer to how-to-adjust-rowHeights, but for an alternative mentioned in my comment: "oversize" the editorComponent only instead of updating the complete rowHeight (which I think would be too irritating to users, but up to you to decide, of course :)

// fake a bigger editing component
JTextField oversized = new JTextField() {

    @Override
    public Dimension getPreferredSize() {
        Dimension dim = super.getPreferredSize();
        dim.height *= 5;
        return dim;
    }

};

TableCellEditor editor = new DefaultCellEditor(oversized);
JTable table = new JTable(new AncientSwingTeam()) {

    @Override
    public boolean editCellAt(int row, int column, EventObject e) {
        boolean result = super.editCellAt(row, column, e);
        if (result) {
            // adjust cell bounds of the editing component
            Rectangle bounds = getEditorComponent().getBounds();
            bounds.height = getEditorComponent().getPreferredSize().height;
            getEditorComponent().setBounds(bounds);
        }
        return result;
    }

    @Override
    public void removeEditor() {
        int editingColumn = getEditingColumn();
        super.removeEditor();
        if (editingColumn >= 0) {
            // cleanup
            repaint();
        }
    }


};
table.getColumnModel().getColumn(0).setCellEditor(editor);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
0

Didn't try it, but I would say implementing MouseListener's mouseClicked() method is the way to go. Keep track of whether the cells height was already increased, and change the height accordingly.

Since MouseListener is an interface, CellRenderer could implement this interface too, keeping all cell-behavior in one class.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
  • no, a mouseListener is not what you want: editing could be started by many means (including mouse, keyboard, programmatically ...) – kleopatra Oct 04 '12 at 13:09
  • The question stated "when I edit any cell by clicking with the mouse", so I figured this is the only case where this behavior is desired. Indeed, when *all* forms of editing need this behavior, a `MouseListener` is not the way to go. – mthmulders Oct 05 '12 at 06:40
  • welcome to the reality of fuzzy questions :-) Seriously: incomplete questions are no excuse for incomplete answers ... it's simply not an option in gui development to not consider (at least) mouse _and_ keyboard as user gestures. – kleopatra Oct 06 '12 at 08:35