无法删除jcombobox的边框

时间:2013-04-14 17:03:33

标签: java swing jcombobox cellrenderer

我试图删除一个不可编辑的组合框的边界一段时间,我做了一些研究,我能够删除边框,设置一个标题边框并删除箭头图标,以及其他一些东西。但我无法弄清楚为什么组合框在所选项目之前和之后仍然有这些'线'。

正如您在此图片中看到的那样(http://d.pr/i/TdDS)。有人知道如何删除它?

showField= new JComboBox();
showField.setBounds(186,504,150,35);
showField.setRenderer(new ItemRenderer());
showField.setEditor(new ItemEditor());
showField.setModel(new DefaultComboBoxModel(new String{"How I Met Your Mother","Greys Anatomy","The Big Bang Theory","Leverage"}));
contentPane.add(serieField);

...

public class ItemEditor extends BasicComboBoxEditor {

private JPanel panel = new JPanel();
private JTextField item = new JTextField();
private String selectedValue;

public ItemEditor(){
    panel.setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.weightx = 1.0;
    constraints.insets = new Insets(2,5,2,2);

    item.setOpaque(false);
    item.setHorizontalAlignment(JTextField.LEFT);
    item.setForeground(Color.WHITE);
    item.setFont(new Font("Calibri", 0, 11));
    item.setBorder(null);

    panel.add(item, constraints);
    panel.setBackground(new Color(44,44,44));
    panel.setBorder(null);
}

public Component getEditorComponent() {
    return this.panel;
}

public Object getItem() {
    return this.selectedValue;
}

public void setItem(Object obj) {
    if(obj == null)
        return;
    String showItem = (String) obj;
    selectedValue = showItem;
    item.setText(selectedValue);        
}
}

...

public class ItemRenderer extends JPanel implements ListCellRenderer {

private JTextField item = new JTextField();

public ItemRenderer() {
    setLayout(new GridBagLayout());
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.weightx = 1.0;
    constraints.insets = new Insets(2,2,2,2);

    item.setOpaque(true);
    item.setHorizontalAlignment(JTextField.LEFT);
    item.setFont(new Font("Calibri", 0, 11));
    item.setBorder(null);

    add(item, constraints);
}

public void paint(Graphics g) {
    this.setBorder(null);
    super.paint(g);
}

public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
    String showItem = (String) value;

    item.setText(showItem);
    item.setForeground(Color.WHITE);

    if (isSelected)
        item.setBackground(new Color(119, 119, 119));
    else
        item.setBackground(new Color(44, 44, 44));

    return this;
}
}

0 个答案:

没有答案