java Swing中的JList中,点击一项在前面自动打勾,需要怎么做求个实例或者思想也成,跪求高手

 我来答
daay1986
2012-02-10 · TA获得超过6019个赞
知道大有可为答主
回答量:2208
采纳率:0%
帮助的人:1502万
展开全部
public class CheckList extends JFrame {

public CheckList() {
super("AKCheckList");
String[] listData = {"Apple", "Orange", "Cherry", "Blue Berry", "Banana", "Red Plum", "Watermelon"};
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.out.println("Unable to find System Look and Feel");
}
final JList listCheckBox = new JList(buildCheckBoxItems(listData.length));
final JList listDescription = new JList(listData);
listDescription.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listDescription.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent me) {
if (me.getClickCount() != 2) {
return;
}
int selectedIndex = listDescription.locationToIndex(me.getPoint());
if (selectedIndex < 0) {
return;
}
CheckBoxItem item = (CheckBoxItem) listCheckBox.getModel().getElementAt(selectedIndex);
item.setChecked(!item.isChecked());
listCheckBox.repaint();

}
});
listCheckBox.setCellRenderer(new CheckBoxRenderer());
listCheckBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listCheckBox.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent me) {
int selectedIndex = listCheckBox.locationToIndex(me.getPoint());
if (selectedIndex < 0) {
return;
}
CheckBoxItem item = (CheckBoxItem) listCheckBox.getModel().getElementAt(selectedIndex);
item.setChecked(!item.isChecked());
listDescription.setSelectedIndex(selectedIndex);
listCheckBox.repaint();
}
});
JScrollPane scrollPane = new JScrollPane();
scrollPane.setRowHeaderView(listCheckBox);
scrollPane.setViewportView(listDescription);
listDescription.setFixedCellHeight(20);
listCheckBox.setFixedCellHeight(listDescription.getFixedCellHeight());
listCheckBox.setFixedCellWidth(20);
getContentPane().add(scrollPane); //, BorderLayout.CENTER);
setSize(350, 200);
setVisible(true);
}

private CheckBoxItem[] buildCheckBoxItems(int totalItems) {
CheckBoxItem[] checkboxItems = new CheckBoxItem[totalItems];
for (int counter = 0; counter < totalItems; counter++) {
checkboxItems[counter] = new CheckBoxItem();
}
return checkboxItems;
}

public static void main(String args[]) {
CheckList checkList = new CheckList();
checkList.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}

class CheckBoxItem {

private boolean isChecked;

public CheckBoxItem() {
isChecked = false;
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean value) {
isChecked = value;
}
}

/* Inner class that renders JCheckBox to JList*/
class CheckBoxRenderer extends JCheckBox implements ListCellRenderer {

public CheckBoxRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
}

public Component getListCellRendererComponent(JList listBox, Object obj, int currentindex,
boolean isChecked, boolean hasFocus) {
setSelected(((CheckBoxItem) obj).isChecked());
return this;
}
}
}

另外可以使用单列的表格,隐藏划线来做,这个简单一点。
shenaodong
2012-02-10 · TA获得超过960个赞
知道小有建树答主
回答量:868
采纳率:0%
帮助的人:812万
展开全部
印象中应该可以设置JList的模型为JCombox就可以实现的,具体的可以看看JFC自带的示例。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式