1

編集中に行の高さを調整するように、カスタムテーブルセルエディターを作成しています。私はこのコードを持っていますが、セルのサイズを変更する代わりに、パネル全体またはフレームのサイズを変更するために継ぎ目があります。セルに文字を入力しようとすると、メインフレームの幅が数ピクセルに狭まります。誰かが問題を見ることができますか?

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

    }
}
4

2 に答える 2

1

見てきた

  • doLayout に関する私の回答 (CellEditor から起動される可能性があります)

  • または(TextUtilsを使用するより快適な方法)@kleopatraによるgetPreferredSizeに関するコメント

  • これはユーザーを(非常に)混乱させる可能性があり、

  • JScrollPane が恋しいので、MaxSize をオーバーライドする必要があります。最大サイズは JScrollPane の高さと重さです。

  • それをしないでください、JTextComponentsでJScrollPaneをそこに置き、CellEditorのPreferredSizeをオーバーライドしてください。


すべてが間違っている、私の見解では、

  • アプリケーションのモーダル ポップアップ ウィンドウを作成します (JWindow は JTextComponent への入力を許可していないため、JDialog のみに基づいています)。JTextComponent を使用して、そこに ESC キーの KeyBindings を実装します。JDialog の失われた Fucus と同じです。その後、問題なく装飾を解除できます。

そこに置くSave JButton

  • 保存ボタンからの出力を選択したセルに再編集すると、JTable 内のアプリケーション モーダルからフォーカスを失うことはありません

  • コンテンツは、JTable のすべてのセルに対して 1 つの JDialog でフォーマット、フィルター処理、変更する必要があります

于 2012-10-15T12:08:34.930 に答える
1

編集中に行のサイズを変更する代わりにTablePopupEditor、 を使用する を検討してJTextAreaください。

于 2012-10-15T16:04:36.577 に答える