选择两行后,默认情况下选择添加的行

时间:2013-10-07 10:35:16

标签: java swing jlist listcellrenderer

mKorbel为例,以此为例:

通过使用mKorbel建议的机制,我已经实现了创建所需的行为。只要我有一行涂成绿色(未选中),这就可以工作。但是,如果我添加一行,选择它然后添加一个新行,它工作正常,我得到一个新的未选择行。如果我添加第二行并且我选择了它,那么它被正确地涂成红色,但是在第二行(两个红色行)之后添加更多行时,默认情况下它们都被选中,这不是我想要的。我希望所有的行都是绿色的,直到我点击它们(双击)。有谁知道为什么会这样?只要我没有选择1个单元格,它为什么会起作用?为什么如果我选择了多于两个或所有行,它会在选定模式下不断添加新行?日Thnx

我的鼠标事件代码如下:

m_list = new JList<String>(m_listModel) 
{
    private MyCellRenderer cellRenderer = new MyCellRenderer();

    // emulate control down for multiple non contiguous selection on the
    // list.
    @Override
    // TODO fix here
    public void processMouseEvent(MouseEvent event) {
    int modifiers = event.getModifiers() | InputEvent.CTRL_MASK;

    m_myME = new MouseEvent((Component) event.getSource(),
        event.getID(), event.getWhen(), modifiers,
        event.getX(), event.getY(), event.getXOnScreen(),
        event.getYOnScreen(), event.getClickCount(),
        event.isPopupTrigger(), event.getButton());

    //if clicked twice
    if (event.getClickCount() == 2) {
        //if the flag is set to true consume event
        if ((MyCellRenderer.getFlag() == true)) {
        m_urlName = MyCellRenderer.getValue();
        m_myME.consume();
        //initiate parsing
        initiateParsing();
        }else{
        m_urlName = MyCellRenderer.getValue();
        }
        //if it is not consume it will emulate CTRL_MASK
        if (!m_myME.isConsumed()) {
        super.processMouseEvent(m_myME);
        m_urlName = MyCellRenderer.getValue();
        //initiate parsing process
        initiateParsing();
        }
     }
    }
};

