如何设置JComboBox项的边框

时间:2023-01-27 13:28:24

I want to create a default JComboBox in my application. But i have a small problem in set border to the JComboBox items.

我想在我的应用程序中创建一个默认的JComboBox。但是我在JComboBox项目的设置边框中遇到了一个小问题。

Here the JComboBox

这里是JComboBox

package test.combobox;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JComboBox;

public class MyComboBox extends JComboBox<Object> {
    public MyComboBox() {
        super();

        setBackground(new Color(0xFFFFFF));
        setFont(new Font("Tahoma", 0, 14));
    }
}

I need to set the items border of JComboxBox with:

我需要设置JComboxBox的项边框:

Border itemBorder = BorderFactory.createCompoundBorder(
        new LineBorder(new Color(0xCCCCCC), 1, true), 
        BorderFactory.createEmptyBorder(0, 7, 0, 7));

Test the ComboBox.

测试ComboBox。

package test.combobox;

import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.UIManager;
import net.sf.jasperreports.engine.JRException;

public class TestComboBox {
    public static void main(String[] args) throws JRException {

        UIManager.getCrossPlatformLookAndFeelClassName(); 

        JFrame frame = new JFrame("MyComboBox");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(300, 70));

        MyComboBox cb = new MyComboBox();
        cb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cb.setPreferredSize(new Dimension(300, 30));

        frame.getContentPane().add(cb);
        frame.pack();
        frame.setVisible(true);
    }
}

1 个解决方案

#1


2  

The items in a JComboBox are rendered via the ListCellRenderer interface.

JComboBox中的项目通过ListCellRenderer接口呈现。

Take a look at How to Use Combo Boxes and Providing a Custom Renderer for examples

请查看如何使用组合框和提供自定义渲染器的示例

This is an important concept as JTable, JTree and JList also use this concept...

这是一个重要的概念,因为JTable,JTree和JList也使用这个概念......

#1


2  

The items in a JComboBox are rendered via the ListCellRenderer interface.

JComboBox中的项目通过ListCellRenderer接口呈现。

Take a look at How to Use Combo Boxes and Providing a Custom Renderer for examples

请查看如何使用组合框和提供自定义渲染器的示例

This is an important concept as JTable, JTree and JList also use this concept...

这是一个重要的概念,因为JTable,JTree和JList也使用这个概念......