2017-02-09 62 views
0

我最近开始学习ListCellRendererJComboBox,最后得到了基本的想法。但是,我无法将组合框的初始状态或初始选定项目设置为null(或选定的索引为-1)。我想将它设置为-1,这样当表单加载时,没有任何选择,直到用户点击下拉选择一个项目。将JComboBox的初始选择索引设置为-1或者什么都不是

我尝试使用comboBox.setSelectedIndex(-1)comboBox.setSelectedItem(null)

 GradeLevelDaoImpl gldi = new GradeLevelDaoImpl(); 
     DefaultComboBoxModel gradeLevelModel = new DefaultComboBoxModel(gldi.getAllActiveGradeLevels().toArray()); 
     jcmbGradeLevel.setModel(gradeLevelModel); 
     jcmbGradeLevel.setRenderer(new JComboBoxRenderer()); 
     jcmbGradeLevel.setSelectedItem(null); //doesn't work 
     jcmbGradeLevel.setSelectedIndex(-1); //doesn't work 

像现在这样。

这就是我发布表单时一直得到的结果。

enter image description here

GradeLevel组合框仍处于选中状态。指数为0;

这是我的渲染器。

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     //Class value conversion to getString value using getter 

     if (value instanceof SchoolYear) { 
      this.setText("" + ((SchoolYear) value).getStart()); 
     } 
     if (value instanceof GradeLevel) { 

      this.setText("" + ((GradeLevel) value).getGradelevel()); 
     } 
     if (value instanceof PaymentTerm) { 
      this.setText("" + ((PaymentTerm) value).getPaymentTerm()); 
     } 
     if (value instanceof FeeCategory) { 
      this.setText("" + ((FeeCategory) value).getFeeCategory()); 
     } 

     //selection formatting 
     if (isSelected) { 
      this.setBackground(Color.YELLOW); 
      //this.setBackground(list.getSelectionBackground()); 
      this.setForeground(list.getSelectionForeground()); 
     } else { 
      this.setBackground(list.getBackground()); 
      this.setForeground(list.getForeground()); 

     } 

     if ((isSelected) && (cellHasFocus)) { 
      this.setBorder(new LineBorder(Color.black)); 
     } else { 
      this.setBorder(null); 
     } 
     return this; 
    } 

我什至index参数设置为-1。 index = -1;没有成功。 试过list.setSelectedIndex(-1),依然不行。

任何建议或解决方案?

+1

1)'if(isSelected){this.setBackground(Color.YELLOW);'这里看起来不是黄色。当使用下拉菜单选择一个项目时它显示为黄色吗? 2)为什么不设置第一个值是'请选择'(或者给用户的任何方式)? 3)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 –

+0

当'value'不是'ShoolYear','GradeLevel','PaymentTerm','FeeCategory'的实例时会发生什么? – MadProgrammer

+0

@MadProgrammer我得到了gradelevel的字符串表示,它是12. – p3ace

回答

1

您没有为渲染器设置“默认”值(或者至少您不检查value是否为)。

记住,这是用在组件中的所有元件共用,所以必须将所有可能的不同对象之间改变的属性值

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    //Class value conversion to getString value using getter 

    if (value instanceof SchoolYear) { 
     this.setText("" + ((SchoolYear) value).getStart()); 
    } else if (value instanceof GradeLevel) { 
     this.setText("" + ((GradeLevel) value).getGradelevel()); 
    } else if (value instanceof PaymentTerm) { 
     this.setText("" + ((PaymentTerm) value).getPaymentTerm()); 
    } else if (value instanceof FeeCategory) { 
     this.setText("" + ((FeeCategory) value).getFeeCategory()); 
    } else { 
     this.setText("---"); 
    } 

    //selection formatting 
    if (isSelected) { 
     this.setBackground(Color.YELLOW); 
     //this.setBackground(list.getSelectionBackground()); 
     this.setForeground(list.getSelectionForeground()); 
    } else { 
     this.setBackground(list.getBackground()); 
     this.setForeground(list.getForeground()); 

    } 

    if ((isSelected) && (cellHasFocus)) { 
     this.setBorder(new LineBorder(Color.black)); 
    } else { 
     this.setBorder(null); 
    } 
    return this; 
} 

这将显示---当该值不是一个您准备渲染的值为

相关问题