2013-02-02 71 views
3

我要去在創建應用 輸入發票我已經創建形式,其中我已經使用的JTable 但最終用戶的要求是,該描述可以是2〜3行 (表中的圖像) [我![]如何設置JTable的行大小?

,所以我想要的代碼,以增加行

+0

參見[此示例](http://stackoverflow.com/a/6175860/418556)。順便一提。 ITYM'數字'的行數爲2或3,而不是每行的'大小'。此外,圖像丟失。爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

4

終端用戶的要求是,該描述可以是 2〜3行

我假設你正在試圖在一個JTable細胞顯示多行的數據。我試圖在單元格內的多行顯示數據的例子。

由於@Sergii建議使用JTable.setRowHeight來增加單元格的高度。 data column單元使用JTextArea呈現。

JTable with a cell rendered as JTextArea

import java.awt.Component; 
import java.awt.EventQueue; 
import java.util.EventObject; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextArea; 
import javax.swing.event.CellEditorListener; 
import javax.swing.table.DefaultTableCellRenderer; 
import javax.swing.table.TableCellEditor; 


public class JTableCellTest { 
    public static void main(String[] args) { 
     Runnable r = new Runnable() { 

      @Override 
      public void run() { 
       Object[] columnNames = {"S.No", "Data"}; 
       Object[][] data = { 
             {"1", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"}, 
             {"2", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"}, 
             {"3", "I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines.\n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines. \n I'm going to create application in that to enter invoice i've created form in which i have used JTable but end user requirement is that the the description may be of 2 to 3 lines"} 
            }; 

       JFrame frame = new JFrame(); 
       JTable table = new JTable(data, columnNames); 
       table.setRowHeight(70); 

       table.getColumnModel().getColumn(1).setCellRenderer(new CustomCellRenderer()); 
       table.getColumnModel().getColumn(1).setCellEditor(new CustomEditor()); 

       frame.setTitle("JTable with JTextArea"); 
       frame.add(new JScrollPane(table)); 
       frame.pack(); 
       frame.setVisible(true); 

      } 
     }; 

     EventQueue.invokeLater(r); 
    } 
} 

class CustomCellRenderer extends DefaultTableCellRenderer { 

     private JTextArea textArea; 
     private JScrollPane scrollPane; 

     public CustomCellRenderer() { 
      textArea = new JTextArea(); 
      scrollPane = new JScrollPane(textArea); 
     } 

     @Override 
     public Component getTableCellRendererComponent(JTable table, Object value, 
       boolean isSelected, boolean hasFocus, int row, int column) { 

      if(null != value) 
       textArea.setText(value.toString()); 

      return scrollPane; 
     } 
} 

class CustomEditor implements TableCellEditor { 

    private JTextArea textArea; 
    private JScrollPane scrollPane; 

    public CustomEditor() { 
     textArea = new JTextArea(); 
     scrollPane = new JScrollPane(textArea); 
    } 

    @Override 
    public Component getTableCellEditorComponent(JTable table, Object value, 
      boolean isSelected, int row, int column) { 
     if(null != value) 
      textArea.setText(value.toString()); 

     return scrollPane; 
    } 

    @Override 
    public void addCellEditorListener(CellEditorListener arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void cancelCellEditing() { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public Object getCellEditorValue() { 
     // TODO Auto-generated method stub 
     return textArea.getText(); 
    } 
    @Override 
    public boolean isCellEditable(EventObject arg0) { 
     // TODO Auto-generated method stub 
     return true; 
    } 
    @Override 
    public void removeCellEditorListener(CellEditorListener arg0) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public boolean shouldSelectCell(EventObject arg0) { 
     // TODO Auto-generated method stub 
     return true; 
    } 
    @Override 
    public boolean stopCellEditing() { 
     // TODO Auto-generated method stub 
     return true; 
    } 
} 
+0

thankx先生,我想通過這種方法,如果客戶端輸入文本,然後在特定的單詞或單詞的長度後自動輸入到下一行 –

6
  • JTable->TableColumnModel->TableColum(列的變化大小, 只是調查的編輯器和渲染)
  • JTable-setRowHeight(變化高度的尺寸行)