÷÷÷÷÷÷=闹完了,向大家请教一个和JList有关的问题=÷÷÷÷÷÷

Iforgot 2002-11-17 04:11:28
就是很简单的,如何不用本来的选择方式,自己写选择方式,具体就是比如我要双击选择,再双击就不选中了。本来是 mousePressed时选中的,我查源码没找到实现的地方,自己不知道如何overwrite。写ListCellRenderer不能截获鼠标,他只管外观。最好有代码,谢谢各位了。
...全文
57 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iforgot 2002-11-25
  • 打赏
  • 举报
回复
谢谢大家的up和回答,很感谢!:)
sikuan 2002-11-24
  • 打赏
  • 举报
回复
见识到高手了,五颗星,哈,跟着沾光学习
netwebs 2002-11-24
  • 打赏
  • 举报
回复
等弄好了一定给俺一份啊~~~俺也给分
zhligang@msn.com
Iforgot 2002-11-24
  • 打赏
  • 举报
回复
我一直没结贴,就是因为你说了你会写的,所以在等。

不过同志们放心,我会结的。
dentance 2002-11-24
  • 打赏
  • 举报
回复
高手在前,不容我放肆,哈哈。
masterz的搞法很好,但迂回曲折(right?)。
我想从javax.swing.plaf.basic.basiclistui入手,但是没实现,masterz能再想想办法吗?
masterz 2002-11-18
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.ListSelectionListener;
import java.util.EventListener;
import javax.swing.event.ListSelectionEvent;

public class NewList extends JList implements ListSelectionListener
{
int sel[];
boolean manualset;
public void setSelectedIndices(int a[])
{
manualset=true;
System.out.println("NewList.setSelectedIndices");
if(a!=null)
super.setSelectedIndices(a);
else
this.clearSelection();
sel = a;
}
public NewList()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent me)
{
if(me.getClickCount()<2)
return;
JList list = (JList) me.getSource();
int index = list.locationToIndex(me.getPoint());
int oldsel[]=list.getSelectedIndices();
int newsel[];
if(list.isSelectedIndex(index))
{
//list.setSelectedIndices(int[] indices)
newsel=new int[oldsel.length-1];
int j=0;
for(int i=0;i<oldsel.length;i++)
{
if(oldsel[i]!=index)
{
newsel[j]=oldsel[i];
j++;
}
}
}
else
{
newsel=new int[oldsel.length+1];
if(oldsel.length>0)
System.arraycopy(oldsel,0,newsel,0,oldsel.length);
newsel[oldsel.length]=index;
}
list.setSelectedIndices(newsel);
Rectangle rect = list.getCellBounds(index, index);
list.repaint(rect);

}
});
addListSelectionListener(this);
}
public static void main(String args[])
{
JFrame frame=new JFrame("new list test");
NewList newlist=new NewList();
String[] listdata={"item1","item2","item3"};

newlist.setListData(listdata);

frame.getContentPane().add(newlist);
frame.setSize(400,400);
frame.setVisible(true);
frame.show();
}
boolean reset=false;
public void valueChanged(ListSelectionEvent e)
{
if(reset==true)
{
return;
}
reset=true;
if(!manualset)
{
if(sel!=null)

super.setSelectedIndices(sel);
else
clearSelection();
}
manualset=false;
reset=false;
//System.out.println("valueChanged");
}
}
mercury1231 2002-11-17
  • 打赏
  • 举报
回复
不错,我也沾点光吧。
mercury1231@sina.com.cn
Luke_cn 2002-11-17
  • 打赏
  • 举报
回复
能给我也发份么?谢谢
deadwalker@vip.sina.com
Iforgot 2002-11-17
  • 打赏
  • 举报
回复
好啊,代码通过给分。当然最好封装好一些,代码也写的规整些。

或者发到我的邮箱zhangjiawang0809@sohu.com
dentance 2002-11-17
  • 打赏
  • 举报
回复
下周帮你解决。我一周上一次网。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