0

I am trying to device a way to make a list selected items not change, that is, the selections are final. Only newly added or non clicked items can be changed but not the ones which have already been clicked and are already in the list. Following is an example by mKorbel (see answer 8). Is there a way to make the selections permanent on this example? THNX!

import java.awt.Component;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Ctrl_Down_JList {

    private static void createAndShowUI() {
        String[] items = {"Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"};
        JList myJList = new JList(items) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void processMouseEvent(MouseEvent e) {
                int modifiers = e.getModifiers() | InputEvent.CTRL_MASK;
                // change the modifiers to believe that control key is down
                int modifiersEx = e.getModifiersEx() | InputEvent.CTRL_MASK;
                // can I use this anywhere?  I don't see how to change the modifiersEx of the MouseEvent
                MouseEvent myME = new MouseEvent((Component) e.getSource(), e.getID(), e.getWhen(), modifiers, e.getX(),
                        e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getButton());
                super.processMouseEvent(myME);
            }
        };
        JFrame frame = new JFrame("Ctrl_Down_JList");
        frame.getContentPane().add(new JScrollPane(myJList));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
Community
  • 1
  • 1
bluetxxth
  • 121
  • 2
  • 16
  • 1
    have to test if is flagged in SelectionModel, if isSelected, then to consume() mouse event – mKorbel Oct 06 '13 at 07:35
  • What if I make a mistake? – trashgod Oct 06 '13 at 16:50
  • This seems to work partially. For reasons that I do not comprehend, if I add, for example, 1 row and I mark it selected, then add another one, then the added one is by default not selected, it works alright. If I, however select that second row, selected and add a third row after it then the third row and any other added beyond the second row are by default selected which is not what I want. – bluetxxth Oct 07 '13 at 09:04

0 Answers0