📄 comboboxrenderer.java
字号:
/*====================================================================*\ComboBoxRenderer.javaCombo box renderer class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program. If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage gui;//----------------------------------------------------------------------// IMPORTSimport java.awt.Component;import java.awt.ComponentOrientation;import java.awt.Dimension;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Rectangle;import javax.swing.JComboBox;import javax.swing.JComponent;import javax.swing.JList;import javax.swing.ListCellRenderer;//----------------------------------------------------------------------// COMBO BOX RENDERER CLASSpublic class ComboBoxRenderer extends JComponent implements ListCellRenderer{////////////////////////////////////////////////////////////////////////// Constants//////////////////////////////////////////////////////////////////////// private static final int TOP_MARGIN = 1; private static final int BOTTOM_MARGIN = TOP_MARGIN; private static final int LEADING_MARGIN = 3; private static final int TRAILING_MARGIN = 5;////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public ComboBoxRenderer( JComboBox owner ) { this.owner = owner; setOpaque( true ); } //------------------------------------------------------------------ public ComboBoxRenderer( JComboBox owner, int maxTextWidth ) { this( owner ); this.maxTextWidth = maxTextWidth; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ListCellRenderer interface//////////////////////////////////////////////////////////////////////// public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { setBackground( isSelected ? list.getSelectionBackground( ) : list.getBackground( ) ); setForeground( isSelected ? list.getSelectionForeground( ) : list.getForeground( ) ); text = (value == null) ? new String( ) : value.toString( ); FontMetrics fontMetrics = getFontMetrics( list.getFont( ) ); textWidth = fontMetrics.stringWidth( text ); if ( (maxTextWidth > 0) && (textWidth > maxTextWidth) ) { int maxWidth = maxTextWidth - fontMetrics.stringWidth( Constants.ELLIPSIS_STR ); char[] chars = text.toCharArray( ); int length = chars.length; while ( (length > 0) && (textWidth > maxWidth) ) textWidth -= fontMetrics.charWidth( chars[--length] ); text = new String( chars, 0, length ) + Constants.ELLIPSIS_STR; } textHeight = fontMetrics.getHeight( ); return this; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : overriding methods//////////////////////////////////////////////////////////////////////// public Dimension getPreferredSize( ) { int width = (maxTextWidth > 0) ? maxTextWidth : textWidth; return new Dimension( LEADING_MARGIN + width + TRAILING_MARGIN, TOP_MARGIN + textHeight + BOTTOM_MARGIN ); } //------------------------------------------------------------------ protected void paintComponent( Graphics gr ) { // Fill background Rectangle rect = gr.getClipBounds( ); gr.setColor( getBackground( ) ); gr.fillRect( rect.x, rect.y, rect.width, rect.height ); // Draw text FontMetrics fontMetrics = gr.getFontMetrics( ); int x = owner.getComponentOrientation( ).isLeftToRight( ) ? LEADING_MARGIN : getWidth( ) - LEADING_MARGIN - fontMetrics.stringWidth( text ); gr.setColor( getForeground( ) ); gr.drawString( text, x, TOP_MARGIN + fontMetrics.getAscent( ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private JComboBox owner; private int maxTextWidth; private int textWidth; private int textHeight; private String text;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -