本文介紹了將Swing組合框TipTool設(shè)置為選定項(xiàng)目的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我有一個(gè)組合框,其中的項(xiàng)是對(duì)象,其字符串值可能很長(zhǎng)。我不想讓框變長(zhǎng),而是希望全文顯示時(shí)鼠標(biāo)懸停。我如何才能做到這一點(diǎn)?
推薦答案
基本解決方案是提供ListCellRenderer
將返回的Component
toolTipText
屬性設(shè)置為所需的相應(yīng)值
DefaultListCellRenderer
擴(kuò)展自JLabel
,因此您只需使用它的toolTipText
方法即可提供您想要的任何”擴(kuò)展”文本。
public class ToolTipListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// I'd extract the basic "text" representation of the value
// and pass that to the super call, which will apply it to the
// JLabel via the setText method, otherwise it will use the
// objects toString method to generate a representation
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setToolTipText("To what ever you need based on the value that has been passsed");
return this;
}
}
有關(guān)詳細(xì)信息,請(qǐng)參閱How to use comboboxes和Providing a custom renderer
這篇關(guān)于將Swing組合框TipTool設(shè)置為選定項(xiàng)目的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,