Swing/LeftClippedComboBox のバックアップソース(No.2)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/LeftClippedComboBox へ行く。
- 1 (2007-06-19 (火) 17:35:29)
- 2 (2008-03-31 (月) 19:37:32)
- 3 (2008-04-01 (火) 22:22:35)
- 4 (2008-04-02 (水) 20:12:16)
- 5 (2008-09-30 (火) 15:36:45)
- 6 (2008-11-13 (木) 14:25:12)
- 7 (2009-11-09 (月) 20:21:54)
- 8 (2011-06-01 (水) 03:10:51)
- 9 (2013-02-05 (火) 17:27:26)
- 10 (2013-08-31 (土) 01:41:10)
- 11 (2014-11-25 (火) 03:03:31)
- 12 (2014-11-30 (日) 00:46:08)
- 13 (2015-02-22 (日) 21:02:43)
- 14 (2015-10-22 (木) 00:39:38)
- 15 (2016-05-27 (金) 15:33:39)
- 16 (2017-08-24 (木) 13:07:24)
- 17 (2017-11-02 (木) 15:28:23)
- 18 (2017-11-02 (木) 15:32:16)
- 19 (2018-02-24 (土) 19:51:30)
- 20 (2019-05-30 (木) 18:33:14)
- 21 (2021-02-18 (木) 06:53:27)
TITLE:JComboBoxのアイテム文字列を左側からクリップ #navi(../) *JComboBoxのアイテム文字列を左側からクリップ [#ia7b0026] Posted by [[terai]] at 2007-06-18 #contents **概要 [#x435c009] JComboBoxのアイテム文字列がコンポーネントより長い場合、これを左側からクリップします。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#x51d3784] #code{{ combo02.setRenderer(new DefaultListCellRenderer() { public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent(list,value,index,isSelected,cellHasFocus); //setHorizontalAlignment(JLabel.RIGHT); int availableWidth = combo02.getWidth(); Insets insets; if(index<0) { insets = combo02.getInsets(); int buttonSize = combo02.getHeight() - (insets.top + insets.bottom); //int buttonSize = getArrowButton(combo02).getWidth(); availableWidth -= (insets.left + insets.right + buttonSize); } JTextField field = (JTextField) combo02.getEditor().getEditorComponent(); insets = field.getMargin(); availableWidth -= (insets.left + insets.right); if(getBorder()!=null) { //insets = getBorder().getBorderInsets(this); insets = getInsets(); availableWidth -= (insets.left + insets.right); } String cellText = (value!=null)?value.toString():""; FontMetrics fm = getFontMetrics(getFont()); if(fm.stringWidth(cellText) > availableWidth) { String dots = "..."; int textWidth = fm.stringWidth(dots); int nChars = cellText.length() - 1; //for(; nChars > 0; nChars--) { while(nChars > 0) { textWidth += fm.charWidth(cellText.charAt(nChars)); if(textWidth > availableWidth) { break; } nChars--; } setText(dots+cellText.substring(nChars+1)); } return this; } }); }} **解説 [#qf9b52f8] 標準のJComboBoxでは、長い文字列は右側をクリップするので、上記のサンプルでは左側を切り取り、"..."で置き換えるようにセルレンダラーを変更しています。 ポップアップリストで描画されている場合は、矢印ボタンの幅は無視しています。 Windows環境の1.5と1.6で色々サイズが微妙に異なるようで、うまく表示されない場合があります。 **参考リンク [#u56983b7] -[[JTable - right align in cell even if the text is wider than the cell>http://forum.java.sun.com/thread.jspa?forumID=57&threadID=634798]] **コメント [#h4fa25ab] #comment