Override method that toggles 'isSelected' in getListCellRendererComponent

I would like to make my own method that controles when a component is 'isSelected'.

I have a JList containing multiple JPanel. The constructing class of the JPanel extends ListCellRenderer<>.

To show that one of the JList-component (the JPanels) is selected i use;

public Component getListCellRendererComponent(..., boolean isSelected, ...) {
    } else {
    return this;

I would like a method that keeps a selected item 'selected' eventhough I choose to select another. I understand this can be done by holding down CTRL, but .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); does not quite do the trick. I would rather like to select multiple by clicking on them, and deselect by clicking on them.

For this i have worked with the ListSelectionMode, but i cant find a way.

When done the above I would like to implement a method that only selects a component in the list when clicked in a certain area (instead of the whole component which is preset). I have made this method, which returns true if the correct area is clicked, else false. But since I cant figure out how to override the mouseevent that makes the components 'isSelected' this has been tricky.

Here is the code for the method I would like to override the 'isSelected' method;

this.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
                //This code runs if that special place is clicked!
                //So now the component should be 'isSelected' or
                //deselected if it already was 'isSelected'.

This code is in the constructor of my JList

And the mouseClickedPrebuild method;

public static boolean mouseClickedPrebuild(Point point) {
        int index = theJList.locationToIndex(point);
        Rectangle bounds = theJList.getCellBounds(index,index);
        Point p = bounds.getLocation();
        return ( ... long list of greater than & less than ...);
        //This gives the certain area which is accepted to return true


I solved the issue!

So I get my view showing by running this line;

// UI Class   JScrollPane               Custom JList
UIConstructor.listview.setViewportView(new ListView( -insert ArrayList here- ));

Here is my ListView. The custom DefaultListSelectionModel I used to solve my problem was posted by @FuryComptuers right here;

JList - deselect when clicking an already selected item

I had to make a few changes to the code, since the two methods in the selectionModel will run before my mouseevent. I saved the variabels staticly, so instead of running the code in setSelectionInterval I did it inside my mousePressed.

I then could add the boolean isSelected which returns true, if a curtain area within a specific list element is clicked.

public class ListViewd extends JList {

static boolean isSelected;
static Point point;

static boolean gS = false;
static int in0;
static int in1;

public Dimension getPreferredScrollableViewportSize() {
    Dimension size = super.getPreferredScrollableViewportSize();
    size.setSize(new Dimension(0,0));
    return size;

public ListView(ArrayList<System> items) {

    DefaultListModel<System> list = new DefaultListModel<System>();

    for (System item : items) {

    this.setSelectionModel(new DefaultListSelectionModel() {

        boolean gestureStarted = false;

        public void setSelectionInterval(int index0, int index1) {
            gS = gestureStarted;
            in0 = index0;
            in1 = index1;

            gestureStarted = true;

        public void setValueIsAdjusting(boolean isAdjusting) {
            if (!isAdjusting) {
                gestureStarted = false;


    ListSelectionModel selectionModel = this.getSelectionModel();

    addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            point = e.getPoint();
            isSelected = ActionHandler.mouseClickedPrebuild(point);

            if(!gS && isSelected){
                if (isSelectedIndex(in0)) {
                    selectionModel.removeSelectionInterval(in0, in1);
                } else {
                    selectionModel.addSelectionInterval(in0, in1);

    setCellRenderer(new ListModelPrebuild());

Need Your Help

Can I build my console as "any cpu" but make it run as 32-bit in dev?

.net console x86 64-bit edit-and-continue

The reason for my slightly odd question is simple. I'd like to run my console as 32-bit on my dev box simply because of the convenience of edit-and-continue, which can be a time-saver for silly mis...

Numpy savetxt heterogenous data

python numpy scipy scikit-learn

I have two arrays A and B such that: