Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7897129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:04:53+00:00 2026-06-03T08:04:53+00:00

I have a model with some string values.This model, I apply it to two

  • 0

I have a model with some string values.This model, I apply it to two jlists. I need every time that I click from one jlist a value, that value to dissapear from the other. Then the same if it happents to the other jlist but first the values must be updated to those the model contains. I made some effort but with my code when I click one value then it dissapears on both lists!
What am I doing wrong?
Here is the code:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

package accessfiletest;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;


/**
 *
* @author 
*/

 @SuppressWarnings("serial")
public class MoveFolders extends JFrame  
{
//start of class MoveFolders 
//start of variables
private DefaultListModel<String> theModel;
private DefaultListModel<String> fromModel;
private DefaultListModel<String> toModel;
private JList<String> fromJList;
private JList<String> toList;
private JButton moveButton;
private JPanel theJPanel;
//end of variables
public MoveFolders( DefaultListModel<String> model1)
{
 super("Μετακίνηση Εγγράφων από Φάκελο σε Φάκελο");
 fromModel=model1;
 toModel=model1;
 theModel=model1;
 theJPanel=new JPanel(null);
 fromJList=new JList<>(fromModel);
 fromJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 fromJList.setSelectedIndex(0);
 fromJList.addMouseListener(fromlistener);

JScrollPane frompane=new  JScrollPane(fromJList);
frompane.setBounds(50, 50, 200, 150);
theJPanel.add(frompane);
moveButton=new JButton("ΜΕΤΑΚΙΝΗΣΗ >>");
moveButton.setBounds(260, 90, 150, 20);
theJPanel.add(moveButton);
toList=new JList<>(toModel);
 if (model1.getSize()>1)
   {
    toList.setSelectedIndex(1);

   }
else
   {
    JOptionPane.showMessageDialog(null,
"Πρέπει να έχετε πάνω από 1 φάκελο για να γίνει αντιγραφή εγγράφων.\nΤο παράθυρο θα       κλείσει.", "Λάθος", JOptionPane.ERROR_MESSAGE);
 dispose();
   }
 toList.addMouseListener(toListener);
 toList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 JScrollPane topane=new  JScrollPane(toList);
 topane.setBounds(420, 50, 200, 150);
 theJPanel.add(topane);

 add(theJPanel);
 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
 setSize(670, 300);
 setVisible(true);
 }

 MouseListener fromlistener = new MouseAdapter() {
 public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        final int index = fromJList.locationToIndex(e.getPoint());
       SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                   toModel=theModel;
                   toModel.remove(index);
                }
            });
        }
   }
};

MouseListener toListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
    if (e.getClickCount() == 1) {
        final int index = fromJList.locationToIndex(e.getPoint());
        SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    fromModel=theModel;
                    fromModel.remove(index);
                }
            });
     }
   }

  };

}//end of class MoveFolders 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:04:55+00:00Added an answer on June 3, 2026 at 8:04 am

    Each list need use its own reference of ListModel,

        public MoveFolders(DefaultListModel<String> model1) {
            ...
            fromModel = new DefaultListModel<>();
            for (Object obj : model1.toArray()) {
                fromModel.addElement((String) obj);
            }
            toModel = new DefaultListModel<>();
            theModel = model1;
            ...
        }
    

    For move elements fromModel to toModel

        MouseListener fromlistener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    final int index = fromJList.locationToIndex(e.getPoint());
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            int index = fromJList.getSelectedIndex();
                            toModel.addElement(fromModel.getElementAt(index));
                            fromModel.remove(index);
                        }
                    });
                }
            }
        };
    

    For move elements toModel to fromModel

        MouseListener toListener = new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    final int index = fromJList.locationToIndex(e.getPoint());
                    SwingUtilities.invokeLater(new Runnable() {
    
                        @Override
                        public void run() {
                            int index = toList.getSelectedIndex();
                            fromModel.addElement(toModel.getElementAt(index));
                            toModel.remove(index);
                        }
                    });
                }
            }
    
        };
    
    
    }
    

    I hope that this can help you

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains data from some model. This class has metadata
I need some help with this one.... I have this simple model: public class
I have a pretty simple form with some fields from a doctrine model. $this->widgetSchema['fields']
I have a model that contains a dictionary property. (this has been distilled from
I have model Article it has field title with some text that may contain
I have some basic questions of calling a Fortran model from GAE. I uploaded
I have a JTable where i enter some values. This is the listener function
I have a simple ComboBox that has some simple values. I'm trying to do
Problem: I have a variables from magento that stored in the model class and
I need to compare some Rails (2.3.11) model attribute values before and after a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.