I want to create a default JComboBox in my application. But i have a small problem in set border to the JComboBox items.
Here the 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:
Border itemBorder = BorderFactory.createCompoundBorder(
new LineBorder(new Color(0xCCCCCC), 1, true),
BorderFactory.createEmptyBorder(0, 7, 0, 7));
Test the 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);
}
}
The items in a JComboBox
are rendered via the ListCellRenderer
interface.
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...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.