2012-10-15 156 views
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); 
     } 
    } 

    } 
} 
+1

爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

+0

只有DocumentListener可以強制Document,PlainDocument返回座標,KeyListener是非生產性的,簡單無用,如果你想過濾un_:想要的字符放在那裏DocumentFilter代替DocumentListener並覆蓋通知到文檔:-) – mKorbel

+0

hmm ..不能在容器層次結構中重現縮小範圍(使用帶有JTextArea的編輯器而不是JTextPane,而不是懶惰:匹配自定義渲染器)。但即便如此,這種行爲還遠沒有達到最佳狀態,因爲在編輯時編輯器的大小發生了不可預測的變化。實際上,我懷疑動態改變行高的同時打字可以在不進行大量調整的情況下實現。 – kleopatra

回答

1

have look at

  • 我約的doLayout答案(可從CellEditor中被解僱)

  • 或@kleopatra約的getPreferredSize

  • 評論(而不是使用文本實用程序舒適的方式更多)
  • 這可能會(非常)混淆用戶,

  • 是因爲我思念JScrollPane中,有覆蓋最大範圍,最大尺寸爲身高體重& JScrollPane的,否則CellEditor中的一部分可以去外面screeens界.........,

  • 不要」做T的是,放在那裏JScrollPane的與JTextComponents,覆蓋PREFERREDSIZE cellEditor的,


一切都是錯誤的,我認爲,

  • 使用JTextComponent創建應用程序模式彈出窗口(僅基於JDialog,因爲JWindow不會將輸入傳遞給JTextComponent),實現了ESC鍵的KeyBindings,對於JDialog丟失Fucus也是一樣的,然後可以不會發生任何問題

放在那裏Save JButton從保存按鈕reditect

  • 輸出到選定的單元格,你不能失去焦點從應用程序模式內的JTable

  • 內容抄ULD被格式化,過濾,經修飾的一個的JDialog所有細胞的JTable

+0

您正在重寫渲染器。我需要重寫編輯器,所以當我編輯單元格時,我可以通過按Enter鍵或僅粘貼長文本來增加行大小。我需要在編輯過程中包裝單元格文本。 – Igor

+0

那裏我不能看到任何差異,肯定我的看法...., – mKorbel

+1

我真的不明白你以前的評論意味着什麼。你看不到Renderer和Editor之間的區別? – Igor