CellRenderer中的代码如下:

 public static class MyCellRenderer extends JLabel implements
        ListCellRenderer {

    private static final long serialVersionUID = 1L;
    private static boolean myFlag = false;
    private static String thisValue;

    public MyCellRenderer() {
        setOpaque(true);

    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());

        Color background = null;
        Color foreground = null;

        if (isSelected == true) {

        background = Color.RED;
        foreground = Color.WHITE;
        myFlag = true;
        } else {

        background = Color.GREEN;
        foreground = Color.BLACK;
        myFlag = false;
        }
        setBackground(background);
        setForeground(foreground);

    public static class MyCellRenderer extends JLabel implements
        ListCellRenderer {

    private static final long serialVersionUID = 1L;
    private static boolean myFlag = false;
    private static String thisValue;

    public MyCellRenderer() {
        setOpaque(true);

    }

    public Component getListCellRendererComponent(JList list, Object value,
        int index, boolean isSelected, boolean cellHasFocus) {
        setText(value.toString());

        Color background = null;
        Color foreground = null;

        if (isSelected == true) {

        background = Color.RED;
        foreground = Color.WHITE;
        myFlag = true;
        } else {

        background = Color.GREEN;
        foreground = Color.BLACK;
        myFlag = false;
        }
        setBackground(background);
        setForeground(foreground);

        // the string where its pointing at
        thisValue = value.toString();
        m_index = index;
        return this;
    }

    public static boolean getFlag() {
        return myFlag;
    }

    public static String getValue() {
        return thisValue;
    }
 }

1 个答案:

答案 0 :(得分:1)

  • 正如我在上一个问题中提到的那样,您可以使用MouseEvent.consume()但不能使用XxxRenderer

  • (我不想发表评论)选择必须存储在XxxModel中,否则一切尝试都是错误的,或者更好的情况是由另一种副作用造成的

  • 例如,使用model_to_view,.... DefaultListSelectionModel

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class JListDisabledItemDemo implements ItemListener, Runnable {

    private JFrame f = new JFrame("Colors");
    private static final String ITEMS[] = {" black ", " blue ", " green ",
        " orange ", " purple ", " red ", " white ", " yellow "};
    private JList jList;
    private JCheckBox[] checkBoxes;
    private boolean[] enabledFlags;

    @Override
    public void run() {
        JPanel pnlEnablers = new JPanel(new GridLayout(0, 1));
        pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items"));
        checkBoxes = new JCheckBox[ITEMS.length];
        enabledFlags = new boolean[ITEMS.length];
        for (int i = 0; i < ITEMS.length; i++) {
            checkBoxes[i] = new JCheckBox(ITEMS[i]);
            checkBoxes[i].setSelected(true);
            checkBoxes[i].addItemListener(this);
            enabledFlags[i] = true;
            pnlEnablers.add(checkBoxes[i]);
        }
        jList = new JList(ITEMS);
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList.setSelectionModel(new DisabledItemSelectionModel());
        jList.setCellRenderer(new DisabledItemListCellRenderer());
        jList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    System.out.println("selection");
                }
            }
        });
        JScrollPane scroll = new JScrollPane(jList);
        scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        Container contentPane = f.getContentPane();
        contentPane.setLayout(new GridLayout(1, 2));
        contentPane.add(pnlEnablers);
        contentPane.add(scroll);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocation(240, 280);
        UIManager.put("List.background", Color.lightGray);
        UIManager.put("List.selectionBackground", Color.orange);
        UIManager.put("List.selectionForeground", Color.blue);
        UIManager.put("Label.disabledForeground", Color.magenta);
        SwingUtilities.updateComponentTreeUI(f);
        f.pack();
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                f.setVisible(true);
            }
        });
    }

    @Override
    public void itemStateChanged(ItemEvent event) {
        JCheckBox checkBox = (JCheckBox) event.getSource();
        int index = -1;
        for (int i = 0; i < ITEMS.length; i++) {
            if (ITEMS[i].equals(checkBox.getText())) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            enabledFlags[index] = checkBox.isSelected();
            jList.repaint();
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new JListDisabledItemDemo());
    }

    private class DisabledItemListCellRenderer extends DefaultListCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            Component comp = super.getListCellRendererComponent(list, value, index, false, false);
            JComponent jc = (JComponent) comp;
            if (enabledFlags[index]) {
                if (isSelected & cellHasFocus) {
                    comp.setForeground(Color.black);
                    comp.setBackground(Color.red);
                } else {
                    comp.setForeground(Color.blue);
                }
                if (!isSelected) {
                    if ((value.toString()).trim().equals("yellow")) {
                        comp.setForeground(Color.orange);
                        comp.setBackground(Color.magenta);
                    }
                }
                return comp;
            }
            comp.setEnabled(false);
            return comp;
        }
    }

    private class DisabledItemSelectionModel extends DefaultListSelectionModel {

        private static final long serialVersionUID = 1L;

        @Override
        public void setSelectionInterval(int index0, int index1) {
            if (enabledFlags[index0]) {
                super.setSelectionInterval(index0, index0);
            } else {
                /*
                 * The previously selected index is before this one,
                 * so walk forward to find the next selectable item.
                 */
                if (getAnchorSelectionIndex() < index0) {
                    for (int i = index0; i < enabledFlags.length; i++) {
                        if (enabledFlags[i]) {
                            super.setSelectionInterval(i, i);
                            return;
                        }
                    }
                } /*
                 * Otherwise, walk backward to find the next selectable item.
                 */ else {
                    for (int i = index0; i >= 0; i--) {
                        if (enabledFlags[i]) {
                            super.setSelectionInterval(i, i);
                            return;
                        }
                    }
                }
            }
        }
    }
}