Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表格单元格编辑器问题_Java_Swing_Jtable_Keylistener_Tablecelleditor - Fatal编程技术网

Java 表格单元格编辑器问题

Java 表格单元格编辑器问题,java,swing,jtable,keylistener,tablecelleditor,Java,Swing,Jtable,Keylistener,Tablecelleditor,我正在构建一个自定义表格单元编辑器,以便在编辑期间调整行高度。我有这段代码,但它不是调整单元格的大小,而是调整整个面板或框架的大小。当我尝试在单元格中输入字符时,主帧宽度会缩小到几个像素。 有人看到问题了吗 class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor { MyTextpane component = new MyTextpane(); MyTable table;

我正在构建一个自定义表格单元编辑器,以便在编辑期间调整行高度。我有这段代码,但它不是调整单元格的大小,而是调整整个面板或框架的大小。当我尝试在单元格中输入字符时,主帧宽度会缩小到几个像素。 有人看到问题了吗

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

    }
}
类MyTableCellEditor扩展AbstractCellEditor实现TableCellEditor{ MyTextpane组件=新建MyTextpane(); MyTable表; 私人int row; 私人国际学院; 公共组件getTableCellEditorComponent(JTable表、对象值、布尔值、, int行索引,int vColIndex){ ((MyTextpane)组件).setText((字符串)值); addKeyListener(新的KeyListener1()); this.table=(MyTable)表; this.row=行索引; this.col=vColIndex; 返回组件; } 公共对象getCellEditorValue(){ return((MyTextpane)组件).getText(); } 公共类KeyListener1实现KeyListener{ @凌驾 public void keyTyped(KeyEvent ke){ } @凌驾 按下公共无效键(KeyEvent ke){ } @凌驾 公开无效密钥已释放(密钥事件){ 调整行高(表、行、列); } private java.util.List rowColHeight=new ArrayList(); private void adjustRowHeight(JTable表格、int行、int列){ //使其正常工作的技巧是将列的宽度设置为 //原因是getPreferredSize()不带宽度 //将所有文本放在一行中。通过设置列的大小, //getPreferredSize()返回行在中应有的正确高度 //为了给课文腾出空间。 int cWidth=table.getTableHeader().getColumnModel().getColumn(column).getWidth(); 设置尺寸(新尺寸(cWidth,1000)); int prefH=getPreferredSize().高度; while(rowColHeight.size()

  • 我对doLayout的回答(可能被CellEditor解雇)

  • 或者@kleopatra关于getPreferredSize的评论(使用TextUtils的方式非常方便)

  • 这可能(非常)让用户困惑

  • 因为我错过了JScrollPane,所以必须覆盖MaxSize,对于JScrollPane,max size是高度和重量,否则CellEditor的一部分可能会超出Screens界限

  • 不要这样做,将JScrollPane与JTextComponents放在一起,覆盖CellEditor的PreferredSize


在我看来,一切都错了

  • 使用JTextComponent创建应用程序模式弹出窗口(仅基于JDialog,因为JWindow不允许输入JTextComponent),为ESC键实现这些键绑定,对于JDialog的丢失Fucus也是如此,然后可以无任何问题地取消装饰
放在那里
保存JButton

  • 从“保存”按钮重新检测到所选单元格的输出,您不能从JTable内的应用程序模式中失去焦点

  • 对于JTable中的所有单元格,内容都应该格式化、过滤、修改一个JDialog


作为编辑时调整行大小的一种选择,考虑使用<代码> JTextArea < /代码> ./P>以更快地提供更好的帮助,POST。Debug监听器可以强制文档,PLAUDICONPART返回坐标,KeyListener是对比性的,简单的无用的,在你想要对UNY进行过滤的情况下:想要的字符放在那里。entFilter代替DocumentListener并覆盖对文档的通知:-)hmm..无法在容器层次结构中复制任何缩小(使用带有JTextArea而不是JTextPane的编辑器,出于懒散:匹配自定义渲染器)。但即使如此,这种行为也远远不是最佳的,因为编辑器的大小在键入时会发生不可预测的变化。实际上,我怀疑在键入时动态更改行高是否可以在不进行大量调整的情况下实现。adjustRowHeight()中一定有错误。我不明白为什么会发生这种情况。adjustRowHeight()中一定有错误我没有看,但现在你提到了…你在那里做什么?只需在列之间循环并使用Math.max来决定是否需要增加高度。你正在覆盖渲染器。我需要覆盖编辑器,以便在编辑单元格时,我可以通过按Enter键或粘贴长文本来增加行大小。我需要编辑过程中要包装的单元格文本。我真的不明白你以前的评论是什么意思。你看不出渲染器和编辑器之间有什么区别吗?是的,顺便说一句,在那里添加DocumentFilter/Listener而不是KeyListener,并覆盖内置键绑定的新行,这是有区别的,它位于adjustRowHeight()的位置函数。在渲染器中,我在GetTableCellRenderRecomponent()中调用此方法我不能在编辑器中用相同的方法调用它,因为它不会在你键入时调整高度。aaach MyTextpane理论讨论,没有人能看到,你能演示一下,通过弹出窗口、工具提示或JDialog,有两种方法包()用于所有重量级容器或JTextComponents的getView(返回插图)吗