2014-03-24 58 views
-2

我在我的JTable中使用以下TableCellRenderer。當我點擊JComboBox時,我看到一個值爲一個字符串的列表(如:[Text1, Text2]),但不是單獨的項目。錯誤在哪裏?JComboBox的內容顯示爲單個字符串,而不是單獨的項目

TableColumn columnComboBox = table.getColumnModel().getColumn(5); 
     columnComboBox.setCellRenderer(getRendererComboBox()); 

    private TableCellRenderer getRendererComboBox() { 
     return new TableCellRenderer() { 

      private JComboBox<String> box = new JComboBox<String>(); 

      @Override 
      public Component getTableCellRendererComponent(JTable table, Object value, 
        boolean isSelected, boolean hasFocus, int row, int column) 
      { 
       box.removeAllItems(); 
       for (String q : employees[row].getQualification()) 
        box.addItem(q); 
       box.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); 
       box.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground()); 
       return box; 
      } 
     }; 
    } 
+1

郵報MCTRE更快的幫助 –

+1

看起來像周圍的相同問題的第n個或多或少相同的問題......你似乎_still_不明白渲染器之間的差異編輯。 – kleopatra

回答

0

如果你想在你的表中有一個下拉字段,你可以像這樣做here。 那麼,這將是你創建的JComboBox部分:

public void setUpSportColumn(JTable table, 
           TableColumn sportColumn) { 
     //Set up the editor for the sport cells. 
     JComboBox comboBox = new JComboBox(); 
     comboBox.addItem("Snowboarding"); 
     comboBox.addItem("Rowing"); 
     comboBox.addItem("Knitting"); 
     comboBox.addItem("Speed reading"); 
     comboBox.addItem("Pool"); 
     comboBox.addItem("None of the above"); 
     sportColumn.setCellEditor(new DefaultCellEditor(comboBox)); 

     //Set up tool tips for the sport cells. 
     DefaultTableCellRenderer renderer = 
       new DefaultTableCellRenderer(); 
     renderer.setToolTipText("Click for combo box"); 
     sportColumn.setCellRenderer(renderer); 
    } 

如果這是不是你想要的,但下拉細胞的只是內容,當你將鼠標懸停在該單元格,所有在列中元素很好,你只需要在傳遞給setToolTipText-method的字符串中使用html-tags。 像這樣:

//Set up tool tips for the sport cells. 
     DefaultTableCellRenderer renderer = 
       new DefaultTableCellRenderer(); 
     String newLine = "<br>"; 
     String toolTipText = "<html>Snowboarding"+newLine+"Rowing"+newLine+"Knitting"+newLine+"Speed reading"+newLine+"Pool"+newLine+"None of the above</html>"; 
     renderer.setToolTipText(toolTipText); 
     sportColumn.setCellRenderer(renderer); 

我發現這個問題的答案在這裏:Multi-line tooltips in Java?

0

公共類平板電腦擴展的JFrame { /** * * / 私有靜態最後的serialVersionUID長= 1升;

public tablet() { 

    JTable table = new JTable(); 
    DefaultTableModel model = new DefaultTableModel(new Object[][] { { "item1", "item2" } }, new Object[] { "A", "B" }); 
    table.setModel(model); 
    table.setPreferredScrollableViewportSize(new Dimension(400, 250)); 
    table.setRowHeight(25); 

    JScrollPane scrollPane = new JScrollPane(table); 

    // These are the combobox values 
    String[] values = new String[] { "item1", "item2", "item3" }; 

    // Set the combobox editor on the 1st visible column 

    //combo editor 
    JComboBox box = new JComboBox(values); 
    TableColumn column1 = table.getColumnModel().getColumn(0); 
    column1.setCellEditor(new DefaultCellEditor(box)); 

    //combo renderer 
    column1.setCellRenderer(new MyComboBoxRenderer(values)); 

    getContentPane().add(scrollPane); 
} 

class MyComboBoxRenderer extends JComboBox implements TableCellRenderer { 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 1L; 

    public MyComboBoxRenderer(String[] items) { 
     super(items); 
    } 

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 
     if(hasFocus) 
     { 
      setForeground(table.getSelectionForeground()); 
      super.setBackground(table.getSelectionBackground()); 
     } 
     else 
     { 
      setForeground(table.getForeground()); 
      setBackground(table.getBackground()); 
     } 

     // Select the current value 
     setSelectedItem(value); 
     return this; 
    } 
} 

public static void main(String args[]) { 
    tablet aTable = new tablet(); 
    aTable.pack(); 
    aTable.setVisible(true); 
} 

}

相關問題