首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何为JCombobox中的线条设置独特的字体颜色?

如何为JCombobox中的线条设置独特的字体颜色?
EN

Stack Overflow用户
提问于 2009-06-01 15:22:39
回答 3查看 3.2K关注 0票数 1

我想为JCombobox中的行/条目设置字体颜色,每行都是唯一的。因此,基本上当您单击下拉箭头时,您应该会看到几条不同颜色的线条,我想根据它们的属性自己指定颜色。我该怎么做呢?谢谢!

EN

回答 3

Stack Overflow用户

发布于 2009-06-01 17:07:18

您需要创建一个自定义ListCellRenderer,如下所示:

代码语言:javascript
复制
class Renderer extends JLabel implements ListCellRenderer {

并实现此方法:

代码语言:javascript
复制
public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        // Get the selected index. (The index param isn't
        // always valid, so just use the value.)

        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }

        // Display the text
        String text = (String) value;
        setText(text);

        // Get the source

然后,根据您的源,使用this.setForeground(颜色颜色)设置文本的颜色。最后,

代码语言:javascript
复制
return this;

}

票数 2
EN

Stack Overflow用户

发布于 2009-06-01 15:32:38

你可能不得不为你的JComboBox提供一个自定义的渲染器,在这里查看Sun的教程:

http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer

(很抱歉没有链接,因为我是新成员,所以还不能发布链接)

票数 1
EN

Stack Overflow用户

发布于 2013-07-18 20:54:16

您可以使用ListCellRenderer。您需要为此编写自定义类。这是基于索引设置前景的完整代码(以避免重复)。您还可以为此设置自定义选择背景和背景。请参阅代码中的注释。

代码语言:javascript
复制
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class ListCellRendererDemo2 extends JFrame
{
Hashtable<Integer,Color> table;
JComboBox<String> c;

    public ListCellRendererDemo2()
    {
        createAndShowGUI();
    }

    private void createAndShowGUI()
    {
        setTitle("JComboBox Demo");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        table=new Hashtable<Integer,Color>();
        table.put(1,Color.RED);
        table.put(2,Color.BLUE);
        table.put(3,Color.GREEN);
        table.put(4,Color.GRAY);


        c=new JComboBox<String>();
        c.addItem("Item 1");
        c.addItem("Item 2");
        c.addItem("Item 3");
        c.addItem("Item 4");
        c.addItem("Item 5");
        c.addItem("Item 6");
        c.addItem("Item 7");
        c.addItem("Item 8");

        c.setRenderer(new MyListCellRenderer(table));

        add(c);
        setSize(400,400);
        setVisible(true);
    }

    public static void main(String args[])
    {
        SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
                new ListCellRendererDemo2();
            }
        });
    }
}
class MyListCellRenderer extends DefaultListCellRenderer
{
Hashtable<Integer,Color> table;

    public MyListCellRenderer(Hashtable<Integer,Color> table)
    {
        this.table=table;

        // Set opaque for the background to be visible
        setOpaque(true);
    }

    public Component getListCellRendererComponent(JList jc,Object val,int idx,boolean isSelected,boolean cellHasFocus)
    {
        // Set text (mandatory)
        setText(val.toString());

        // Set the foreground according to the selected index
        setForeground(table.get(idx));

            // Set your custom selection background, background
            // Or you can get them as parameters as you got the table
            if(isSelected) setBackground(Color.LIGHT_GRAY);
            else setBackground(Color.WHITE);

    return this;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/935286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档