大虾米们,帮个忙,我现在把一些组件放到了JList内部,能正常显示出来了,可状态我改变不了怎么办啊??分不多,谁正确回答了分全给谁!为了在其中应用JCheckBox,我重写了JList的单元渲染器,代码如下:public class JListRender extends JCheckBox implements ListCellRenderer,MouseListener{
      
      //这个私有变量当前没用
      private JList jlist;
      public void setJlist(JList jlist){
         this.jlist = jlist;
      }
      public JList getJlist(){
        return this.jlist;
      }
      //实现ListCellRenderer的单元渲染器方法
      public Component getListCellRendererComponent(
        JList list,
        Object value,            // value to display
        int index,               // cell index
        boolean isSelected,      // is the cell selected
        boolean cellHasFocus)    // the list and the cell have the focus
      {
        JCheckBox s = (JCheckBox)value;
        setText(s.getText());
             this.setJlist(list);
            if(isSelected){
               list.addMouseListener(this);
            }
            if (isSelected) {
             //list.addMouseListener(this);
              //这是最初我想改变JCheckBox状态的方法,但是单击一次却进行了N次操作
              //实现了事件接口也一样
              if(this.isSelected()){
                 this.setSelected=false;
             }else{
                 this.setSelected=true;
             }
              setBackground(list.getSelectionBackground());
              setForeground(list.getSelectionForeground());
            }
          else {
                //this.setSelected(true);
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            setEnabled(list.isEnabled());
            setFont(list.getFont());
            setOpaque(true);
            list.getSelectedValue();
            return this;
      }
  //为了方便事件的处理后来实现了事件接口
  public void mouseClicked(MouseEvent e) {    if(e.getClickCount()==1){
        int index = getJlist().locationToIndex(e.getPoint());
        //JCheckBox checkBox = (JCheckBox)getJlist().getSelectedValue();
        //if(getJlist().locationToIndex(e.getPoint()) == getJlist().getAnchorSelectionIndex()){       // }
        System.out.println("监听事件中取得的当前项目编号:"+index);
        if(this.isSelected()){           this.setSelected(false);
        }else{
           this.setSelected(true);
        }
    }
  }  public void mousePressed(MouseEvent e) {
  }  public void mouseReleased(MouseEvent e) {
  }  public void mouseEntered(MouseEvent e) {
  }  public void mouseExited(MouseEvent e) {
  }
}

解决方案 »

  1.   

    据你的意思,需要既显示成combox,又可以选择编辑.那光有listcellrenderer恐怕不够,还要有listcelleditor,因为renderer只负责显示,editor负责返回编辑值.我用过tablecell是这样的,listcell应该类似:创建table时这么用: 你查一下,在list添加数据时也该有类似接受renderer和editor的构造.
    this.initColumn("Date", new DateTableCellRenderer(), new DateTableCellEditor());定义renderer和editor:
    class DateTableCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer { public Component getTableCellRendererComponent(JTable table,
    Object value, boolean isSelected, boolean hasFocus, int row,
    int column) {
    //返回单元格显示的组件
    }
    } class DateTableCellEditor extends AbstractCellEditor implements
    TableCellEditor {

    public Component getTableCellEditorComponent(JTable table,
    Object value, boolean isSelected, int row, int column) {

    //返回编辑组件
    } public boolean shouldSelectCell(EventObject anEvent) {
    return true;
    } public Object getCellEditorValue() {
    //返回编辑组件对应的value,通常是string,date之类的
    }
    }
      

  2.   

    我刚查了下,list似乎没有listcelleditor之类的东西,因为list不应该被编辑的,只能有不同的显示....要编辑的话只该是增加删除,所以你得换格思路了.
      

  3.   

    呵呵,做只有一列的表格来,把表格线去掉,不就是一个可以编辑的JList了嘛,是吧
      

  4.   

    public void mouseClicked(MouseEvent e) {  }
    当中repaint()一下jlist就行了