005
22.04.2006, 11:39 Uhr
KaraHead
|
Ich hatte mal sowas ähnliches programmiert. Bei mir ging es um eine JList die links JCheckboxes und rechts einen Text in Form von JLabels haben sollte.
Ich habe es so gelöst, dass ich für die JCheckboxes eine JList und für die JLabels eine JList genommen habe. Und die beiden Listen habe ich in einer JComponent gekapselt. Auch die Items, die das Model zurückgibt, habe ich in einer Klasse gekapselt, die mir dann bei Bedarf Icon und Text zurückgeben.
Aber guck es dir am besten mal an, vielleicht hilft es dir.
C++: |
import java.awt.BorderLayout; import java.awt.Component; import java.awt.ItemSelectable; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.LinkedList; import java.util.List;
import javax.swing.AbstractListModel; import javax.swing.ImageIcon; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.ListModel; import javax.swing.ListSelectionModel; import javax.swing.UIManager; import javax.swing.event.ListSelectionListener;
/** * * @author karahead * */ public class CheckList extends JComponent { protected class CheckListItem implements ItemSelectable { private boolean checked; private int index; private List<ItemListener> listeners = null; private String text = null;
public CheckListItem(boolean isChecked, String text, int index) { this.checked = isChecked; this.text = text; this.index = index;
this.listeners = new LinkedList<ItemListener>(); }
public void addItemListener(ItemListener listener) { if (listener != null) this.listeners.add(listener); }
protected void fireItemStateChanged() { for (ItemListener item : this.listeners) { item.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, this, this.isChecked() ? ItemEvent.SELECTED : ItemEvent.DESELECTED)); } }
public int getIndex() { return this.index; }
public Object[] getSelectedObjects() { return null; }
public String getText() { return this.text; }
public boolean isChecked() { return this.checked; }
public void removeItemListener(ItemListener l) { if (l != null) this.listeners.remove(l); }
public void setChecked(boolean isChecked) { this.checked = isChecked; this.fireItemStateChanged(); } } // -----------------------------------------------------------------
@SuppressWarnings("serial") protected class CheckListModel extends AbstractListModel implements ItemListener { private CheckListItem[] CLI = null;
public CheckListModel(CheckListItem[] items) { this.CLI = items; for (int i = 0; i < items.length; ++i) { this.CLI[i].addItemListener(this); } }
public Object getElementAt(int index) { return this.CLI[index]; }
public int getSize() { return this.CLI.length; }
public void itemStateChanged(ItemEvent e) { if (e.getSource() instanceof CheckListItem) { CheckListItem cli = (CheckListItem) e.getSource(); String text = cli.getText(); ListModel model = CheckList.this.textList.getModel(); for (int i = 0; i < textList.getModel().getSize(); i++) { if (model.getElementAt(i).toString().equals(text)) CheckList.this.textList .setSelectedIndex(cli.getIndex()); } } } } // -----------------------------------------------------------------
@SuppressWarnings("serial") protected class CheckListRenderer extends JCheckBox implements ListCellRenderer {
public CheckListRenderer() { setBackground(UIManager.getColor("List.textBackground")); setForeground(UIManager.getColor("List.textForeground")); }
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { CheckListItem check = (CheckListItem) value; setSelected(check.isChecked()); setSelectedIcon(new ImageIcon("res/img/IFLayers/lyrvisible.png")); setIcon(new ImageIcon("res/img/IFLayers/lyrinvisible.png")); return this; } }
private static final long serialVersionUID = -5809894969437555295L;
// --------- nested classes -----------------------
private JList checkList = null; private JList textList = null; private CheckListItem[] data = null;
@SuppressWarnings("serial") public CheckList(final String[] items) { super(); setupData(items, true);
// CheckList checkList = new JList(); checkList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); checkList.setCellRenderer(new CheckListRenderer()); checkList.setModel(new CheckListModel(data)); checkList.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { int index = checkList.locationToIndex(e.getPoint()); if (index != -1) { CheckListItem check = (CheckListItem) checkList.getModel() .getElementAt(index);
check.setChecked(!check.isChecked()); repaint(); } } });
// TextList textList = new JList(items); textList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); textList.setFixedCellHeight(checkList.getCellBounds(0,0).height);
// adding setLayout(new BorderLayout()); add(checkList, BorderLayout.WEST); add(textList, BorderLayout.CENTER); }
protected JList getCheckList() { return this.checkList; }
public CheckListItem getElementAt(int i) { return (CheckListItem) this.checkList.getModel().getElementAt(i); }
public int getSelectedIndex() { return this.textList.getSelectedIndex(); }
protected JList getTextList() { return this.textList; } /** * bereitet übergebene Daten vor */ protected void setupData(final String[] items, boolean initially_checked) { data = new CheckListItem[items.length]; for(int i=0; i<items.length; ++i) { data[i] = new CheckListItem(initially_checked, items[i], i); } } // -------- public int getListSize() { return data.length; } public void setListData(final String[] items, final boolean[] checked) { data = new CheckListItem[items.length]; for(int i=0; i<items.length; ++i) { data[i] = new CheckListItem(checked[i], items[i], i); }
checkList.setModel(new CheckListModel(data)); textList.setListData(items); } public void setSelectedIndex(int index) { textList.setSelectedIndex(index); checkList.setSelectedIndex(index); } public void addListSelectionListener(ListSelectionListener listener) { textList.addListSelectionListener(listener); } }
|
Du musst nur die JCheckbox-Liste durch eine JLabel-Liste ersetzten, die dir dann die Icons zurückgibt.
Good Luck Dieser Post wurde am 22.04.2006 um 11:46 Uhr von KaraHead editiert. |